Sub topic 9
Reducing repetition with CSS variables
This chapter assumes you know Tailwind utility classes and variants, can set up the dark class strategy from the earlier chapters, and can read basic CSS custom properties. You will move theme colors out of individual dark: classes and into CSS variables, so a single class works in both themes.
The problem with paired classes
With the dark: variant, every colored element needs two classes: one for light and one for dark.
<div class="bg-white text-gray-900 dark:bg-gray-900 dark:text-white">
<p class="text-gray-600 dark:text-gray-300">This card follows the active theme.</p>
</div>This works, but it repeats. If you change the dark background from gray-900 to gray-950, you must find and edit every element that uses it. Pairs are also easy to forget, which leaves some elements stuck in a light style on a dark page.
The idea: define the color once per theme
A CSS custom property (also called a CSS variable) holds a value you can reuse with var(). You define the same variable names in two places. One definition applies in light mode, and another applies when the dark class is present. Elements refer to the name, and the browser picks the value.
:root {
--surface: #ffffff;
--text: #111827;
color-scheme: light;
}
.dark {
--surface: #111827;
--text: #f9fafb;
color-scheme: dark;
}:root matches the <html> element. When the dark class is on <html>, the .dark rule comes later and wins, so --surface and --text change for the whole page. The color-scheme lines are covered in the next chapter.
Using the variables in Tailwind classes
Tailwind accepts arbitrary values in square brackets. Inside them you can write var(--name).
<div class="bg-[var(--surface)] text-[var(--text)]">
<h1 class="text-2xl font-bold">Hello</h1>
</div>Now there is no dark: prefix on the element. The class bg-[var(--surface)] stays the same in both themes, and only the variable's value changes.
How the pieces connect
The toggle script from the earlier chapters does not change. It still adds or removes the dark class. Only the CSS responds differently.
Choosing between variables and `dark:`
Neither approach is always better. Use this table as a guide.
| Situation | Better choice |
|---|---|
| Shared colors used across many components (background, text, borders) | CSS variables |
| One-off tweak on a single element | `dark:` class |
| You want to add a third theme later | CSS variables |
| Different layout or spacing in dark mode | `dark:` class |
You can mix both. A common pattern is to use variables for the base palette and dark: for a few exceptions.
<button class="rounded bg-[var(--surface)] px-3 py-1 text-[var(--text)] hover:bg-gray-200 dark:hover:bg-gray-700">
Save
</button>Here the base colors come from variables. The hover color is still handled with stacked variants, because it is a special case.
Naming variables by role
Name variables for what they do, not for how they look. --surface and --text are good names. --white and --dark-gray are poor names, because the value behind --white is not white in dark mode.
Keep the list short. Four to eight roles, such as surface, text, muted text, border and accent, cover most interfaces.
Common errors
- Variable defined only in
.dark. In light modevar(--surface)is then undefined and the background is transparent. Always define every variable in:rootas well. - Variables defined on
bodywhile the class sits onhtml. The.darkselector must match an element that is an ancestor of the elements using the variables. Define both blocks on:rootand.darkfor thehtmlclass. - Using
--surfacewith themediastrategy. The.darkrule only applies if your setup adds that class. If you rely onprefers-color-scheme, put the dark values inside@media (prefers-color-scheme: dark) { :root { ... } }.
Exercise
You wrote .dark { --surface: #111827; } but forgot to define --surface in :root. What do you see in light mode on an element with bg-[var(--surface)], and how do you fix it?
Show answer
The variable is undefined in light mode, so the background declaration is invalid at computed-value time and the element gets no background color (it appears transparent). Fix it by adding --surface: #ffffff; (your light value) to the :root rule.
Exercise
Your site uses darkMode with the media strategy, with no dark class. How would you define the dark values of --surface and --text?
Show answer
Put them in a media query: @media (prefers-color-scheme: dark) { :root { --surface: #111827; --text: #f9fafb; } }. The .dark class rule is not needed because nothing adds that class.