Sub topic 10
Defining the palette as CSS custom properties
This chapter assumes you can write basic HTML and CSS, read hex, RGB and HSL colour values, and edit a stylesheet and reload it in a browser. It also assumes you already have palette scales for primary, neutral and accent colours, and that you know how to check contrast against WCAG. Here you turn those scales into code that a whole site can use and that you can change in one place.
Why custom properties suit a palette
A custom property is a CSS variable whose name starts with two dashes, such as --primary-500. You read it with the var() function. Unlike a Sass variable, a custom property exists in the browser at runtime. It follows the cascade and inheritance, and it can be reassigned inside a media query or on a single element.
That runtime behaviour matters for palettes. A single declaration can change every colour that depends on it, and a media query can swap values for dark mode without touching any component.
Define palette variables on :root, which matches the <html> element. Every element inherits from it, so the variables are available everywhere.
Layer 1: the raw palette
The first layer holds the actual colour values. Each name says what the colour is, not where it is used. A hue name plus a step number works well.
:root {
/* Primary: one hue, varied lightness */
--primary-100: hsl(220 80% 94%);
--primary-500: hsl(220 80% 50%);
--primary-700: hsl(220 80% 34%);
/* Neutral: low saturation, slightly tinted toward the primary hue */
--neutral-50: hsl(220 15% 97%);
--neutral-300: hsl(220 12% 80%);
--neutral-700: hsl(220 15% 28%);
--neutral-900: hsl(220 20% 12%);
/* Accent: a different hue, used sparingly */
--accent-500: hsl(35 95% 50%);
}The palette is written in HSL, so each primary step keeps hue 220 and only its lightness changes. The neutrals share that hue at low saturation, which makes greys feel related to the brand colour. The accent uses a different hue (35, an orange) so it stands out.
The step numbers (50 to 900) follow a common convention: a low number is light and a high number is dark. The numbers are only labels, and they do not have to be evenly spaced. Leave gaps for steps you may add later. A real project usually has more steps than this example, such as 100 to 900 for each colour.
Naming rules for the raw layer
- Use the colour's role family (
primary,neutral,accent) rather than a word likeblue. If the brand changes from blue to green,--primary-500is still correct and--blue-500is not. - Use numbers for steps, not words like
lightanddark. Words run out as soon as you add a third shade. - Keep the same step numbers across families, so
500always means the mid-point.
Layer 2: the semantic layer
If components use var(--primary-500) directly, you hit two problems. First, a developer has to remember which shade is right for a link and which for a button. Second, dark mode would have to rewrite every component.
The fix is a second layer of names that describe purpose. These point at the raw palette.
:root {
--color-bg: var(--neutral-50);
--color-text: var(--neutral-900);
--color-border: var(--neutral-300);
--color-link: var(--primary-700);
--color-button-bg: var(--primary-500);
--color-highlight: var(--accent-500);
}
body {
background: var(--color-bg);
color: var(--color-text);
}
a {
color: var(--color-link);
}
.card {
border: 1px solid var(--color-border);
}Components only use names beginning with --color-. They never mention --primary-500 or --neutral-900. To change the link colour across the site, you edit one line in the semantic layer.
These names are called design tokens: named values that store a design decision. The two-layer pattern is the common core of most token systems. Larger systems add a third layer for component tokens such as --button-bg, but two layers are enough for most websites.
Choosing semantic names
Name by job, not by appearance. --color-text is a good name because its value can change. --color-dark-grey is a bad name, because in dark mode the value is light.
Useful starting names:
| Token | Purpose |
|---|---|
| `--color-bg` | Page background |
| `--color-surface` | Cards, panels, menus raised above the page |
| `--color-text` | Body text |
| `--color-text-muted` | Secondary text, captions |
| `--color-border` | Dividers and outlines |
| `--color-link` | Inline links |
| `--color-button-bg` | Primary button background |
| `--color-highlight` | Accent emphasis |
| `--color-success`, `--color-warning`, `--color-error` | Status messages |
Keep the list short. Each token is a decision somebody must remember, so add one only when two places need the same colour for the same reason.
Contrast belongs in the token choice
A token is only as accessible as the pair it creates. --color-text on --color-bg must meet the WCAG ratio for body text, which is 4.5:1. Large text and UI components need 3:1. Check the pairs, not the individual colours.
This function computes the WCAG 2 contrast ratio from two RGB colours.
function channel(value) {
const s = value / 255;
return s <= 0.04045 ? s / 12.92 : Math.pow((s + 0.055) / 1.055, 2.4);
}
function luminance([r, g, b]) {
return 0.2126 * channel(r) + 0.7152 * channel(g) + 0.0722 * channel(b);
}
function contrastRatio(colorA, colorB) {
const a = luminance(colorA);
const b = luminance(colorB);
const lighter = Math.max(a, b);
const darker = Math.min(a, b);
return (lighter + 0.05) / (darker + 0.05);
}
console.log(contrastRatio([0, 0, 0], [255, 255, 255])); // 21Black on white gives 21, the maximum. Real pairs will be lower. Run each text and background pair you define in the semantic layer through a function like this, and compare the result with 4.5 or 3.
Keep a list of which tokens are meant to be used together, for example --color-text on --color-bg, and --color-text on --color-surface. If you later change a raw colour, you re-check only the listed pairs.
Layer 3 in practice: dark mode by reassigning tokens
Because components use only semantic tokens, a dark theme needs only new values for those tokens.
@media (prefers-color-scheme: dark) {
:root {
--color-bg: var(--neutral-900);
--color-text: var(--neutral-50);
--color-border: var(--neutral-700);
--color-link: var(--primary-100);
}
}The prefers-color-scheme media feature matches when the user's operating system or browser is set to a dark theme. Inside it, you reassign four semantic tokens. The raw palette is unchanged, and no component rules change.
Notice that --color-button-bg and --color-highlight are not reassigned. They keep the light-theme values. That is only safe if they still pass contrast on the dark background, so you must check them. If they fail, add them to the dark block.
The dark block is a full second set of decisions, not an automatic inversion. Swapping neutral-50 with neutral-900 works for text and background, but links moved from step 700 to step 100 because 700 is too dark on a dark background.
How resolution works
var() is resolved when the browser computes the style of each element. This has consequences you will meet in practice.
- Inheritance. A custom property set on
:rootis inherited by every element. You can override it on any element to re-theme a section. - Late binding.
--color-link: var(--primary-700)stores a reference. When--primary-700changes on an element, the semantic token follows it only if both are resolved on that same element or the change is made at an ancestor and the dependent token is declared there too. - Fallbacks.
var(--color-link, blue)usesblueif--color-linkis not defined. A fallback guards against typos, but it can also hide them. Use fallbacks sparingly in a palette.
A section-level override looks like this:
.promo {
--color-bg: var(--primary-700);
--color-text: var(--neutral-50);
--color-link: var(--primary-100);
}
.promo {
background: var(--color-bg);
color: var(--color-text);
}Anything inside .promo that uses semantic tokens now follows the promo colours automatically. The same rule that sets the custom properties also reads them, which is valid because the element's own values are used.
Common errors
- Using a raw token in a component.
border-color: var(--neutral-300)works today, but dark mode will not change it. Usevar(--color-border). - Misspelling a token name. An undefined variable makes the declaration invalid at computed-value time. The property falls back to its inherited or initial value, often with no warning. Search the stylesheet for each new name.
- Defining tokens in the wrong place. Variables set on
.cardare not available outside it. Put shared tokens on:root. - Naming tokens after colours.
--color-bluebecomes wrong as soon as the brand changes. - Forgetting the dark pair. You reassign
--color-bgbut not--color-text, and the result is dark text on a dark page. - Assuming a token passes contrast in both themes. Contrast is a property of a pair in a theme. Check it again after each theme change.
- Splitting HSL into parts carelessly. Some palettes store
--primary-h: 220and build colours withhsl(var(--primary-h) 80% 50%). This lets you change a hue in one place, but it makes values harder to read in developer tools. Use it only if you want to switch brand hues often.
A short workflow
- Write the raw palette on
:root, with a family name and a step number for each colour. - Write the semantic tokens, naming each by its job.
- Replace every colour in your component CSS with a semantic token.
- Check each text and background pair against 4.5:1 (or 3:1 for large text and UI).
- Add the
prefers-color-scheme: darkblock and reassign the tokens that need it. - Re-check contrast for the dark pairs.
Exercise
A stylesheet has .alert { border: 1px solid var(--neutral-300); }. Dark mode reassigns --color-border but the alert border stays light grey on a dark background. What is the cause and the fix?
Show answer
The component uses a raw palette token, so the dark-mode block, which only reassigns semantic tokens, never reaches it. Change the rule to border: 1px solid var(--color-border); so the border follows the theme.
Exercise
You add --color-surface: var(--neutral-50); for cards. After enabling the dark block from this chapter, cards are bright white on a dark page. What did you forget, and what pair must you check afterwards?
Show answer
You did not reassign --color-surface inside the prefers-color-scheme: dark block, for example to a dark neutral step. Afterwards, check --color-text on --color-surface for at least 4.5:1, since text now sits on the new surface colour.
Exercise
Why is --color-link a better token name than --color-blue-700?
Show answer
It names the job, so its value can change per theme or brand without the name becoming wrong. A name containing a colour and a step describes a fixed value, which becomes misleading when the value changes.