Sub topic 6
Writing both themes in one declaration with light-dark()
This chapter assumes you know basic HTML and CSS, including selectors and the cascade. It also assumes you know how prefers-color-scheme and CSS custom properties work from the earlier chapters. You will learn how light-dark() lets you put both theme colors in a single declaration, and where it stops being enough.
The problem it solves
With a media query, every themed color is written twice: once in the default rules and once inside @media (prefers-color-scheme: dark). The two copies can drift apart as the stylesheet grows. light-dark() is a CSS function that takes two colors and returns one of them, depending on the active color scheme. The first argument is used in a light scheme, and the second in a dark scheme.
Step 1: Declare the color schemes
light-dark() only works when the element has a computed color-scheme that includes light, dark, or both. Without that, it always returns the first (light) color. Set the property on the root element:
:root {
color-scheme: light dark;
}
body {
background: light-dark(#ffffff, #121212);
color: light-dark(#1a1a1a, #e6e6e6);
}color-scheme: light dark tells the browser that the page supports both schemes. The browser then picks the one that matches the user's system setting. The same property also makes built-in UI such as form controls and scrollbars follow the scheme.
Step 2: Combine it with tokens
You can use light-dark() as the value of a custom property. Then the rest of the stylesheet keeps using semantic tokens and never repeats the function:
:root {
color-scheme: light dark;
--bg: light-dark(#ffffff, #121212);
--text: light-dark(#1a1a1a, #e6e6e6);
--link: light-dark(#0b5fff, #8ab4ff);
}
body {
background: var(--bg);
color: var(--text);
}
a {
color: var(--link);
}Compare this with the media query version. The three tokens now take three lines instead of nine, and each light value sits next to its dark partner. That makes it easy to see when one half has been forgotten.
How the browser chooses
The function reads the used color scheme of the element it is applied to. Because color-scheme is inherited, a child can switch schemes on its own. This is useful for a panel that should always be dark:
.always-dark {
color-scheme: dark;
background: light-dark(#ffffff, #1e1e1e);
color: light-dark(#1a1a1a, #e6e6e6);
}Inside .always-dark, both declarations resolve to their second color, even when the rest of the page is light.
Common errors
- Forgetting
color-scheme. The function then always returns the light color. - Swapping the argument order. The light color comes first. A wrong order gives a dark page in light mode with no error message.
- Passing non-color values.
light-dark()accepts colors only. In older versions of the specification it cannot choose lengths, fonts, or images. For those, use custom properties inside a media query. - Assuming universal support. The function is supported in current versions of the major browsers, but not in older ones. If you must support older browsers, put a plain fallback declaration before it. A browser that does not understand the function ignores the second line.
body {
background: #ffffff;
background: light-dark(#ffffff, #121212);
}Where it fits with a manual toggle
light-dark() follows color-scheme, so a manual toggle can work by changing that one property. Setting color-scheme: dark on the root forces the dark colors, and color-scheme: light forces the light ones. The next chapters build on this idea, and you will see that it avoids rewriting every token for each theme.
If your design needs more than two colors per token, for example a third high-contrast theme, a custom property set per theme is still the better tool.
Exercise
A page sets background: light-dark(#fff, #111) on body, but it always shows a white background, even when the operating system is in dark mode. What is the most likely cause, and how do you fix it?
Show answer
The element's computed color-scheme does not include dark, which is the default normal value, so light-dark() returns its first color. Add color-scheme: light dark; to :root.
Exercise
Rewrite these rules so that each color is declared once. :root { --text: #1a1a1a; } @media (prefers-color-scheme: dark) { :root { --text: #e6e6e6; } }
Show answer
:root { color-scheme: light dark; --text: light-dark(#1a1a1a, #e6e6e6); }