Sub topic 6
Building a palette and hover states from tokens
This chapter assumes you know basic CSS selectors, custom properties (variables), and the oklch() and color-mix() syntax from earlier chapters. You will turn one brand color into a small set of tokens: a base color, a hover state, a tint, and a shade.
Why tokens
A design token is a named value, such as --brand, that the rest of the stylesheet reuses. With hex values, every hover color and tint is a separate hand-picked number. If the brand color changes, you must pick them all again.
With oklch() and color-mix(), you store one base color and compute the rest from it. Changing --brand then updates every variant.
Defining the base token
Start with the brand color and a translucent version of it. The alpha value goes after a slash.
:root {
--brand: oklch(62% 0.19 255);
--brand-translucent: oklch(62% 0.19 255 / 0.5);
}Keep the base in :root so every selector can read it.
Hover states by changing lightness
In OKLCH, lightness is perceptually even. Lowering L by the same amount darkens different hues by about the same visible amount. That is what makes a hover rule predictable.
.button {
background: oklch(60% 0.15 250);
}
.button:hover {
background: oklch(52% 0.15 250);
}Only the lightness changed, from 60% to 52%. Chroma and hue stay the same, so the hover color looks like a darker version of the same blue, not a different blue.
Tints and shades with color-mix()
Writing a new oklch() value for each variant works, but it copies the numbers from the base. color-mix() reads the token instead.
.card {
background: color-mix(in oklch, var(--brand) 15%, white);
}This takes 15% of the brand color and the remaining 85% white, which gives a light tint. A shade works the same way with black:
.card-header {
background: color-mix(in oklch, var(--brand) 80%, black);
}If you give only one percentage, the other color gets the remainder. If the two percentages add up to less than 100%, the result becomes partly transparent, so state them clearly.
Putting the tokens together
Define the variants once as custom properties, then use them by name.
:root {
--brand: oklch(62% 0.19 255);
--brand-hover: color-mix(in oklch, var(--brand), black 15%);
--brand-tint: color-mix(in oklch, var(--brand) 15%, white);
--brand-shade: color-mix(in oklch, var(--brand) 80%, black);
}
.button {
background: var(--brand);
color: white;
}
.button:hover {
background: var(--brand-hover);
}
.card {
background: var(--brand-tint);
border: 1px solid var(--brand);
}
.card h2 {
color: var(--brand-shade);
}Custom properties store the color-mix() expression, and the browser resolves it where the property is used. Because --brand-hover refers to var(--brand), overriding --brand in a smaller scope updates the variants in that scope too. This works for themes:
.theme-green {
--brand: oklch(62% 0.19 150);
}Elements inside .theme-green get a green hover, tint, and shade. Note that custom properties are resolved where they are declared. If you want the variants to follow a scoped --brand, declare them in a selector that applies to that scope, for example :root, .theme-green { ... }.
flowchart TDCommon errors
- Declaring variants only in
:rootand expecting a scoped override to change them. The variant is computed from--brandwhere the variant is declared. Redeclare the variants in the scope that overrides--brand. - Percentages that do not add up.
color-mix(in oklch, var(--brand) 15%, white 15%)gives a mostly transparent color. Give one percentage, or two that total 100%. - Assuming a tint is accessible. A light tint behind brand-colored text can fail contrast. Check it, as covered in a later chapter.
- Pushing chroma too high. Very dark or very light variants may not fit the display gamut, and the browser will adjust them. Gamut limits are covered in the next chapter.
Exercise
You set --brand-hover: color-mix(in oklch, var(--brand), black 15%) in :root. A .theme-green block overrides only --brand, and a button inside it still shows the blue hover. Why, and how do you fix it?
Show answer
Custom properties are resolved where they are declared. --brand-hover was computed in :root using the blue --brand, and the children inherit that computed result. Fix it by declaring the variants in the scope that overrides --brand, for example :root, .theme-green { --brand-hover: color-mix(in oklch, var(--brand), black 15%); }.
Exercise
Write a .badge rule whose background is a 20% mix of --brand with white in OKLCH.
Show answer
.badge { background: color-mix(in oklch, var(--brand) 20%, white); }