<p>The other day, Florens Verschelde asked about defining dark mode styles for both a class and a media query, without repeat CSS custom properties declarations. I had run into this issue in the past but hadn’t come up with a proper solution. </p> <p>What we want is to avoid redefining—and thus repeating—custom properties when switching between light and dark modes. That’s the goal of DRY (Don’t Repeat Yourself) programming, but the typical pattern for switching themes is usually something like this: </p> <p>:root {<br /> &#8211;background: #fff;<br /> &#8211;text-color: #0f1031;<br /> /* etc. */<br /> } </p> <p>@media (prefers-color-scheme: dark) {<br /> :root {<br /> &#8211;background: #0f1031;<br /> &#8211;text-color: #fff;<br /> /* etc. */<br /> }<br /> } </p> <p>See what I mean? Sure, it might not seem like a big deal in an abbreviated example like this, but imagine juggling dozens of custom properties at a time—that’s a lot of duplication! </p> <p>Then I remembered Lea Verou’s trick using &#8211;var: ;, and while it didn’t hit me at first, I found a way to make it work: not with var(&#8211;light-value, var(&#8211;dark-value)) or some nested combination like that, but by using both side by side! </p> <p>Certainly, someone smarter must have discovered this before me, but I haven‘t heard of leveraging (or rather abusing) CSS custom properties to achieve this. Without further ado, here’s the idea: </p> <p>&#8211;color: var(&#8211;light, orchid) var(&#8211;dark, rebeccapurple); </p> <p>If the &#8211;light value is set to initial, the fallback will be used (orchid), which means &#8211;dark should be set to a whitespace character (which is a valid value), making the final computed value look like this: </p> <p>&#8211;color: orchid ; /* Note the additional whitespace */ </p> <p>Conversely, if &#8211;light is set to a whitespace and &#8211;dark to initial, we end up with a computed value of: </p> <p>&#8211;color: rebeccapurple; /* Again, note the whitespace */ </p> <p>Now, this is great but we do need to define the &#8211;light and &#8211;dark custom properties, based on the context. The user can have a system preference in place (either light or dark), or can have toggled the website‘s theme with some UI element. Just like Florens‘s example, we’ll define these three cases, with some minor readability enhancement that Lea proposed using “on” and “off” constants to make it easier to understand at a glance: </p> <p>:root {<br /> /* Thanks Lea Verou! */<br /> &#8211;ON: initial;<br /> &#8211;OFF: ;<br /> } </p> <p>/* Light theme is on by default */<br /> .theme-default,<br /> .theme-light {<br /> &#8211;light: var(&#8211;ON);<br /> &#8211;dark: var(&#8211;OFF);<br /> } </p> <p>/* Dark theme is off by default */<br /> .theme-dark {<br /> &#8211;light: var(&#8211;OFF);<br /> &#8211;dark: var(&#8211;ON);<br /> } </p> <p>/* If user prefers dark, then that&#8217;s what they&#8217;ll get */<br /> @media (prefers-color-scheme: dark) {<br /> .theme-default {<br /> &#8211;light: var(&#8211;OFF);<br /> &#8211;dark: var(&#8211;ON);<br /> }<br /> } </p> <p>We can then set up all of our theme variables in a single declaration, without repetition. In this example, the theme-* classes are set to the html element, so we can use :root as a selector, as many people like to do, but you could set them on the body, if the cascading nature of the custom properties makes more sense that way: </p> <p>:root {<br /> &#8211;text: var(&#8211;light, black) var(&#8211;dark, white);<br /> &#8211;bg: var(&#8211;light, orchid) var(&#8211;dark, rebeccapurple);<br /> } </p> <p>And to use them, we use var() with built-in fallbacks, because we like being careful: </p> <p>body {<br /> color: var(&#8211;text, navy);<br /> background-color: var(&#8211;bg, lightgray);<br /> } </p> <p>Hopefully you’re already starting to see the benefit here. Instead of defining and switching armloads of custom properties, we’re dealing with two and setting all the others just once on :root. That’s a huge improvement from where we started. </p> <p>Even DRYer with pre-processors </p> <p>If you were to show me this following line of code out of context, I’d certainly be confused because a color is a single value, not two! </p> <p>&#8211;text: var(&#8211;light, black) var(&#8211;dark, white); </p> <p>That’s why I prefer to abstract things a bit. We can set up a function with our favorite pre-processor, which is Sass in my case. If we keep our code above defining our &#8211;light and &#8211;dark values in various contexts, we need to make a change only on the actual custom property declaration. Let’s create a light-dark function that returns the CSS syntax for us: </p> <p>@function light-dark($light, $dark) {<br /> @return var(&#8211;light, #{ $light }) var(&#8211;dark, #{ $dark });<br /> } </p> <p>And we’d use it like this: </p> <p>:root {<br /> &#8211;text: #{ light-dark(black, white) };<br /> &#8211;bg: #{ light-dark(orchid, rebeccapurple) };<br /> &#8211;accent: #{ light-dark(#6d386b, #b399cc) };<br /> } </p> <p>CodePen Embed Fallback </p> <p>You’ll notice there are interpolation delimiters #{ … } around the function call. Without these, Sass would output the code as is (like a vanilla CSS function). You can play around with various implementations of this but the syntax complexity is up to your tastes. </p> <p>How’s that for a much DRYer codebase? </p> <p>More than one theme? No problem! </p> <p>You could potentially do this with more than two modes. The more themes you add, the more complex it becomes to manage, but the point is that it is possible! We add another theme set of ON or OFF variables, and set an extra variable in the list of values. </p> <p>.theme-pride {<br /> &#8211;light: var(&#8211;OFF);<br /> &#8211;dark: var(&#8211;OFF);<br /> &#8211;pride: var(&#8211;ON);<br /> } </p> <p>:root {<br /> &#8211;text:<br /> var(&#8211;light, black)<br /> var(&#8211;dark, white)<br /> var(&#8211;pride, #ff8c00)<br /> ; /* Line breaks are absolutely valid */ </p> <p> /* Other variables to declare… */<br /> } </p> <p>Is this hacky? Yes, it absolutely is. Is this a great use case for potential, not-yet-existing CSS booleans? Well, that’s the dream. </p> <p>How about you? Is this something you’ve figured out with a different approach? Share it in the comments! </p> <p>The post A DRY Approach to Color Themes in CSS appeared first on CSS-Tricks.<br /> You can support CSS-Tricks by being an MVP Supporter.</p>

Breakdown

The other day, Florens Verschelde asked about defining dark mode styles for both a class and a media query, without repeat CSS custom properties declarations. I had run into this issue in the past but hadn’t come up with a

...