CSS Reference ยท Functions
The light-dark() function
Reference for the CSS light-dark() function: pick one color for light mode and another for dark mode, how color-scheme controls it, and live examples.
The light-dark() function takes two colors and returns the first when the element uses a light color scheme and the second when it uses a dark one. It lets you write a theme-aware color in a single declaration instead of repeating it inside a prefers-color-scheme media query.
It only works when the element's color-scheme allows both schemes. Without that, it always picks the light value.
:root { color-scheme: light dark; } body { background: light-dark(#ffffff, #111827); color: light-dark(#1f2937, #e5e7eb); }
Try it
The first two cards force a scheme; the third follows your system preference.
Syntax
light-dark(<color>, <color>)
Syntax and arguments
| Argument | Description |
|---|---|
First <color> |
Used when the element's used color scheme is light. |
Second <color> |
Used when the used color scheme is dark. |
Usage notes
- Set
color-schemefirst. Putcolor-scheme: light darkon:rootso the browser follows the user's preference. Setting onlylightor onlydarkmakes the function always pick that side. - It follows the element, not the page. Because
color-schemeinherits, a card that setscolor-scheme: darkinside a light page gets the dark values. - Colors only (in practice). Use it for color values. For other properties, such as images or sizes, use a
prefers-color-schememedia query or switch custom properties. - Replaces duplication. One declaration instead of a base value plus a media query override.
- A theme toggle can set
color-schemeon:rootfrom a class or attribute, and everylight-dark()color updates at once. - Form controls and scrollbars also follow
color-scheme, so they match your colors.
Browser support
Supported in current versions of the major browsers; it is a fairly recent addition, so check a compatibility table and add a plain fallback color before it if you support older ones.
Related
color-schemecolor-mix()- Lesson: Dark mode