Sub topic 5
Picking the interpolation color space
This chapter assumes you know basic CSS syntax, custom properties, and the earlier chapters on oklch() and color-mix() percentages. You should be comfortable writing color-mix(in oklch, red, blue) and reading what it does. Here you will learn how the in <space> part changes the result, and how to choose it on purpose.
What the interpolation space does
When color-mix() blends two colors, it does not average the text you wrote. It does these steps:
- Convert both colors into the interpolation space you named after
in. - Mix each channel of that space by the given percentages.
- Convert the result back to a color the browser can paint.
The same two input colors therefore give different outputs in different spaces, because the channels mean different things. Averaging red, green and blue values is a different operation from averaging lightness, chroma and hue.
The in keyword is required. color-mix(red, blue) is invalid, and the browser drops the declaration. There is no default space to fall back on.
Rectangular and polar spaces
Color spaces fall into two families, and the family decides which extra rules apply.
- Rectangular spaces describe a color with three independent numeric axes, such as red, green and blue. Mixing is a straight line between two points.
- Polar spaces use lightness, chroma and an angle for hue. The hue is a position on a circle, so mixing needs a rule for which way around the circle to go.
| Space | Family | Notes for mixing |
|---|---|---|
| `srgb` | Rectangular | Matches how older CSS blends. Gamma-encoded values, so midpoints look dark and muddy. |
| `srgb-linear` | Rectangular | Mixes actual light intensity. Midpoints look brighter than in `srgb`. |
| `display-p3` | Rectangular | Same behavior as `srgb`, with a wider range of colors. |
| `lab` | Rectangular | Perceptual, but known to shift blues toward purple. |
| `oklab` | Rectangular | Perceptual, with good hue stability. A strong default. |
| `hsl`, `hwb` | Polar | Hue circle, but lightness is not perceptual. |
| `lch` | Polar | Perceptual lightness, same blue problem as `lab`. |
| `oklch` | Polar | Perceptual and keeps chroma. Needs a hue method. |
You do not need all of these. In practice the choice is usually between srgb, oklab and oklch.
The same mix in three spaces
Here is one pair of colors mixed three ways.
.swatch-srgb {
background: color-mix(in srgb, #1d4ed8, #facc15);
}
.swatch-oklab {
background: color-mix(in oklab, #1d4ed8, #facc15);
}
.swatch-oklch {
background: color-mix(in oklch, #1d4ed8, #facc15);
}The colors are a deep blue and a yellow, which sit on nearly opposite sides of the hue circle.
- In
srgbthe midpoint is the channel-wise average. Blue and yellow cancel, so the result is a dull gray-brown. - In
oklabthe result is also a low-chroma color. The mix passes close to the neutral center of the color space, because a and b are averaged as a straight line. This is correct for a rectangular space, and it is often the look you want for a calm blend. - In
oklchchroma is averaged separately from hue. Both inputs are strongly colored, so the midpoint stays strongly colored. Its hue lies on the arc between blue and yellow, so you get a vivid green or cyan-leaning color instead of gray.
Neither answer is wrong. They answer different questions. oklab asks what you get by blending the colors as if mixing paint. oklch asks what color sits between these two hues at average vividness.
Mixing with white, black and gray
Most real uses of color-mix() are not blue with yellow. They blend a brand color with white or black to get tints and shades, as in the earlier .card example. For this case the space matters in a specific way.
:root {
--brand: oklch(62% 0.19 255);
}
.tint-oklab {
background: color-mix(in oklab, var(--brand) 20%, white);
}
.tint-oklch {
background: color-mix(in oklch, var(--brand) 20%, white);
}
.tint-srgb {
background: color-mix(in srgb, var(--brand) 20%, white);
}
.shade-oklab {
background: color-mix(in oklab, var(--brand), black 25%);
}Mixing toward a neutral in oklab moves the a and b values in a straight line toward zero. That scales the color's chroma down and leaves its hue angle unchanged. The tint looks like a paler version of the same color, which is what a designer expects.
srgb also produces a plausible tint, but its lightness steps are uneven. Equal percentage steps do not look like equal steps to the eye, and some hues drift slightly.
oklch works well here too, with one detail covered in the next section.
Achromatic colors and missing hues
White, black and pure grays have no real hue. In a polar space the hue of an achromatic color is treated as missing (powerless). When one input has a missing hue, the mix uses the other color's hue instead of inventing one.
That is why color-mix(in oklch, var(--brand) 20%, white) keeps the brand hue. The white contributes lightness and almost no chroma, and the hue stays at the brand value. This is the behavior you want.
The result can surprise you when an input is almost gray but not exactly gray. A color like oklch(60% 0.01 30) has a tiny chroma but a real hue of 30. In a polar space that hue takes part in the mix, so a near-gray can pull a tint toward orange. In a rectangular space like oklab the tiny chroma barely matters, because the color sits close to the center. If your inputs come from user data or generated tokens, oklab is the safer choice for mixing with neutrals.
Mixing with transparent
Mixing toward transparent is a common way to fade a color. CSS interpolates alpha along with the other channels and uses premultiplied color values. That keeps transparent, which is rgb(0 0 0 / 0), from darkening the result.
.overlay {
background: color-mix(in oklab, var(--brand), transparent 60%);
}The result is the brand color at 40% opacity. The color channels stay the brand color, and only the alpha changes. You get the same visual result from oklch(from var(--brand) l c h / 0.4), which a later chapter covers.
If the percentages add up to less than 100%, the mix keeps the normalized proportions and multiplies the alpha by the total. For example, color-mix(in oklab, red 30%, blue 30%) is a purple at 60% opacity. If you want a fully opaque mix, leave one percentage off or make them add up to 100%.
Gradients use the same rule
Gradients interpolate colors with the same machinery. You can set the space inside the gradient function.
.banner-default {
background: linear-gradient(to right, #1d4ed8, #facc15);
}
.banner-oklab {
background: linear-gradient(in oklab to right, #1d4ed8, #facc15);
}
.banner-oklch {
background: linear-gradient(in oklch to right, #1d4ed8, #facc15);
}Without an in clause, a gradient interpolates in srgb. The default is why old gradients between saturated colors often pass through a gray or muddy band in the middle. Adding in oklab removes the uneven darkening. Using in oklch keeps the middle vivid, at the cost of passing through other hues on the way.
Polar gradients accept the same hue methods as color-mix(), such as in oklch longer hue. Those are the subject of the hue interpolation chapter, so this chapter does not cover them.
A decision path
The diagram turns the guidance above into a short sequence of questions.
Use it as a starting point, then look at the result. A swatch on screen is a better test than a rule.
Common errors
- Forgetting
in.color-mix(red, blue)is invalid. The whole declaration is discarded, and the element keeps an earlier value or the inherited one. - Assuming the input space matters. The space you wrote the colors in (hex,
hsl(),oklch()) does not change the mix. Only theinspace does. - Using
hslbecause the inputs are hsl.hsllightness is not perceptual, so tints of different hues do not match in apparent lightness. - Expecting one space to be best everywhere.
oklchkeeps colors vivid, but it can take an unexpected route around the hue circle.oklabis steadier, but it can produce gray midpoints between opposite hues. - Ignoring gamut. Mixing in
oklchcan produce a color the display cannot show. The browser maps it into the display gamut, which can alter it slightly. The gamut chapter explains how to handle this. - Testing in one browser only. Older browsers ignore
color-mix()completely. Provide a fallback declaration before it, as shown in the browser support chapter.
Practice
Exercise
You want a 12% tint of a brand color on white for a card background, and the brand color comes from a user-supplied theme that may be a near-gray. Which interpolation space is the safer choice, and why?
Show answer
Use oklab, for example color-mix(in oklab, var(--brand) 12%, white). It is rectangular, so it has no hue angle to misbehave. A near-gray brand color has a tiny chroma that barely affects an oklab mix. In oklch the near-gray color still has a real hue angle, and that angle can shift the tint.
Exercise
What does color-mix(in srgb, red 30%, blue 30%) produce, and how would you make it fully opaque?
Show answer
It produces a purple at 60% opacity, because the percentages add up to 60% and the alpha is scaled by that total. To make it opaque, make the percentages total 100%, for example color-mix(in srgb, red 50%, blue 50%), or leave out one percentage so it is calculated for you.
Exercise
A gradient from a saturated blue to a saturated yellow looks muddy and gray in the middle. Name two changes you can make to the gradient's interpolation space, and describe how the middle will differ.
Show answer
Add in oklab to get a smoother gradient with more even lightness, which may still be low in chroma in the middle because the colors are opposites. Add in oklch to keep chroma high through the middle, so the band passes through vivid in-between hues such as green instead of gray.