Sub topic 10
Defining the palette as CSS custom properties
This chapter assumes you can write CSS selectors and that you have built the HSL colour scales from the earlier chapters. Here you move those colours into a stylesheet as custom properties. You will arrange them in two layers so that components never use raw shades.
What a custom property is
A CSS custom property is a variable that you name yourself. Its name starts with two dashes, such as --primary-500. You read it with var(). Custom properties are inherited, so a value set on :root is available to every element on the page. :root matches the html element and has higher specificity than the html type selector.
Unlike Sass variables, custom properties exist at runtime. The browser resolves them while it renders. This lets you change a value in a media query or on a single element without recompiling anything. The dark mode chapter relies on this.
Layer 1: the palette
The first layer holds the raw colours. Each name describes the colour itself, such as its role and lightness step. It says nothing about where the colour is used.
: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 numbers in the names (100, 500, 900) are lightness steps, where a higher number means a darker shade. Because every step in a family shares one hue, you can add a step such as --primary-300 later without affecting the others.
Layer 2: semantic tokens
The second layer gives each colour a job. A semantic token is a custom property named for its purpose, such as --color-text. It points at a palette step.
: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 use only the semantic names. If you decide that links need more contrast, you change one line, --color-link: var(--primary-700). Every link on the site updates, and no component rule is edited.
Why two layers
Two layers keep each kind of decision in one place.
| Question | Where you answer it |
|---|---|
| What colours does the brand use? | Palette layer |
| Which colour is body text? | Semantic layer |
| How does a button look? | Component rules |
The same token can also be shared. A link, a focus ring and a selected tab can all use --color-link, so they stay consistent.
Fallbacks and mistakes to avoid
var() accepts a fallback as its second argument. The browser uses it when the property is not defined.
.badge {
background: var(--color-highlight, hsl(35 95% 50%));
}Check these common errors when a colour does not appear:
- A misspelt name.
var(--color-txt)is not an error. The declaration becomes invalid at computed-value time, and the property falls back to its inherited or initial value. Check the computed styles in your browser's developer tools. - A component that uses a palette step directly. Writing
color: var(--neutral-900)in a component bypasses the semantic layer, so a theme change will skip it. - A token defined on the wrong element. A property set on
.cardis not visible tobody. Keep global tokens on:root. - Too many tokens. Create a semantic token only when a colour has a job that more than one rule needs. A token used once adds a name to learn without any benefit.
Design tokens in general
A design token is a named design decision that is stored as data. Your CSS custom properties are one format for tokens. Larger teams often keep tokens in a JSON file and generate CSS from it. For a single site, writing the CSS directly is simpler. Keep the two-layer structure either way, so you can move to generated tokens later.
Exercise
A designer asks you to make every link darker. Your palette has --primary-700 and a darker --primary-900. Which single line do you change, and why does this not require editing any component rule?
Show answer
Change --color-link: var(--primary-700); to --color-link: var(--primary-900); in the semantic layer. Every rule that sets a link colour reads var(--color-link), so all of them pick up the new value.
Exercise
Why is --color-danger a better token name than --color-red?
Show answer
It names the purpose of the colour. If the danger colour later changes to orange for contrast or branding reasons, the name remains accurate, but --color-red would then be misleading.