light-dark() beats media queries by 7.2% on real CSS
Replacing prefers-color-scheme queries with light-dark() cut CSS-Tricks' minified CSS by 12,262 bytes (7.2%). A custom-property abstraction removes the repetitive argument pairs, but fallbacks remain a manual task.
- AuthorSophie Lindqvist
- ChannelDesign Tools
- Filed
- Read3 min

- Minified CSS: 169,948 B with light-dark() versus 182,210 B with media queries, a 12,262 B (7.2%) difference
- Gzip difference: 1,207 B (+4.0%); Brotli difference: 337 B (+1.4%)
- Unsupported browsers default to black-on-white in Light Mode and white-on-dark-grey in Dark Mode without fallbacks
- Cascade fallback pattern: declare color: black before color: light-dark(black, white)
- Grayscale token scale runs from --gray-1: #eaeaea to --gray-10: #111
Swapping prefers-color-scheme media queries for the light-dark() function saved 12,262 bytes on CSS-Tricks' minified stylesheet: 169,948 B versus 182,210 B, a 7.2% reduction. Compressed, the gap narrows to 1,207 B (+4.0%) under gzip and 337 B (+1.4%) under Brotli. The numbers come from a direct comparison of both approaches applied to the same site's stylesheets.
The measurement answers a question Martin Underhill raised directly: he asked whether moving to light-dark() is even worth it, given that developers still have to think about fallbacks. His own byte-savings, he noted, were negligible. The CSS-Tricks figures suggest the answer is a qualified yes — the savings are real but modest, and the stronger argument is developer experience, not payload.
What changes for developers who write themes by hand?
The pre-light-dark() workflow required repeating the prefers-color-scheme media query for every color pair. The author describes this as cumbersome to write time and again. The function collapses that pattern into a single declaration: light-dark(black, white) resolves to black in light mode and white in dark mode, driven by the browser's color-scheme property rather than a query.
A second annoyance followed: writing two color values inside every light-dark() call. The solution was a layer of custom properties built on a grayscale scale from --gray-1: #eaeaea to --gray-10: #111:
- --light-dark: light-dark(var(--gray-1), var(--gray-10)) for normal text direction
- --dark-light: light-dark(var(--gray-10), var(--gray-1)) for reversed contrast, such as widget text on dark surfaces
Selectors then stay terse:
.article-content {
background-color: var(--dark-light);
color: var(--light-dark);
}
The author went through several iterations before landing here — a --text/--text-reverse pairing, then a --surface variant — and discarded each because the token names grew harder to keep straight. The final pair, described as "perfect, no. But I'm pretty happy with it," trades semantic naming for a general two-token system that requires remembering only the argument order.
Where does light-dark() still fail?
Browsers without support for the function do not break the layout outright. Underhill explains: "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." He adds: "Again, it's risky, so test thoroughly."
The safeguard is a two-declaration cascade: set color: black first, then color: light-dark(black, white) on the same rule. Browsers that cannot parse the function keep the earlier value; supporting browsers override it. The same pattern applies to background-color.
The author acknowledges he should be shipping these fallbacks and may do so, but proposes a different fix: a fallback argument inside the function itself, in the form light-dark(var(--dark-light), var(--light-dark) / var(--dark-light)). It is a sketch, not a proposal, and it raises the verbosity problem that fallbacks already carry.
For teams weighing the migration, the trade now looks like this: roughly 7% off the minified stylesheet, fewer repeated media queries, and a fallback layer that still has to be written by hand until the platform offers a built-in escape hatch.
Whether the CSS Working Group picks up a fallback argument will decide if the last manual step disappears.
Original: tempertemper.net
More from Sophie Lindqvist
Show full bio
Staff writer covering people, launches and day-to-day practice in Product and interaction design, design systems at Interface Wire.
190 articles