Sub topic 4
Semantic color tokens for light and dark
This chapter assumes you know CSS custom properties, the prefers-color-scheme media query, and the cascade. Here you will organize your colors so that adding a dark theme means changing a small set of values and leaving your component rules alone.
The problem with naming colors by value
Suppose your stylesheet has --white: #ffffff and you use it as a page background. In dark mode you want that background to be near-black. You can't redefine --white as black without making the name misleading. You also can't leave it alone, because the page stays bright.
The fix is to name colors by their job, not their appearance. A name like --bg-page or --text-primary stays true in both themes, and only its value changes.
Two layers of tokens
A design token is a named value that stands for a design decision. Most themes use two layers:
- Primitives are raw palette values, named by what they look like:
--gray-900,--blue-500. They never change between themes. - Semantic tokens are named by role:
--bg-page,--text-primary,--link. Each theme maps these roles to primitives.
Components use only semantic tokens.
Defining the tokens
The light theme is the default. The dark mapping overrides only the semantic layer.
:root {
/* Primitives */
--white: #ffffff;
--gray-100: #f2f3f5;
--gray-300: #c9ccd1;
--gray-800: #1e1f22;
--gray-900: #121212;
--gray-50: #e6e6e6;
--ink: #1a1a1a;
--blue-600: #0b5fff;
--blue-300: #8ab4ff;
/* Semantic tokens: light theme */
--bg-page: var(--white);
--bg-surface: var(--gray-100);
--text-primary: var(--ink);
--border: var(--gray-300);
--link: var(--blue-600);
}
@media (prefers-color-scheme: dark) {
:root {
--bg-page: var(--gray-900);
--bg-surface: var(--gray-800);
--text-primary: var(--gray-50);
--border: #3a3c40;
--link: var(--blue-300);
}
}The var() references resolve on the element where the property is used. Because both blocks target :root, the primitives and the semantic tokens live on the same element and the mapping works.
Using the tokens in components
Component rules mention no theme at all:
body {
background: var(--bg-page);
color: var(--text-primary);
}
.card {
background: var(--bg-surface);
border: 1px solid var(--border);
padding: 1rem;
border-radius: 8px;
}
a {
color: var(--link);
}If you add a new component, you write it once. It follows the theme because every color comes from a token.
Choosing the token set
Keep the set small and role-based. A typical starting point:
| Token | Role |
|---|---|
| `--bg-page` | Page background |
| `--bg-surface` | Cards, panels, inputs |
| `--text-primary` | Body text |
| `--text-muted` | Secondary text |
| `--border` | Dividers and outlines |
| `--link` | Links and accents |
Add a token only when a new role appears. If two elements need different colors for different reasons, give them separate tokens even when their values match today.
Common errors
- Semantic names that describe a color.
--light-bgor--whitebreaks the moment a theme changes its value. - Primitives inside components. A rule like
color: var(--gray-900)stays dark in both themes, so text vanishes on a dark background. - Hard-coded leftovers. A stray
#ffforrgb(0 0 0 / 0.1)in a component ignores the theme. Search your stylesheet for hex values outside the token block. - Partial overrides. If the dark block sets
--bg-pagebut forgets--text-primary, you get dark text on a dark background. Change tokens in pairs and check them together. - Overriding on the wrong element. If you define tokens on
:rootand override them on.card, the override applies only inside.card. Keep both mappings on the same selector unless you want a scoped theme.
Exercise
A teammate defines --white: #fff and uses background: var(--white) on cards. In dark mode they redefine --white: #1e1f22. Give two problems with this approach and a better design.
Show answer
The name --white now holds a dark color, which misleads anyone reading the code. Any element that really needs white, such as a logo plate, also turns dark. A better design keeps --white as a primitive that never changes. It adds a semantic token, --bg-surface, mapped to var(--white) in light and to a dark gray primitive in dark. Cards then use background: var(--bg-surface).