CSS relative color syntax: a theme palette from one color token
color-mix() blends two colors. Relative color syntax goes one step further: it takes a color apart into its channels and lets you change each of them with calc(). “The brand color, 15% lighter”, “the brand color with half the chroma”, “the complementary hue” become one line of CSS each, computed from the color that the site owner picks in the Site Editor.
Problem: A block theme lets the client change the primary color in Styles. Hover states, light backgrounds and borders are hard-coded hex values derived from the old color, so after every change the theme looks inconsistent.
Solution: Derive every related color from var(--wp--preset--color--primary) with relative colors in OKLCH, for example oklch( from var(--ha-primary) calc(l + .15) c h ), clamp the lightness so text contrast stays acceptable, and keep color-mix() or static values as fallback.
The syntax
:root {
--ha-primary: var(--wp--preset--color--primary);
--ha-primary-hover: oklch(from var(--ha-primary) calc(l - .08) c h);
--ha-primary-soft: oklch(from var(--ha-primary) .95 calc(c * .25) h);
--ha-primary-muted: oklch(from var(--ha-primary) l calc(c * .5) h);
--ha-primary-a15: oklch(from var(--ha-primary) l c h / .15);
--ha-accent: oklch(from var(--ha-primary) l c calc(h + 180));
}
After from comes the origin color, in any format: hex, rgb(), a custom property. Inside the function, l, c and h are the channels of that color converted to OKLCH (lightness 0–1, chroma, hue in degrees), and each position takes either the channel as it is or a calc() expression. The alpha comes after the slash.
- hover: the same color, a bit darker.
- soft: a fixed, very light lightness (0.95) with little chroma, for backgrounds of notices and cards.
- a15: the same color at 15% opacity, for borders and focus rings.
- accent: the opposite hue at the same lightness and chroma.
Use it in the theme
.wp-element-button,
.wp-block-button__link {
background: var(--ha-primary);
}
.wp-element-button:hover,
.wp-block-button__link:hover {
background: var(--ha-primary-hover);
}
.ha-notice {
background: var(--ha-primary-soft);
border: 1px solid var(--ha-primary-a15);
}
:focus-visible {
outline: 3px solid var(--ha-primary-a15);
outline-offset: 2px;
}
When the client changes the primary color in Styles, WordPress updates --wp--preset--color--primary, and every derived color follows.
Keep text readable
A derived color can end up too light for white text. Fixing the lightness instead of adding to it keeps the result predictable:
.ha-badge {
background: oklch(from var(--ha-primary) min(l, .55) c h);
color: #fff;
}
min( l, .55 ) keeps the background at most at 55% lightness, which in OKLCH usually means a contrast with white of 4.5:1 or more. Still check the result with a contrast checker for the palette the client actually uses.
relative colors or color-mix()
color-mix( in oklch, var(--ha-primary) 20%, white ) mixes toward another color; it is simpler for tints and shades and has wider support. Relative colors are the tool when a single channel should change, or be set to a fixed value, independently of the others. The same palette with color-mix(): color-mix() and OKLCH.
Fallback
@supports not (color: oklch(from red l c h)) {
:root {
--ha-primary-hover: color-mix(in srgb, var(--ha-primary) 85%, black);
--ha-primary-soft: color-mix(in srgb, var(--ha-primary) 10%, white);
--ha-primary-a15: color-mix(in srgb, var(--ha-primary) 15%, transparent);
}
}
NOTE: Relative color syntax works in Chrome and Edge 119 (2023), Firefox 128 (July 2024) and Safari 18 (September 2024); the earlier Safari 16.4 implementation had channel values with other units. Older Chrome versions 111–118 supported it only partly. With the @supports fallback above, all of them get a usable palette.