One person reading your site at night has asked their operating system for Dark Mode. Another, a dyslexic reader, finds text easier when it isn't pure black on pure white. Both depend on colour declarations that their browser may not understand. When it doesn't, they get whatever is left over.
That is the warning behind a short CSS-Tricks post from Geoff Graham, dated 27 November. It links to Martin Underhill's article "Using light-dark() without getting caught out" on tempertemper.net. Graham says he hadn't thought about getting caught out by light-dark(), even though he leans on it heavily for CSS-Tricks. He then shows how he arranges his own colour tokens around it.
Two values and a prerequisite
Underhill's explanation is short. light-dark() takes two values. The browser uses the first in Light Mode and the second in Dark Mode:
color: light-dark(black, white);
background-color: light-dark(white, black);
Before it does anything, you have to tell the browser which colour schemes the site supports. You can do that with a meta element in the <head>, or in CSS on :root, which Underhill says is the better choice in most cases:
<meta name="color-scheme" content="light dark">
:root {
color-scheme: light dark;
}
When the browser says no, it drops everything
Underhill had hoped an older browser might still pull a colour out of light-dark() even if it didn't understand the function. It doesn't. It ignores the entire declaration. He put support at just under 90% when he wrote the piece. That leaves a real group of visitors who see browser defaults, which he says are typically black text on white in Light Mode and white text on dark grey in Dark Mode. His own site looks fine that way, but he still calls skipping fallbacks risky and says to test thoroughly.
The anti-pattern is assuming the remaining visitors see something close enough. The fix is the oldest trick in CSS, a declaration the browser does understand placed first:
color: black;
color: light-dark(black, white);
background-color: white;
background-color: light-dark(white, black);
A browser without support applies the first line and discards the second.
Grey on grey
Every team should read this section of Underhill's article. His fallback text colour is an off-black, #0b0c0c, because high-contrast black text on a white background can cause visual stress for some dyslexic readers. That's a good call. It also exposed a gap. According to Underhill, color-scheme got solid support in early 2022 and light-dark() in mid 2024, and some browsers, with Opera Mini as his example, still support the first but not the second.
So picture a visitor in that gap who prefers Dark Mode. color-scheme gives them the browser's dark background. light-dark() is ignored. His dark grey fallback text sits on a dark background. Two sensible decisions, made separately, add up to text nobody can read comfortably.
His fix turns on both colour schemes only when light-dark() works:
@supports (color: light-dark(black, white)) {
:root {
color-scheme: light dark;
}
}
Browsers with the mismatch skip the block, so the fallback text stays on the normal light background.
Name the job, not the direction
Graham's half of the post is about developer experience. Writing prefers-color-scheme media queries over and over got cumbersome, so he moved to light-dark(), then to custom properties so he wasn't typing two values every time:
:root {
--gray-1: #eaeaea; /* Light */
/* ... */
--gray-10: #111; /* Dark */
--text: light-dark(var(--gray-1), var(--gray-10));
}
.article-content {
color: var(--text);
}
From there he tries --surface, --text-reverse and --surface-reverse, then settles on two generic tokens, --light-dark and --dark-light. His own verdict: "Perfect, no. But I'm pretty happy with it."
My concern is contrast, which is always about a pair: text against the surface under it. A token called --dark-light says which way it flips, not what it's for. Nothing in that name tells the next developer it needs a matching background. The post shows how easy the direction is to lose. In the --text-reverse step, the same color: var(--text-reverse) declaration is commented "Light then dark" on one rule and "Dark then light" on the other. Read literally, the first snippet also puts #eaeaea, the value labelled Light, in the Light Mode slot of --text. If the author can mix that up while writing the post, so can whoever inherits the stylesheet. I'd keep role names like --text and --surface, define them in pairs, and check each pair in both schemes.
Graham also suggests that a fallback argument inside light-dark() would be nice. He calls it spitballing, and it isn't syntax you can ship.
The byte count settles nothing
Both authors measured. Underhill's generated CSS shrank by 0.923 kB, but compressed transfer grew slightly, by 149 bytes with Gzip and 132 with Brotli. Graham's CSS-Tricks comparison went the other way: a prefers-color-scheme version would have been 12,262 bytes (7.2%) larger minified and 337 bytes (1.4%) larger with Brotli. Neither result changes anything for a reader. The case for light-dark() rests on maintainability, and maintainability includes the fallbacks and the token names.
Verdict
Underhill thinks light-dark() is worthwhile for him, with sensible fallbacks where they matter. I agree, as long as you decide on purpose what a browser without support shows. Before your next merge:
- Put a plain colour declaration before any
light-dark()whose fallback matters to readers. - If your fallback text isn't the browser default, wrap
color-schemein the@supportscheck. - Test with
light-dark()unsupported in both Light Mode and Dark Mode. - Name tokens by role and check text and surface as a pair.
Top comments (0)