Sub topic 6
Writing both themes in one declaration with light-dark()
This chapter assumes you know the prefers-color-scheme media query, CSS custom properties, and the color-scheme property from the earlier chapters. You should also be comfortable reading a basic data-theme toggle. Here you will replace paired light and dark rules with one declaration, and learn where that shortcut stops working.
The problem light-dark() solves
With a media query and custom properties, every themed color is written twice. You define the light value in :root. Then you repeat the dark value inside @media (prefers-color-scheme: dark). With a manual toggle you repeat the dark value a third time under :root[data-theme="dark"].
The three copies can drift apart. You can change one and forget the others. light-dark() removes the duplication by putting both values in the same place:
:root {
color-scheme: light dark;
}
body {
background: light-dark(#ffffff, #121212);
color: light-dark(#1a1a1a, #e6e6e6);
}The function takes two colors. It returns the first when the element uses a light color scheme and the second when it uses a dark one. There is no media query, no override block, and no JavaScript.
How the function decides
light-dark() does not read the operating system setting directly. It reads the used color scheme of the element where the value is applied. That scheme comes from the color-scheme property.
color-scheme: light darkmeans "support both, and follow the user's preference". The browser picks the scheme from the system setting.color-scheme: lightforces the light scheme on that element and its descendants.color-scheme: darkforces the dark scheme.
So color-scheme is the switch, and light-dark() is the lookup that uses the switch. This is why the previous chapter's color-scheme declaration matters. If you forget it, the page stays light and the dark values never appear.
What happens without both schemes
Suppose color-scheme is not set. The default is normal, which is treated as light. The function always returns the first color, so your dark theme never appears and the page never shows an error.
Suppose you set only color-scheme: dark. The function always returns the second color. That is correct, because you said the page is dark only.
You also need light dark on :root for the automatic behavior. Putting it on body works for the body's content but leaves the root canvas and the scrollbars unthemed.
Browser support and fallbacks
light-dark() has worked in all major browsers since mid-2024 (Chrome 123, Firefox 120, Safari 17.5). Older browsers treat the whole declaration as invalid and drop it.
You can use that to write a fallback for a direct declaration. List the plain value first and the light-dark() value second:
body {
background: #ffffff;
background: light-dark(#ffffff, #121212);
}An old browser drops the second line and keeps the first. A new browser uses the second.
This trick fails when light-dark() is inside a custom property. A custom property accepts almost any text, so the browser does not check the value when it parses the rule. The declaration is not dropped. Instead, the property that uses var() becomes invalid when the value is substituted, and the fallback line is not used. For that case, use @supports:
:root {
--bg: #ffffff;
--text: #1a1a1a;
}
@media (prefers-color-scheme: dark) {
:root {
--bg: #121212;
--text: #e6e6e6;
}
}
@supports (color: light-dark(#000000, #ffffff)) {
:root {
color-scheme: light dark;
--bg: light-dark(#ffffff, #121212);
--text: light-dark(#1a1a1a, #e6e6e6);
}
}
body {
background: var(--bg);
color: var(--text);
}Older browsers use the media-query version. Newer browsers use the @supports block, which comes later and has the same specificity, so it wins. If your audience is current browsers only, you can skip the fallback and keep the short version.
Combining light-dark() with custom properties
The most useful pattern is to put light-dark() inside your semantic tokens. Each token is defined once, and components keep using var():
:root {
color-scheme: light dark;
--bg: light-dark(#ffffff, #121212);
--surface: light-dark(#f4f5f7, #1e1e1e);
--text: light-dark(#1a1a1a, #e6e6e6);
--muted: light-dark(#52525b, #a1a1aa);
--border: light-dark(#d4d4d8, #3f3f46);
--link: light-dark(#0b5fff, #8ab4ff);
}
body {
background: var(--bg);
color: var(--text);
}
.card {
background: var(--surface);
border: 1px solid var(--border);
}
a {
color: var(--link);
}Each token's light and dark values sit on one line, so a reviewer can compare them at a glance.
Where the function is evaluated
For ordinary (unregistered) custom properties, the browser stores the text light-dark(#ffffff, #121212) and does not resolve it where the property is declared. The function is resolved wherever var(--bg) is used. That has a useful consequence. An element inside the page can switch its own scheme, and the tokens follow:
.banner {
color-scheme: dark;
background: var(--bg);
color: var(--text);
}The .banner is always dark, even when the rest of the page is light. It gets the second value of every token. You do not need to define a separate set of dark variables for it.
If you register a property with @property and the syntax <color>, the value is computed where it is declared instead. A descendant that changes color-scheme then inherits the already-resolved color. Do not register your theme tokens unless you want that behavior.
Simplifying the manual toggle
The manual toggle from the spine used three data-theme rules, and the dark values appeared twice. With light-dark() the toggle only changes color-scheme:
:root {
color-scheme: light dark;
--bg: light-dark(#ffffff, #121212);
--text: light-dark(#1a1a1a, #e6e6e6);
}
:root[data-theme="light"] {
color-scheme: light;
}
:root[data-theme="dark"] {
color-scheme: dark;
}
body {
background: var(--bg);
color: var(--text);
}There are three states:
| `data-theme` attribute | `color-scheme` on `:root` | Result |
|---|---|---|
| absent (system) | `light dark` | Follows the operating system |
| `light` | `light` | Always light |
| `dark` | `dark` | Always dark |
The JavaScript does not change. It still sets or removes the attribute and saves the choice. Because the colors are no longer duplicated in the toggle rules, adding a new token requires editing one line.
The "no flash" script from the persistence section also stays the same. It sets data-theme before the first paint, and color-scheme follows from the attribute.
What light-dark() cannot do
The function has limits you should know before you rely on it.
- Colors only. Both arguments must be colors. You cannot write
light-dark(1px, 2px)for a border width, or use it for a font weight. Abox-shadowcan use it only if you apply it to the color part, for examplebox-shadow: 0 1px 3px light-dark(rgb(0 0 0 / 0.2), rgb(0 0 0 / 0.6)). - Two schemes only. There is no third value. A high-contrast theme or a sepia theme needs a different mechanism, such as another
data-themevalue with its own custom properties. - Images. The
<picture>element andbackground-imageneed their own handling. Support forlight-dark()with image values is not reliable across browsers, so use the<picture>approach from the images section. - Media-query-only logic. If you need to change layout, spacing, or fonts in dark mode, you still need
@media (prefers-color-scheme: dark). The media query reads the system setting and does not respond to a manualdata-themeoverride unless you duplicate the rule.
That last point is a common source of bugs. A user who chose "light" in your toggle can still match prefers-color-scheme: dark, because the media query ignores your attribute. Rules inside the media query that change colors will then apply even though the user forced light. Keeping colors in light-dark() avoids this, because the function follows color-scheme and that does respond to your toggle.
Common errors
- Missing
color-scheme. The page never goes dark. Addcolor-scheme: light darkto:root. - Only one argument or a non-color argument. The whole declaration is invalid and the property falls back to its inherited or initial value. Check the dev tools for a struck-through declaration.
- Setting
color-scheme: darkonbodyonly. The canvas background and scrollbars stay light. Set it on:root. - Using the function in
@mediaconditions. It is a value function, not a condition. It cannot appear in a media query or in@supportsas a condition by itself. The@supports (color: light-dark(...))form in this chapter tests the declaration, which is allowed. - Expecting a transition on scheme change. Switching
color-schemechanges resolved colors, and the browser animates them if you havetransition: background-colorset on the elements. If you want an instant switch, do not add one.
Choosing between the approaches
Use light-dark() when the theme is only a set of colors and your supported browsers include it. Use the media-query override pattern when you must support older browsers, or when dark mode changes non-color properties. Mixing the two is common: tokens in light-dark(), with a media query for the occasional non-color adjustment such as the image filter from the images section.
Exercise
A page sets color-scheme: light dark on :root, and --bg: light-dark(#ffffff, #121212). A .panel element sets color-scheme: dark and background: var(--bg). The user's system is set to light. What background color does .panel get, and why?
Show answer
It gets #121212. The custom property is unregistered, so light-dark() is resolved where var(--bg) is used, on .panel. That element's used color scheme is dark, so the function returns its second color, regardless of the system setting.
Exercise
Your toggle sets data-theme="light" on :root. One component still turns dark for users whose system is dark. Its CSS is @media (prefers-color-scheme: dark) { .note { background: #222; } }. Why does this happen, and how would you fix it?
Show answer
The media query reads the system setting and ignores the data-theme attribute, so it still matches. Replace the media query with a token that uses light-dark(), for example background: light-dark(#f4f5f7, #222). The function follows color-scheme, which the data-theme="light" rule forces to light.