PUBLIC.INTERNET

CSS light-dark() Previewer

light-dark() lets one custom property hold both a light and a dark color, so you stop duplicating variables inside a prefers-color-scheme media query. Here you set a pair of colors for four tokens: background, text, border and accent. A sample card switches between the two with a Light and Dark toggle, and the CSS block updates as you go.

Set a light and a dark value for each token. The preview switches with the toggle, no media queries needed.

Scheme

Theme preview

This card uses the light-dark() function for its background, text, and accent. Flip the scheme to see both states from one set of declarations.

CSS


    

How to build a light-dark() theme

  1. In the token list, set a Light and a Dark color for each of Background, Text, Border and Accent using the color inputs.
  2. Use the Scheme toggle (Light or Dark) to switch the preview card between the two sets of values.
  3. Check the card heading, paragraph, border and accent button. The button label is always white.
  4. Read the CSS block. It has a :root rule with color-scheme: light dark and four --token: light-dark(...) lines, plus a .card rule that uses them.
  5. Click Copy CSS and read the note under the box about whether your browser supports the function.

How light-dark() picks a value

light-dark(a, b) returns the first color when the element's used color scheme is light and the second when it is dark. The used scheme comes from the color-scheme property, which is why the output sets color-scheme: light dark on :root. Without it the function returns the light value. With it, the page follows the user's OS preference, and you can force one scheme on any element by setting color-scheme there.

In current browsers the function takes colors only. You can use it as the color inside a box-shadow or gradient, but not to swap a whole shadow or image. Because the output stores it in custom properties, an older browser does not skip the line: the variable is accepted, then fails where it is used. A plain declaration written before it will not help, so wrap the token block in @supports (color: light-dark(#000, #fff)) and keep plain values outside it.

In this tool the preview is simulated by choosing the light or dark value in JavaScript and setting it on the card, so the toggle works in any browser. The accent button text is fixed to white and is not contrast-checked, so confirm that your accent works with white text before shipping.