Sub topic 4
Semantic color tokens for light and dark
A dark theme is not a second stylesheet. It is a second set of values for the same set of decisions. This chapter shows how to organize colors as semantic tokens so that switching themes means changing a few variable values and no component rules.
This chapter assumes you know CSS custom properties and the prefers-color-scheme media query from the earlier chapters. It also assumes you are comfortable with the cascade and with hex colors.
The problem with naming colors by appearance
The first attempt at theming is often a list of variables named after what the color looks like:
:root {
--white: #ffffff;
--black: #1a1a1a;
--blue: #0b5fff;
}
.card {
background: var(--white);
color: var(--black);
}This breaks in dark mode. If you redefine --white as a dark gray, the name lies. Every reader of .card sees "white" and has to know it is sometimes dark. If you leave the variables alone and override .card in a media query, you copy every component rule into a dark version. That copy is the maintenance cost semantic tokens remove.
A semantic token is named for its job in the interface, not its color. --surface means "the background of a card or panel". --text-muted means "secondary text". The job stays the same in both themes while the color changes.
Two layers: primitives and semantic tokens
Most design systems use two layers.
- Primitive tokens are the raw palette. They are named by hue and step, such as
--gray-900or--blue-600. They never change between themes. - Semantic tokens are named by role, such as
--surfaceor--accent. Each one points at a primitive. These are the only tokens that components use, and they are the only ones a theme overrides.
:root {
/* Layer 1: primitives. Same in every theme. */
--gray-0: #ffffff;
--gray-50: #f5f6f8;
--gray-200: #d9dce3;
--gray-300: #b9bec9;
--gray-600: #5a6070;
--gray-700: #3a3f4b;
--gray-800: #23262e;
--gray-900: #16181d;
--gray-950: #0e0f12;
--blue-300: #8ab4ff;
--blue-600: #0b5fff;
/* Layer 2: semantic tokens, light values. */
--bg: var(--gray-0);
--surface: var(--gray-50);
--border: var(--gray-200);
--text: var(--gray-900);
--text-muted: var(--gray-600);
--accent: var(--blue-600);
--on-accent: var(--gray-0);
}
@media (prefers-color-scheme: dark) {
:root {
--bg: var(--gray-950);
--surface: var(--gray-900);
--border: var(--gray-700);
--text: var(--gray-50);
--text-muted: var(--gray-300);
--accent: var(--blue-300);
--on-accent: var(--gray-950);
}
}The dark block lists the same seven names with different targets. If you add a token to the light block and forget the dark block, the light value silently shows up in dark mode. Keep the two lists side by side, and check that they match.
Components use only semantic tokens
With the tokens in place, component rules contain no theme logic at all:
body {
background: var(--bg);
color: var(--text);
}
.card {
background: var(--surface);
border: 1px solid var(--border);
border-radius: 8px;
padding: 1rem;
}
.card p.meta {
color: var(--text-muted);
}
.button {
background: var(--accent);
color: var(--on-accent);
border: 0;
padding: 0.5rem 1rem;
border-radius: 6px;
}A useful audit is to search your stylesheet for # and rgb(. Any hit outside the token definitions is a color that will not follow the theme.
Choosing token names
Good names describe a role and stay valid when the color changes. A workable starting vocabulary:
| Token | Role | Notes |
|---|---|---|
| `--bg` | Page background | The lowest layer |
| `--surface` | Cards, panels, inputs | Sits on top of `--bg` |
| `--border` | Dividers and outlines | Often needs more care in dark mode |
| `--text` | Primary text | Must meet contrast against `--bg` and `--surface` |
| `--text-muted` | Secondary text | Still needs readable contrast |
| `--accent` | Links, primary buttons | Often a lighter shade in dark mode |
| `--on-accent` | Text placed on `--accent` | Pairs with `--accent` and is not always white |
| `--danger` | Errors and destructive actions | Same idea, one token per status |
Two naming rules prevent most trouble.
- Name the role, not the value. Avoid
--light-gray-bgor--dark-text. In dark mode they would describe the opposite of what they hold. - Create "on" tokens for paired colors. The text on a button depends on the button's background, so
--on-accenttravels with--accent. When you change one, you review the other.
Resist making a token for every component, such as --card-title-color. That creates dozens of values to maintain in two themes. Add a component token only when a component needs a value that no existing role covers.
Why not simply invert everything
A common shortcut is filter: invert(1) hue-rotate(180deg) on the page. It is quick, but it flips images and brand colors too, and you cannot tune contrast. Semantic tokens let you make each decision on purpose.
Dark palettes are not mirror images of light ones. In a light theme, higher layers are usually white and the page behind is slightly gray. In a dark theme, higher layers are usually lighter than the page behind them, because a shadow is hard to see on a dark background. In the example, --bg is --gray-950 and --surface is the lighter --gray-900. The relationship between the layers is the design decision, and tokens are where you record it.
How it works internally
Custom properties are resolved at computed-value time, and they inherit. That has three consequences you will meet in practice.
First, the media query decides which declaration of --surface wins on :root. Elements below inherit that value. The var(--surface) inside .card is then substituted when the browser computes the style. Switching the system theme re-evaluates the media query, and the page updates live without a reload.
Second, var() cannot appear inside a media query condition. This is invalid:
@media (min-width: var(--breakpoint)) {
/* Never matches. Custom properties are not allowed here. */
}The theme switch must be a plain @media (prefers-color-scheme: dark) block, with var() used only in declaration values.
Third, because tokens inherit, you can override them on any element to make a region with a different look. A banner that is always dark, in both themes, only needs local token values:
.banner-dark {
--surface: var(--gray-900);
--text: var(--gray-50);
--border: var(--gray-700);
background: var(--surface);
color: var(--text);
border: 1px solid var(--border);
padding: 1rem;
}Everything inside .banner-dark that uses var(--text) now gets the light text. No child rule needed to change.
Common errors
- Defining tokens on
bodyand reading them on:rootorhtml. Custom properties inherit downward only. A rule onhtmlcannot see a variable defined onbody. Define global tokens on:root. - Misspelled token names. If
var(--surfce)has no definition and no fallback, the declaration becomes invalid at computed-value time. For an inherited property such ascolorit falls back to the inherited value. For a non-inherited property such asbackgroundit falls back to the initial value, which is transparent. The page often looks fine in one theme and broken in the other. A fallback such asvar(--surface, #f5f6f8)hides the typo instead of fixing it, so use fallbacks sparingly. - Overriding primitives instead of semantic tokens. If the dark block changes
--gray-900, then every use of that primitive changes, including places that wanted it to stay dark. Override only the semantic layer. - Forgetting translucent colors. A token like
--overlay: rgba(0, 0, 0, 0.4)is fine on a light page and nearly invisible on a dark one. Give it a dark value too. - Leaving hard-coded colors in SVG or inline styles. An inline
fill="#000"will not follow your tokens. Usefill="currentColor"or afill: var(--text)rule.
Putting it together
This page uses every idea from the chapter. It has no color literals outside the token layers, and it follows the system setting.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Token demo</title>
<style>
:root {
--gray-0: #ffffff;
--gray-50: #f5f6f8;
--gray-200: #d9dce3;
--gray-300: #b9bec9;
--gray-600: #5a6070;
--gray-700: #3a3f4b;
--gray-900: #16181d;
--gray-950: #0e0f12;
--blue-300: #8ab4ff;
--blue-600: #0b5fff;
--bg: var(--gray-0);
--surface: var(--gray-50);
--border: var(--gray-200);
--text: var(--gray-900);
--text-muted: var(--gray-600);
--accent: var(--blue-600);
--on-accent: var(--gray-0);
}
@media (prefers-color-scheme: dark) {
:root {
--bg: var(--gray-950);
--surface: var(--gray-900);
--border: var(--gray-700);
--text: var(--gray-50);
--text-muted: var(--gray-300);
--accent: var(--blue-300);
--on-accent: var(--gray-950);
}
}
body {
background: var(--bg);
color: var(--text);
font: 1rem/1.5 system-ui, sans-serif;
margin: 2rem;
}
.card {
background: var(--surface);
border: 1px solid var(--border);
border-radius: 8px;
padding: 1rem;
max-width: 24rem;
}
.meta {
color: var(--text-muted);
}
.button {
background: var(--accent);
color: var(--on-accent);
border: 0;
border-radius: 6px;
padding: 0.5rem 1rem;
font: inherit;
}
</style>
</head>
<body>
<div class="card">
<h1>Release notes</h1>
<p class="meta">Updated today</p>
<button class="button">Read more</button>
</div>
</body>
</html>Open the file, then change your operating system between light and dark. The card, border, muted text, and button all change together, and the component rules never mention a theme.
Later chapters add to this foundation. They cover telling the browser about your themes so built-in controls match, writing both values in one declaration, and adding a manual toggle that overrides the system setting. All of them work by changing which values the semantic tokens hold, so the structure from this chapter stays the same.
Exercise
A teammate defines --white: #ffffff and --black: #1a1a1a and styles .card with background: var(--white). For dark mode they add a media query that redefines --white as #121212. Give two problems with this approach and say how you would restructure it.
Show answer
First, the name --white now holds a dark color in dark mode, so the code is misleading and every reader must know the override exists. Second, every other use of --white changes too, including places that wanted real white, such as text on a colored button. To restructure, keep primitives such as --gray-0 and --gray-950 fixed, add semantic tokens such as --surface and --text that point to primitives, override only the semantic tokens in the dark @media (prefers-color-scheme: dark) block, and make .card use var(--surface).
Exercise
Your stylesheet has --accent and a button with color: #ffffff hard-coded. In dark mode --accent becomes a light blue, #8ab4ff, and the button text becomes hard to read. Which token fixes this, and how should it be defined in each theme?
Show answer
Add an --on-accent token and use color: var(--on-accent) on the button. In the light theme, point it to white (--gray-0) so it reads on the dark blue. In the dark theme, point it to a very dark color (--gray-950) so it reads on the light blue. The paired token follows the background it sits on.
Exercise
You write @media (prefers-color-scheme: dark) { :root { --text: var(--gray-50); } }, but the page text stays dark in dark mode. A rule body { color: #1a1a1a; } exists elsewhere. What is wrong, and how do you find other places with the same problem?
Show answer
The body rule uses a hard-coded color instead of var(--text), so the token override has no effect on it. Replace it with color: var(--text). To find similar cases, search the stylesheet for hex values and rgb( calls outside the token definitions, and replace each with the semantic token for its role.