← Kembali ke Daftar Berita

Using light-dark() without getting caught out

Tanggal: 2026-12-02

Using light-dark() without getting caught out
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:

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.

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:

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:

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:

…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:

So I went with an even more general abstraction:

Perfect, no. But I’m pretty happy with it.

Back to Martin and fallbacks, he goes about it with a classic cascade safeguard:

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:

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:

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.

Really appreciate the reminder about setting color-scheme on :root before relying on light-dark(). I’ve seen cases where a third-party widget or embedded canvas inherits unexpected defaults when color-scheme isn’t explicitly scoped.

Are you sure the color: var(--text-reverse); /* Light then dark */ is correct? To make the comment match the defined variable, shouldn’t it be color: var(--text); /* Light then dark */ instead?

Your email address will not be published. Required fields are marked *

Save my name, email, and website in this browser for the next time I comment.

Notify me of follow-up comments by email.

Join our community of front-end nerds! You'll get into the Discord server and member-only perks and free swag.

Sign up to get front-end roundups, browser updates, and exclusive content sent to your inbox bi-weekly.

Get your t-shirts, prints, stickers, and yes, even mousepads at the brand-spankin' new CSS-Tricks store, now open.

CSS-Tricks is powered by a small team of real-life humans.

Handwritten. Twice a month. No spam. See past issues →

→ Baca artikel asli