CSS color-mix() and oklch: Modern Color Systems for WordPress Themes

color-mix() and OKLCH: a color system for a WordPress theme from one variable

A theme palette usually needs more colors than the brand has: a lighter tint for backgrounds, a darker shade for hover, a transparent version for borders. Without a preprocessor these ended up as a dozen hard-coded hex values that drift apart when the brand color changes. color-mix() computes them in the browser from one custom property, and the OKLCH color space makes the results look even.

Problem: A theme has 14 hex values derived from one brand blue. When the client changes the brand color, all of them have to be recalculated, and some hover states end up grayish while others are too saturated.

Solution: Define the brand color once in theme.json, derive tints, shades and transparent variants with color-mix( in oklch, ... ) in the stylesheet, and use OKLCH values for the base colors so that lightness steps look the same for every hue.

OKLCH in short

oklch( L C H ) describes a color with lightness (0–100%), chroma (colorfulness, 0 to about 0.37) and hue (0–360 degrees). Unlike HSL, the lightness is perceptual: two colors with the same L look equally light, whatever their hue. In HSL, yellow and blue with “50% lightness” look completely different. That is why tints and shades computed in OKLCH stay consistent.

The palette in theme.json

{
    "$schema": "https://schemas.wp.org/wp/6.7/theme.json",
    "version": 3,
    "settings": {
        "color": {
            "palette": [
                { "slug": "brand", "name": "Brand", "color": "oklch(55% 0.2 255)" },
                { "slug": "base", "name": "Base", "color": "#ffffff" },
                { "slug": "contrast", "name": "Contrast", "color": "oklch(20% 0.02 255)" }
            ]
        }
    }
}

WordPress prints the palette as custom properties: --wp--preset--color--brand and so on. The color picker in the editor shows OKLCH colors correctly in current browsers.

Derived colors with color-mix()

:root {
    --ha-brand: var(--wp--preset--color--brand);
    --ha-brand-50: color-mix(in oklch, var(--ha-brand) 8%, white);
    --ha-brand-100: color-mix(in oklch, var(--ha-brand) 16%, white);
    --ha-brand-700: color-mix(in oklch, var(--ha-brand) 75%, black);
    --ha-brand-a20: color-mix(in oklch, var(--ha-brand) 20%, transparent);
}

.wp-block-button__link {
    background: var(--ha-brand);
}
.wp-block-button__link:hover,
.wp-block-button__link:focus-visible {
    background: var(--ha-brand-700);
}

.ha-notice {
    background: var(--ha-brand-50);
    border: 1px solid var(--ha-brand-a20);
}

color-mix( in oklch, A 8%, white ) means 8% brand color and 92% white, interpolated in OKLCH. Mixing with transparent gives a semi-transparent version of the color, the replacement for rgba() with a hard-coded RGB value. When the brand color changes in the Site Editor (Styles → Colors), all derived colors follow.

The color space matters: in srgb mixes like a paint program and often produces dull, grayish middle colors; in oklch keeps the chroma, so a lighter blue still looks blue.

Contrast is still your job

Derived colors can fail the contrast requirements. White text on --ha-brand needs a contrast ratio of 4.5:1 (WCAG AA for normal text). Check the final colors in the browser’s DevTools (the color picker shows the contrast ratio) or with a contrast checker after every brand change. With OKLCH a rule of thumb works well: backgrounds for white text need a lightness of about 60% or lower.

Fallback for older browsers

The usual fallback pattern, a plain value first and the modern one second, does not help here:

.ha-notice {
    background: #eef4ff;                      /* fallback */
    background: var(--ha-brand-50);
}

Browsers that do not understand color-mix() drop the second declaration only when it is invalid at parse time; with a custom property it is invalid at computed time and the property falls back to its initial value instead. For a robust fallback, use @supports:

@supports not (color: color-mix(in oklch, red, blue)) {
    :root {
        --ha-brand-50: #eef4ff;
        --ha-brand-700: #1e3a8a;
    }
}

NOTE: color-mix() and oklch() work in Chrome and Edge 111, Firefox 113 and Safari 16.2 (all 2023). The next step, relative color syntax like oklch( from var(--ha-brand) calc(l + 10%) c h ), changes one channel of a color directly; it arrived in Safari 18 (September 2024) and is newer than color-mix(), so check the visitors’ browsers before relying on it. Custom properties in themes: CSS custom properties in WordPress themes.