Sub topic 6
Building a palette and hover states from tokens
This chapter combines oklch() and color-mix() into a small token system. You will define a few base values once and derive a full palette, hover states, tints and a dark theme from them. It assumes you know CSS custom properties and the oklch() and color-mix() syntax from the earlier chapters. It also assumes you know that lightness, chroma and hue are separate channels in OKLCH.
What a color token is
A color token is a named value, stored in a custom property, that stands for a design decision. Tokens come in two layers:
- Primitive tokens hold raw values:
--hue,--brand-500,--neutral-900. They describe what colors exist. - Semantic tokens describe how colors are used:
--color-action,--color-surface,--color-text. They point at primitives.
Components use only semantic tokens. To retheme the site, you change what the semantic tokens point to, and the component rules stay as they are.
Step 1: a scale from one hue
In OKLCH, a palette is mostly a list of lightness values with one shared hue. Because lightness is perceptually even, equal steps look like equal steps. The same is not true in hsl(), where a yellow and a blue at 50% lightness look very different in brightness.
Store the hue as its own property so you can change the whole palette in one place. A custom property can sit inside the function:
:root {
--hue: 255;
--brand-50: oklch(96% 0.025 var(--hue));
--brand-100: oklch(92% 0.05 var(--hue));
--brand-300: oklch(80% 0.11 var(--hue));
--brand-500: oklch(62% 0.19 var(--hue));
--brand-600: oklch(54% 0.18 var(--hue));
--brand-700: oklch(46% 0.16 var(--hue));
--brand-900: oklch(30% 0.09 var(--hue));
}Chroma is not constant across this scale. Very light and very dark colors cannot hold much chroma before they leave the display's gamut, the range of colors a screen can show. If you keep chroma at 0.19 for the 96% step, the browser has to clip or map it. The result can shift the hue slightly. Lowering chroma toward both ends, as above, keeps the steps predictable. The gamut chapter covers this in more detail.
To retheme the palette to green, change --hue: 255 to --hue: 150. Check the result, though. Different hues reach their maximum chroma at different lightness values. A chroma that is safe for blue may be out of gamut for green, so a single shared chroma curve is a starting point and not a guarantee.
Step 2: semantic tokens
Now map roles to primitives:
:root {
--color-action: var(--brand-500);
--color-action-hover: var(--brand-600);
--color-surface-tint: var(--brand-50);
--color-border: var(--brand-100);
--color-text-on-action: white;
}
.button {
background: var(--color-action);
color: var(--color-text-on-action);
border: 1px solid transparent;
}
.button:hover {
background: var(--color-action-hover);
}This works and is easy to read. The cost is that every state needs a hand-picked primitive. A button that can be primary, danger or success, each with hover, active and disabled states, needs many tokens. The next section shows how to derive states instead of listing them.
Step 3: hover states by deriving
There are two ways to derive a hover color from a base color.
Option A: change lightness directly
If the base color is built from separate numbers, you can change one of them:
.button {
--l: 60%;
--c: 0.15;
--h: 250;
background: oklch(var(--l) var(--c) var(--h));
}
.button:hover {
--l: 52%;
}Redeclaring --l on :hover changes the lightness and leaves chroma and hue alone. This is the clearest approach when you control the component's inputs. The drawback is that you must store channels separately, and you can no longer pass in a finished color such as var(--brand-500).
Option B: mix with black or white
When you only have a finished color, use color-mix():
.button {
--bg: var(--brand-500);
background: var(--bg);
}
.button:hover {
background: color-mix(in oklch, var(--bg), black 15%);
}
.button:active {
background: color-mix(in oklch, var(--bg), black 25%);
}Here black 15% means 15% black and 85% --bg. In OKLCH, black has lightness 0 and chroma 0. The hue of black is "powerless", meaning it has no meaningful value, so the browser keeps the hue of the other color. The mix therefore lowers lightness and chroma by the same fraction and leaves the hue alone. A color with L = 62% and C = 0.19 mixed with 15% black comes out at roughly L = 52.7% and C = 0.16.
This is why mixing in oklch gives a good hover color. In srgb, mixing toward black can shift the apparent hue, and the result often looks muddy.
Use the same pattern for any base color, which makes variants cheap:
.button--danger { --bg: var(--red-500); }
.button--success { --bg: var(--green-500); }The hover and active rules do not change, and both variants get correct states.
Step 4: tints and shades
A tint is a color mixed toward white. A shade is a color mixed toward black. Both are one-liners:
.card {
background: color-mix(in oklch, var(--brand-500) 8%, white);
border: 1px solid color-mix(in oklch, var(--brand-500) 25%, white);
}
.card__title {
color: color-mix(in oklch, var(--brand-500), black 55%);
}The card background is 8% brand and 92% white, a faint wash of blue. The border is 25% brand. The title is a dark shade of the same hue. All three come from one token, so changing --brand-500 updates them together.
Mixing toward white lowers chroma in proportion, so tints are naturally less saturated and stay inside the gamut. That makes this safer than writing oklch(96% 0.19 255) by hand.
How percentages behave
The percentage rules cause most color-mix() surprises:
| Written as | Meaning |
|---|---|
| `color-mix(in oklch, A 30%, B)` | 30% A, 70% B |
| `color-mix(in oklch, A, B 30%)` | 70% A, 30% B |
| `color-mix(in oklch, A, B)` | 50% each |
| `color-mix(in oklch, A 30%, B 30%)` | The sum is 60%, so the result is 60% opaque |
| `color-mix(in oklch, A 80%, B 80%)` | The sum exceeds 100%, so values are scaled to 50% each |
When you give only one percentage, the other is the remainder. When you give two and they add up to less than 100%, the result gets an alpha equal to the sum. That is useful on purpose and confusing by accident.
A deliberate use is a translucent variant:
.button:disabled {
background: color-mix(in oklch, var(--bg) 40%, transparent);
}Mixing with transparent keeps the color's channels and lowers only the alpha. Mixes with transparent use premultiplied alpha, so the hue does not drift toward the transparent color's black.
Step 5: a dark theme by remapping tokens
Dark themes are a natural fit for lightness-based scales. Redefine the semantic tokens and leave the components alone:
:root {
color-scheme: light dark;
--color-surface: oklch(99% 0.005 var(--hue));
--color-text: oklch(25% 0.03 var(--hue));
--color-action: var(--brand-500);
}
@media (prefers-color-scheme: dark) {
:root {
--color-surface: oklch(18% 0.02 var(--hue));
--color-text: oklch(93% 0.01 var(--hue));
--color-action: var(--brand-300);
}
}
body {
background: var(--color-surface);
color: var(--color-text);
}
.button {
background: var(--color-action);
}
.button:hover {
background: color-mix(in oklch, var(--color-action), white 12%);
}Two details matter. First, the neutrals use the brand hue with very low chroma (0.005 to 0.03). This gives grays that feel related to the brand without looking colored. Second, the hover direction flips. On a light theme a hover darkens the control, and on a dark theme a lighter color reads as "raised". If you want one rule for both themes, put the mix target in a token:
:root {
--hover-mix: black;
}
@media (prefers-color-scheme: dark) {
:root {
--hover-mix: white;
}
}
.button:hover {
background: color-mix(in oklch, var(--color-action), var(--hover-mix) 12%);
}Custom properties can hold any value that is valid where they are used, so var(--hover-mix) works as a color-mix argument.
Common errors
- Mixing in the wrong space by habit.
color-mix(in srgb, ...)is the default people copy from older examples. Specify the space every time, and pickoklchfor lightness-driven results like tints and shades. - Forgetting that
in oklchneeds a hue method for two chromatic colors. Mixing a brand color with black or white is safe because they have no hue. Mixing two real colors, such as red and blue, picks a path around the hue circle. The default is the shorter arc, and the hue chapter explains how to control it. - Assuming lightness guarantees readable text. A shade 55% toward black is dark, but whether white text on a mix passes a contrast ratio depends on the actual result. Generated colors still need a contrast check, covered later in this tutorial.
- Putting the percentage on the wrong color.
color-mix(in oklch, var(--bg), black 15%)darkens a little.color-mix(in oklch, var(--bg) 15%, black)is almost black. Read the percentage as belonging to the color it follows. - Unfallbacked tokens. If a browser does not understand
oklch(), an entire token declaration is dropped, andvar()then yields nothing for that property. Declare a hex fallback in the property that uses the token, or wrap the token block in@supports. The support chapter shows both patterns. - One chroma for every hue. A scale that is in gamut at hue 255 may clip at hue 150. After changing
--hue, look at the lightest and darkest steps.
Choosing between the approaches
| Need | Use |
|---|---|
| A fixed, named scale for designers to pick from | Hand-written `oklch()` steps (Step 1) |
| Hover or active state for any base color | `color-mix()` toward black or white |
| Hover state where you control the channels | Redeclare `--l` on `:hover` |
| Light tint for backgrounds and borders | `color-mix()` toward white at a small percentage |
| Translucent or disabled variant | `color-mix()` with `transparent` |
| Changing one channel while keeping the others | Relative color syntax, covered in a later chapter |
A practical rule: write the scale by hand where a person needs to choose from it, and derive everything else. Hand-written steps are readable and reviewable. Derived values stay in sync when the base changes.
Exercise
A button uses --bg: oklch(62% 0.19 255). You write .button:hover { background: color-mix(in oklch, var(--bg) 20%, black); } and the hover state looks almost black. What went wrong, how do you fix it to get a slightly darker blue, and what lightness and chroma should you roughly expect after the fix?
Show answer
The percentage 20% is attached to var(--bg), so the mix is 20% blue and 80% black. To darken slightly, put the percentage on black: color-mix(in oklch, var(--bg), black 20%), which is 80% blue and 20% black. Black has lightness 0 and chroma 0 and its hue is powerless, so the hue stays 255 while lightness and chroma shrink by about 20%. Expect roughly L = 50% and C = 0.15.
Exercise
You mix color-mix(in oklch, var(--brand-500) 30%, transparent) for a disabled button. What does the result look like compared with --brand-500, and why does the hue not shift?
Show answer
The result has the same color channels as --brand-500 with alpha 0.3. Mixes involving transparent use premultiplied alpha, so the transparent color contributes only opacity and does not pull the hue toward black. Because the percentages given (30%) sum to less than 100% in this case, the leftover share becomes transparency.