Sub topic 8
Deriving colors with relative color syntax
This chapter assumes you can write oklch() colors, you know what lightness, chroma and hue mean, and you are comfortable with CSS custom properties. If those are unfamiliar, review the earlier chapters on oklch() syntax first.
The problem: variations without rewriting
Suppose your brand color lives in --brand. A hover state needs to be a little darker. A border needs to be the same color at 40% opacity. Without relative color syntax, you copy the numbers from --brand into a new oklch() call and edit them. When the brand color changes, every copy has to change too.
Relative color syntax solves this. You start from an existing color, read its channels into named variables, and build a new color from them.
How it works
Put the from keyword and a source color right after the opening parenthesis of a color function. The browser converts the source color into that function's color space. Inside the function, each channel becomes a keyword you can use as a value or inside calc().
:root {
--brand: oklch(62% 0.19 255);
}
.button {
background: oklch(from var(--brand) calc(l - 0.1) c h);
border-color: oklch(from var(--brand) l c h / 0.4);
}Read the first declaration from left to right:
from var(--brand)names the source color.calc(l - 0.1) c hgives the new lightness, chroma and hue.l,candhare the channel values of--brand, as converted to OKLCH.
In the second declaration, l c h pass the channels through unchanged. The value after the slash sets the alpha to 0.4.
Channel ranges
The keywords hold numbers, not percentages. In oklch(), l is a number from 0 to 1 (so 62% becomes 0.62), c is roughly 0 to 0.4, and h is an angle in degrees. This is why the example subtracts 0.1 and not 10%.
| Keyword | Meaning | Typical range |
|---|---|---|
| `l` | Lightness | 0 to 1 |
| `c` | Chroma | 0 to about 0.4 |
| `h` | Hue angle | 0 to 360 |
| `alpha` | Opacity | 0 to 1 |
Changing one channel at a time
Because the channels are independent in OKLCH, you can change one without disturbing the others. Here are three common derivations from one token.
:root {
--brand: oklch(62% 0.19 255);
--brand-hover: oklch(from var(--brand) calc(l - 0.08) c h);
--brand-muted: oklch(from var(--brand) l calc(c * 0.4) h);
--brand-accent: oklch(from var(--brand) l c calc(h + 150));
}--brand-hoveris darker, with the same chroma and hue.--brand-mutedkeeps its lightness and hue but loses 60% of its chroma.--brand-accentrotates the hue by 150 degrees to get a contrasting color.
Change --brand once and all three follow.
Using a source in a different color space
The source color does not have to be in OKLCH. You can pass a hex value, a named color, or an rgb() color. The browser converts it for you.
.badge {
background: oklch(from #3b82f6 calc(l + 0.2) calc(c * 0.5) h);
}This is useful when a design system gives you hex values but you want to adjust them perceptually.
Common errors
- Using percentages in
calc().calc(l - 10%)mixes a number with a percentage and is invalid. Usecalc(l - 0.1). - Forgetting the output can leave the gamut. Raising
cor changinglcan produce a color the display cannot show. The browser maps it into range, which may change the look. The gamut chapter covers this. - Assuming the result is accessible. Lowering
lby a fixed amount does not guarantee readable text. Check contrast separately. - Using the keywords in the wrong function. Inside
hsl(from ...)the keywords areh,sandl, with different ranges. Use the keywords that belong to the function you are writing. - Omitting
fromsupport checks. Older browsers drop the whole declaration. Provide a fallback line before it.
.button {
background: #2f6fd6;
background: oklch(from var(--brand) calc(l - 0.1) c h);
}A browser without relative color support ignores the second declaration and keeps the hex fallback.
Relative color or color-mix()?
Both can build variations, and they suit different jobs.
| Need | Better tool |
|---|---|
| Blend two colors | `color-mix()` |
| Tint or shade toward white or black | `color-mix()` |
| Set an exact lightness or alpha | Relative color syntax |
| Rotate hue or scale chroma | Relative color syntax |
Exercise
Write a declaration that sets background to --brand with its chroma halved and its lightness raised by 0.15, keeping hue unchanged. Why does calc(l + 15%) fail?
Show answer
background: oklch(from var(--brand) calc(l + 0.15) calc(c * 0.5) h);. Inside oklch(from ...), the l keyword is a number from 0 to 1, so adding a percentage mixes incompatible types and makes calc() invalid. Use a plain number such as 0.15.
Exercise
A browser lacks relative color support. You write border-color: oklch(from var(--brand) l c h / 0.4); with no earlier line. What border color does the element get?
Show answer
The browser drops the whole declaration as invalid, so border-color falls back to its inherited or initial value (currentcolor). Add an earlier fallback declaration such as border-color: rgb(59 130 246 / 0.4);.