legacy browsers fall back to rendered defaults
Martin Underhill's post on light-dark() frames the feature around the cost of the unsupported case. Without fallbacks, a browser that doesn't understand light-dark() typically renders black text on white in Light Mode and white text on dark grey in Dark Mode. Those aren't the intended colors, but the page still reads as usable. His conclusion remains a warning: it's risky, so test thoroughly.
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.Again, it’s risky, so test thoroughly.
from prefers-color-scheme to light-dark()
Chris Coyier's route to the feature ran through media queries. Building themes on repeated prefers-color-scheme blocks became cumbersome, and Sara Joy's earlier piece was the nudge to make the switch. Once inside light-dark(), the next annoyance was writing two color values per call. CSS variables reduce that burden, but not enough — so he abstracted further.
:root {
--gray-1: #eaeaea; /* Light */
/* ... */
--gray-10: #111; /* Dark */
--text: light-dark(var(--gray-1), var(--gray-10));
}
.article-content {
color: var(--text);
}
That works for text defaulting in one direction. For cases where text must invert against its background, the abstraction had to continue: define the background variable as the normal contrast to text.
:root {
--gray-1: #eaeaea; /* Light */
/* ... */
--gray-10: #111; /* Dark */
--text: light-dark(var(--gray-1), var(--gray-10));
--surface: light-dark(var(--gray-10), var(--gray-1));
}
.widget-content {
color: var(--surface);
}
Functionally fine, but semantically awkward — calling a helper named "surface" on a color property. Naming matters, so the next step renamed things:
:root {
--gray-1: #eaeaea; /* Light */
/* ... */
--gray-10: #111; /* Dark */
--text: light-dark(var(--gray-1), var(--gray-10));
--text-reverse: light-dark(var(--gray-10), var(--gray-1));
}
.article-content {
color: var(--text-reverse); /* Light then dark */
}
.widget-content {
color: var(--text-reverse); /* Dark then light */
}
Here "reverse" means the opposite direction of a normal light-dark call, with the dark value listed first. Even so, it leaves multiple tokens to remember for semantic use:
:root {
--gray-1: #eaeaea; /* Light */
/* ... */
--gray-10: #111; /* Dark */
--text: light-dark(var(--gray-1), var(--gray-10));
--text-reverse: light-dark(var(--gray-10), var(--gray-1));
--surface: light-dark(var(--gray-1), var(--gray-10));
--surface-reverse: light-dark(var(--gray-10), var(--gray-1));
}
.article-content {
background-color: var(--surface); /* Dark then light */
color: var(--text-reverse); /* Light then dark */
}
.widget-content {
background-color: var(--surface-reverse); /* Light then dark */
color: var(--text-reverse); /* Dark then light */
}
The final move was a more general abstraction:
:root {
--gray-1: #eaeaea; /* Light */
/* ... */
--gray-10: #111; /* Dark */
--light-dark: light-dark(var(--gray-1), var(--gray-10));
--dark-light: light-dark(var(--gray-10), var(--gray-1));
}
.article-content {
background-color: var(--dark-light); /* Dark then light */
color: var(--light-dark); /* Light then dark */
}
.widget-content {
background-color: var(--dark-light); /* Dark then light */
color: var(--light-dark); /* Light then dark */
}
Not perfect, but satisfactory.
fallbacks and the cascade
Underhill's fallback is a classic cascade safeguard:
color: black;
color: light-dark(black, white);
background-color: white;
background-color: light-dark(white, black);
Chris concedes he ought to do the same, and wonders whether the function itself could take a fallback argument — spitballing, not proposing:
color: light-dark(var(--dark-light), var(--light-dark) / var(--dark-light));
That quickly turns verbose, as fallbacks generally do.
the size comparison
The open question is whether a light-dark() move earns its keep once fallbacks enter the picture. Is it more efficient than a media query by then? Underhill's savings were negligible, so Chris measured the difference against the existing CSS-Tricks stylesheet:
function | prefers-color-scheme media query | 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 isn't encouraging. No performance tests were run, but the working conclusion is that the trade-off was developer experience over performance.



