Bea’s palette is finished, with a green scale, mixed tints, and hover and muted shades derived from her brand colors. In the exercise, you mixed a whole seed packet palette from one base color. But every token holds a single light value. Bea checks the catalogue on her phone in the evening, with her system set to dark, and the page still glows pale green. Plenty of the neighbors who browse for seeds after work have the same setting.
In this lesson, you’ll give each color role a dark value next to its light one, with the light-dark() function, and turn on the dark theme for visitors whose system is set to dark. You’ll be able to predict which of the two values an element gets and why. Start with the roles themselves.
Give Each Role a Dark Value
A dark theme needs a second value for each role that changes, so where does it go? The usual answer is a media query. The prefers-color-scheme media feature tells you whether the visitor’s system is set to light or dark, and the dark theme redefines the roles inside it:
@media (prefers-color-scheme: dark) {
:root {
--color-surface: var(--green-15);
--color-text: var(--green-98);
}
}
That works, but each role now lives in two places, and a role you add later can lose its dark half without anyone noticing. The light-dark() function keeps both halves in one declaration: light-dark(<light color>, <dark color>). It gives the first color in a light theme and the second in a dark one.
light-dark() goes in the roles tier. Primitives stay plain values, because --green-15 is a dark green in either theme. Components keep reading roles, so they need no light-dark() of their own. Each role picks two primitives from the scale you built.
The dark theme needs one new primitive, a lighter terracotta, so that the accent stands out on a dark surface. Add it under --terracotta-60 in :root:
--terracotta-70: oklch(70% 0.13 40);
Then replace five of the roles in the semantic tokens with these:
--color-surface: light-dark(var(--green-98), var(--green-15));
--color-text: light-dark(var(--green-20), var(--green-98));
--color-accent: light-dark(var(--terracotta-60), var(--terracotta-70));
--color-border: light-dark(var(--green-50), var(--green-60));
--color-surface-highlight: light-dark(var(--green-tint), var(--green-shade));
The dark surface is the darkest step of the scale, and the text is the lightest. The “Returns week” banner gets the mixed green shade instead of its pale tint.
Two roles stay as they are. The focus ring’s sunflower yellow and the see-through card border can keep one value for now. Later in the course, you’ll measure how well they work against both surfaces. Leave --color-accent-hover and --color-text-muted alone too.
Watch out, because light-dark() can’t switch any value you like. This lesson uses its color form. A length such as border-width: light-dark(1px, 2px) is invalid, so the browser drops the declaration.