5 min read
How to Use CSS light-dark() Without Falling Into Fallback and Performance Traps

- Reading
- 5 min
- Tables
- 1
- Code
- 4
light-dark() cuts down on repeating prefers-color-scheme in CSS, but without a fallback the visual breaks in older browsers, and the switch can cost more bytes than it seems.
The CSS light-dark() function promises to solve a long-standing annoyance for anyone implementing light and dark themes: no longer writing the same prefers-color-scheme block over and over just to swap a text or background color. It's been available in major browsers since 2024, but a recent CSS-Tricks post, written from Martin Underhill's experience, shows that adopting it carelessly has side effects: from screens with broken contrast in unsupported browsers to final CSS that's heavier than the traditional approach.
The article matters because it touches a territory every front-end developer revisits sooner or later: color theme architecture. And it doesn't sell the function as a final solution, it shows where it helps and where it charges a price.
What light-dark() Solves (and What It Requires Under the Hood)
The syntax is straightforward: light-dark(light-value, dark-value) returns the first value when the active scheme is light and the second when it's dark. Instead of duplicating an entire declaration inside two media query rules, the developer writes the color once, inline in the property.
.article-content {
color: light-dark(#111, #eaeaea);
}This only works if the browser knows which scheme is active for that element, and that depends on the color-scheme property, usually declared on :root with color-scheme: light dark. Without it, light-dark() has no way to decide which of the two values to apply, and that's exactly where the first trap in the title lives.
The Fallback Trap: What Breaks Without a Safeguard
According to Martin Underhill's account reproduced by CSS-Tricks, browsers that don't support light-dark() don't simply ignore the declaration, they apply a default behavior that can clash with the design:
Without fallbacks, browsers that don't support light-dark() typically use black text on a white background in Light Mode, and white text on a dark grey background in Dark Mode. They're not the exact colours I'd want, but my website looks fine.
Martin Underhill
"Fine" is the key word: it works, but it's not what was designed. Underhill's recommendation, repeated by the CSS-Tricks author, is the classic CSS cascade safeguard: declare the property twice, with the second overriding the first in browsers that understand light-dark().
color: black;
color: light-dark(black, white);
background-color: white;
background-color: light-dark(white, black);Browsers that don't recognize the function ignore the second line and stick with the first. Those that do recognize it override it with the correct pair. It's a pattern any front-end dev has already used for vendor prefixes or @supports, just applied within the same property. The author's warning is direct: "it's risky, so test thoroughly" before blindly trusting the function in production.
Abstracting Tokens Without Losing Your Mind
The point that matters most to anyone organizing a design system is the second half of the article: how to avoid writing the color pair every time. The proposed solution uses CSS variables as an intermediate layer.
:root {
--gray-1: #eaeaea; /* Light */
--gray-10: #111; /* Dark */
--text: light-dark(var(--gray-1), var(--gray-10));
}
.article-content {
color: var(--text);
}It works well until the inverse case shows up: a widget whose text needs to go in the opposite direction from the default (light on the app's dark background, dark on a light background). That's how the chain of names the author documents with self-deprecating humor is born, passing through --surface, --text-reverse and --surface-reverse, until arriving at a more generic abstraction:
--light-dark: light-dark(var(--gray-1), var(--gray-10));
--dark-light: light-dark(var(--gray-10), var(--gray-1));
background-color: var(--dark-light);
color: var(--light-dark);It's not elegant, the author himself admits it, but it solves the practical problem of having just two tokens to cover both contrast directions, instead of multiplying semantic names for every component combination.
DX vs. Performance: What the Numbers Show
The most useful part of the article, for anyone deciding whether it's worth migrating an entire project, is the comparison of final CSS weight. The author compared CSS-Tricks' own CSS compiled with light-dark() against an equivalent version using prefers-color-scheme:
| Format | light-dark() | prefers-color-scheme | Difference |
|---|---|---|---|
| Minified | 169,948 B | 182,210 B | +12,262 B (+7.2%) |
| Gzip | 30,215 B | 31,422 B | +1,207 B (+4.0%) |
| Brotli | 24,933 B | 25,270 B | +337 B (+1.4%) |
The result is counterintuitive: the prefers-color-scheme version came out larger, not smaller, in all three measurements. But the author's own reading is cautious, he acknowledges that the raw byte difference nearly disappears after Brotli compression (a 1.4% variance) and that he didn't run rendering performance tests, only file size ones. In other words: the DX gain (less repetition, centralized tokens) had, in his case, a neutral to slightly positive effect on final weight, but that doesn't replace measuring parse time and repaint in a real browser.
The critical point, which Martin Underhill also raises in the original text, is whether the need to maintain cascade fallbacks for older browsers cancels out the line savings that light-dark() promised. Writing each declaration twice, once raw and once inside the function, brings back part of the verbosity the function was supposed to eliminate.
When It's Worth Swapping Media Queries for light-dark()
For those maintaining a design system with dozens of color tokens, abstracting through CSS variables reduces the maintenance surface: a single place centralizes the light/dark pair for each semantic color, and the rest of the CSS just consumes the variable. That offsets the initial effort of naming the tokens correctly, something the CSS-Tricks author himself admits he underestimated.
It's worth considering when:
- The project already uses CSS variables as a token layer and wants to eliminate media query duplication;
- Support for very old browsers isn't critical, or the cascade fallback covers the edge case well;
- The team has a process to test rendering in light and dark mode before every release.
It's not worth it, or at least it calls for more caution, when the project depends on CSS weight as a hard performance metric (sites with a tight byte budget), when there's no time to write and validate the cascade fallbacks, or when the codebase already has a stable, tested prefers-color-scheme architecture: migrating for DX alone, without measuring the real rendering impact, is trading a solved problem for an unquantified risk.
Translated from the Brazilian Portuguese original · Read the original
Nudeps uses import maps to import npm packages in the browser without a bundler
The duo Lea Verou and Dmitry Sharabin launched Nudeps, a CLI that automatically generates and maintains import maps, allowing packages installed via npm to be imported by name, directly in HTML, without going through a bundler.





