A dark preview inside a light page is a good test for a theme. If the preview’s colors come from @media (prefers-color-scheme: dark), changing the system setting changes the whole page. I want the preview and the surrounding page to be able to use the same CSS and pick their colors independently. light-dark() gives me a way to do that.

Start with color-scheme

light-dark() takes two colors, with the light value first and the dark value second. The browser picks one based on the element’s used color scheme. Setting color-scheme: light dark on :root tells the browser that the page supports both, so it can use the visitor’s preference. The same property also lets browser-styled controls and scrollbars match the page. CSS Color Adjustment, color-scheme

CSS

:root {
  color-scheme: light dark;
}

body {
  background-color: light-dark(#f4f6fa, #101522);
  color: light-dark(#1c2538, #f3f6ff);
}

Try this with a paragraph and a text input on the page. Switch the emulated color scheme in DevTools and both the page colors and the input’s default styling follow it. If the page stays light, check the computed color-scheme first. On an otherwise unstyled page, removing the :root rule leaves the function picking the first color.

Put both schemes on the same page

Switch the demo’s surrounding scheme between Light and Dark. The area around the previews changes color. Each preview and its text field keep their own scheme. Choose System to follow your browser’s preference again.

Surrounding scheme

Surrounding page

color-scheme: light
color-scheme: dark

Add these two previews inside the body. Each uses the same .preview rule and gets its own color-scheme from a modifier class.

HTML

<div class="preview preview--light">
  <label>Light preview <input type="text" /></label>
</div>
<div class="preview preview--dark">
  <label>Dark preview <input type="text" /></label>
</div>
CSS

.preview {
  padding: 1rem;
  background-color: Canvas;
  background-color: light-dark(#fff, #1b2435);
  color: CanvasText;
  color: light-dark(#1c2538, #f3f6ff);
}

.preview--light {
  color-scheme: light;
}

.preview--dark {
  color-scheme: dark;
}

Try removing both color declarations from .preview while the page is light. The dark panel keeps its dark background, and its label inherits the dark text from body. The browser already resolved the body’s light-dark() before the color was inherited. Setting color on .preview gives the function a chance to use the preview’s scheme. CSS Color Level 5, light-dark()

This also fits a theme picker. When someone explicitly chooses light or dark, set color-scheme to that value on the root. When they choose System, restore light dark. A prefers-color-scheme media query still checks the visitor’s preference and is useful for rules that should follow that preference, like choosing an image in a <picture> element.

Keep a fallback when using custom properties

The preview puts Canvas and CanvasText immediately above the declarations that use light-dark(). An older browser ignores the unsupported declarations and uses the system colors for the background and text.

If I move the pair into a custom property, I guard the assignment with @supports. An unregistered custom property can hold the function as text in an older browser. The failure happens later, when var() substitutes that text into color. By then, the cascade has already picked the declaration. CSS Custom Properties, invalid variables

CSS

:root {
  --text: CanvasText;
}

@supports (color: light-dark(black, white)) {
  :root {
    --text: light-dark(#1c2538, #f3f6ff);
  }
}

.preview {
  color: var(--text);
}

The unregistered --text property carries the pair down to .preview. The browser picks the color when it computes the preview’s color declaration, so the token still works in both panels.

Swap a background image

light-dark() can also take image values. Here, the light preview gets a gradient and the dark preview skips it. The background-color from the earlier rule still provides the surface in both schemes.

CSS

.preview {
  background-image: light-dark(linear-gradient(135deg, #dbeafe, transparent), none);
}

Browser support for the image form differs from the color form. If you’re checking for it with @supports, test a background-image declaration. MDN has a live gradient example you can try in your own browser. MDN, light-dark()