I truthfully hadn’t considered “getting caught” by light-dark() — notably when leaning closely on it for this website — however Martin Underhill explains:
With out fallbacks, browsers that don’t help
light-dark()usually use black textual content on a white background in Mild Mode, and white textual content on a darkish gray background in Darkish Mode. They’re not the precise colors I’d need, however my web site seems to be effective.Once more, it’s dangerous, so check totally.
Like Martin, I initially started implementing shade themes based mostly on media queries sniffing out person preferences. However it grew to become tremendous cumbersome to jot down prefers-color-scheme repeatedly. I imply, completely DX-world issues. I additionally remembered the piece Sara Pleasure wrote with us some time again encouraging a soar into the light-dark() waters and thought it’d be good to take that recommendation.
Doing that, I bumped into one other DX annoyance: writing two shade values for every light-dark(). CSS variables assist, in fact, however even then it exhausted me. That claims one thing extra about me than the function, however I digress. So, I took Martin’s course of one step additional to summary all of the issues:
:root {
--gray-1: #eaeaea; /* Mild */
/* ... */
--gray-10: #111; /* Darkish */
--text: light-dark(var(--gray-1), var(--gray-10));
}
.article-content {
shade: var(--text);
}
Very good, a minimum of for my penchant for laziness. However I bumped into one other difficulty as a result of typically textual content must go the opposite course. Maintain abstracting. I might outline the background variable which is often the distinction to textual content:
:root {
--gray-1: #eaeaea; /* Mild */
/* ... */
--gray-10: #111; /* Darkish */
--text: light-dark(var(--gray-1), var(--gray-10));
--surface: light-dark(var(--gray-10), var(--gray-1));
}
.widget-content {
shade: var(--surface);
}
Works, however utilizing a helper referred to as “floor” on a property controlling shade bothers me too. I could also be lazy, however I’m just a little anal with semantics. Therefore:
:root {
--gray-1: #eaeaea; /* Mild */
/* ... */
--gray-10: #111; /* Darkish */
--text: light-dark(var(--gray-1), var(--gray-10));
--text-reverse: light-dark(var(--gray-10), var(--gray-1));
}
.article-content {
shade: var(--text-reverse); /* Mild then darkish */
}
.widget-content {
shade: var(--text-reverse); /* Darkish then mild */
}
…the place “reverse” means it goes the other way of a traditional “light-dark” name and the “darkish” worth known as first. However even that’s nonetheless an excessive amount of for my mind to deal with as a result of now I’m left with a number of tokens to recollect if I would like issues to be tremendous semantic:
:root {
--gray-1: #eaeaea; /* Mild */
/* ... */
--gray-10: #111; /* Darkish */
--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); /* Darkish then mild */
shade: var(--text-reverse); /* Mild then darkish */
}
.widget-content {
background-color: var(--surface-reverse); /* Mild then darkish */
shade: var(--text-reverse); /* Darkish then mild */
}
So I went with an much more normal abstraction:
:root {
--gray-1: #eaeaea; /* Mild */
/* ... */
--gray-10: #111; /* Darkish */
--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); /* Darkish then mild */
shade: var(--light-dark); /* Mild then darkish */
}
.widget-content {
background-color: var(--dark-light); /* Darkish then mild */
shade: var(--light-dark); /* Mild then darkish */
}
Good, no. However I’m fairly proud of it.
Again to Martin and fallbacks, he goes about it with a basic cascade safeguard:
shade: black;
shade: light-dark(black, white);
background-color: white;
background-color: light-dark(white, black);
He’s completely proper and I must be doing that as nicely. Possibly I’ll, however I couldn’t assist considering {that a} light-dark() fallback argument can be good. Spitballing right here, in fact:
shade: light-dark(var(--dark-light), var(--light-dark) / var(--dark-light));
Now we’re getting verbose. However so are fallbacks basically. Certainly, Martin requested if shifting to light-dark() is even price it given the necessity to consider fallbacks. Is it actually extra environment friendly than a media question at this level? His byte-savings had been negligible. I made a decision to check what the variations would have been on CSS-Tips:
perform |
prefers-color-scheme media question |
Distinction | |
|---|---|---|---|
| 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 unhealthy, really. Not an earth shattering distinction nevertheless it does assist me justify favoring DX even when I used to be simply fortunate it labored out that method.







