Traditional web colors used sRGB via HEX (#ff0000), RGB (rgb(255, 0, 0)), or HSL (hsl(0, 100%, 50%)).
However, HSL is perceptually non-uniform: yellow at 50% lightness appears dazzlingly bright to human vision, while blue at 50% lightness appears dark and muddy. Furthermore, standard RGB cannot render the vivid wide-gamut colors supported by modern P3 Retina and OLED displays.
The OKLCH Color Space
OKLCH (Lightness, Chroma, Hue in Oklab space) is the new standard for web color design:
/* oklch(LIGHTNESS CHROMA HUE [/ ALPHA]) */
.button-primary {
background-color: oklch(0.65 0.22 250);
}
- Lightness (
L):0(pure black) to1/100%(pure white). Perceptually uniform: a lightness of 0.7 has identical perceived brightness across all hues. - Chroma (
C): Color saturation/vibrancy.0is pure grayscale; values above0.2access ultra-vivid Display-P3 gamuts. - Hue (
H): Color angle from0to360degrees on the color wheel.
/* Generating an accessible, harmonized color palette in OKLCH:
Because lightness is identical, both buttons share identical perceived contrast! */
.blue-badge { background: oklch(0.7 0.18 250); color: #000; }
.yellow-badge{ background: oklch(0.7 0.18 90); color: #000; }
The color-mix() Function
color-mix() enables mixing two colors in a specified color space directly in CSS without Sass preprocessors:
:root {
--brand: oklch(0.6 0.2 250);
}
/* Generate hover states dynamically by mixing 20% white */
.button:hover {
background-color: color-mix(in oklch, var(--brand), white 20%);
}
/* Generate semi-transparent tinted backgrounds */
.card-highlight {
background-color: color-mix(in oklch, var(--brand) 12%, transparent);
}
The light-dark() CSS Function
Modern browsers support light-dark(), which automatically toggles between two colors based on the current color scheme without media query nesting:
:root {
color-scheme: light dark; /* Mandatory: declares support for both modes */
}
.card {
/* Automatically resolves to white in light mode, dark gray in dark mode! */
background-color: light-dark(#ffffff, #1e293b);
color: light-dark(#0f172a, #f8fafc);
}
Exercises
- Convert a legacy HEX color palette to OKLCH and verify perceptual uniformity across hues.
- Use
color-mix()to create disabled, hover, and active states from a single brand token. - Implement the
light-dark()function to build a 2-line themeable card component.