Using light-dark() without getting caught out
I honestly hadn't thought about "getting caught" by light-dark() — particularly when leaning heavily on it for this site — but Martin Underhill explains the user and rendering considerations, making me reflect on my choices as well.
honestly hadn’t thought about “getting caught” by light-dark() — particularly when leaning heavily on it for this site — but Martin 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. They’re not the exact colours I’d want, but my website looks fine.Again, it’s risky, so test thoroughly.
Like Martin, I initially began implementing color themes based on media queries sniffing out user preferences. But it became super cumbersome to write prefers-color-scheme time and again. I mean, totally DX-world problems. I also remembered the piece Sara Joy wrote with us a while back encouraging a jump into the light-dark() waters and thought it’d be nice to take that advice.
Doing that, I ran into another DX annoyance: writing two color values for each light-dark(). CSS variables help, of course, but even then it exhausted me. That says something more about me than the feature, but I digress. So, I took Martin’s process one step further to abstract all the things:
:root {
--gray-1: #eaeaea; /* Light */
/* ... */
--gray-10: #111; /* Dark */
--text: light-dark(var(--gray-1), var(--gray-10));
}
.article-content {
color: var(--text);
}
Very nice, at least for my penchant for laziness. But I ran into another issue because sometimes text needs to go the other direction. Keep abstracting. I could define the background variable which is normally the 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);
}
Works, but using a helper called “surface” on a property controlling color bothers me too. I may be lazy, but I’m a little anal with semantics. Hence:
: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 */
}
…where “reverse” means it goes the opposite direction of a normal “light-dark” call and the “dark” value is called first. But even that is still too much for my brain to handle because now I’m left with multiple tokens to remember if I want things to be super semantic:
: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 */
}
So I went with an even 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 */
}
Perfect, no. But I’m pretty happy with it.
Back to Martin and fallbacks, he goes about it with a classic cascade safeguard:
color: black;
color: light-dark(black, white);
background-color: white;
background-color: light-dark(white, black);
He’s totally right and I ought to be doing that as well. Maybe I will, but I couldn’t help thinking that a light-dark() fallback argument would be nice. Spitballing here, of course:
color: light-dark(var(--dark-light), var(--light-dark) / var(--dark-light));
Now we’re getting verbose. But so are fallbacks in general. Indeed, Martin asked if moving to light-dark() is even worth it given the need to think about fallbacks. Is it really more efficient than a media query at this point? His byte-savings were negligible. I decided to compare what the differences would have been on CSS-Tricks:
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%) |
Not bad, actually. Not an earth shattering difference but it does help me justify favoring DX even if I was just lucky it worked out that way.
Did you enjoy this article?
Recommend it — Standard Reader surfaces well-loved writing to more readers across the network.