2.4 · Color

Theme in one line: light-dark()

Write both theme colors in one declaration and let color-scheme pick.

light-dark(white, black) returns the first color in a light color scheme and the second in a dark one. Set color-scheme: light dark on the root and the browser follows the system setting. Set it to light or dark on any element to force a theme for that subtree.

Form controls and scrollbars follow color-scheme too. This site uses light-dark() for all of its colors.

Before
:root { --surface: white; }

@media (prefers-color-scheme: dark) {
  :root { --surface: #1d1f27; }
}
[data-theme='dark'] {
  --surface: #1d1f27; /* again */
}
Now
:root {
  color-scheme: light dark;
  --surface: light-dark(white, #1d1f27);
}

[data-theme='dark'] {
  color-scheme: dark;
}