Sub topic 2
Picking the Interpolation Color Space
Assume you know basic CSS selectors, custom properties, and hex, rgb(), and hsl() notation. This chapter uses color-mix() to create palette variants and explains how to handle colors that screens cannot display exactly.
Choosing a color space for mixing
color-mix() blends two colors in an interpolation color space. The space controls the path between the colors.
For interface colors, oklch is often a useful default. Its lightness changes more predictably than mixing in srgb, which can produce dull or unexpectedly dark middle colors.
.swatch {
background: color-mix(in oklch, #2563eb 70%, white);
}
.swatch-srgb {
background: color-mix(in srgb, #2563eb 70%, white);
}The percentages describe each color's contribution. If one percentage is omitted, the remaining percentage belongs to that color. You can also mix with transparent, black, or white.
Building a palette from tokens
A design token is a named value stored for reuse. Keep the base color in a custom property, then derive related colors from it.
:root {
--brand: oklch(62% 0.19 255);
--brand-hover: oklch(from var(--brand) calc(l - 0.08) c h);
--brand-tint: color-mix(in oklch, var(--brand) 15%, white);
}
.button {
background: var(--brand);
color: white;
}
.button:hover {
background: var(--brand-hover);
}
.card {
background: var(--brand-tint);
}This keeps hover and tint relationships consistent when you change --brand. A darker lightness value usually makes a useful hover state, but always verify text contrast.
Gamut limits and fallbacks
A color's gamut is the range of colors a device or color space can display. An oklch() color with high chroma may fall outside the sRGB gamut used by some displays. Browsers then map it to a displayable color, which can reduce saturation or change its appearance.
Provide a conventional fallback before the wider-gamut declaration. Older browsers ignore the declaration they do not understand.
.button {
background: #2563eb;
background: oklch(62% 0.19 255);
}
@supports (color: color-mix(in oklch, red, blue)) {
.button:hover {
background: color-mix(in oklch, var(--brand), black 15%);
}
}Exercise
Why might color-mix(in oklch, blue, white) be preferable to an equivalent srgb mix for a tint?
Show answer
OKLCH usually creates a more predictable lightness progression, while an sRGB mix can produce a less even visual result.