Sub topic 5
Modern color: oklch() and color-mix()
This chapter assumes you already know hex, rgb(), and hsl() notation, CSS custom properties, and how currentColor works. It covers two newer tools: the oklch() color function and the color-mix() function. Together they let you build palettes and derived shades from a single color value.
Why hsl() falls short
hsl() describes a color with hue, saturation, and lightness. The lightness value looks like a brightness control, but it does not match what people see. Compare these two colors:
.yellow { background-color: hsl(60 100% 50%); }
.blue { background-color: hsl(240 100% 50%); }Both have 50% lightness. Yellow looks very bright and blue looks dark. If you build a palette by changing only the hue and keeping the other numbers fixed, the swatches differ in perceived brightness. Text on those swatches then has very different contrast.
hsl() is also a rearrangement of sRGB, the color space most displays used historically. It cannot name colors outside that space, such as the more vivid colors on wide-gamut (Display P3) screens.
The oklch() function
oklch() uses three channels plus an optional alpha value:
color: oklch(L C H / alpha);| Channel | Meaning | Typical range |
|---|---|---|
| L | Perceptual lightness | `0%` (black) to `100%` (white), or `0` to `1` |
| C | Chroma, the amount of color | `0` (gray) to about `0.4` |
| H | Hue angle in degrees | `0` to `360` |
| alpha | Opacity | `0` to `1`, or a percentage |
The key property is that L tracks perceived lightness. Two colors with the same L look about equally bright, whatever their hue. This is what makes oklch() useful for palettes.
The hue angles differ from hsl(). In OKLCH, roughly 30 is red, 100 is yellow, 145 is green, 195 is cyan, 260 is blue, and 330 is magenta. These are approximate. Check a color in a browser rather than guessing.
Chroma is not saturation
Chroma has no fixed upper limit. The largest chroma available depends on lightness and hue. A very light or very dark color has little room for chroma. A blue at mid lightness can reach a high value, while a yellow at the same lightness cannot.
For everyday UI colors, chroma between 0.02 and 0.2 is a safe working range. Low values such as 0.02 give tinted grays. They work well for page backgrounds and borders.
A palette with consistent lightness
This stylesheet builds status colors by varying only the hue:
:root {
--success: oklch(65% 0.15 145);
--info: oklch(65% 0.15 250);
--warning: oklch(65% 0.15 80);
--danger: oklch(65% 0.15 25);
}
.badge {
padding: 0.25em 0.75em;
border-radius: 999px;
color: black;
}
.badge.success { background-color: var(--success); }
.badge.info { background-color: var(--info); }
.badge.warning { background-color: var(--warning); }
.badge.danger { background-color: var(--danger); }All four backgrounds have about the same perceived lightness, so the same black text reads similarly on each. With hsl() you would need to adjust each color by eye.
Gamut and out-of-range values
A gamut is the range of colors a display can show. If you write an oklch() value outside the screen's gamut, the browser maps it to the nearest displayable color. This does not cause an error. The result is a duller color than you wrote. On a wide-gamut screen the same value may look more vivid than on a standard screen.
The channel values are also not clamped the way rgb() values are in older notation. A chroma of 0.5 is valid syntax. It is simply mapped down. Raising chroma beyond what the display supports changes nothing visible, so you can waste time tuning numbers that have no effect.
The color-mix() function
color-mix() blends two colors in a chosen color space:
color-mix(in <color-space>, <color> [<percentage>], <color> [<percentage>])Each color can have a percentage. The result is a single color you can use anywhere a color is allowed.
.mixed {
background-color: color-mix(in oklch, oklch(55% 0.2 260), white 30%);
}This takes 30% white and 70% of the blue. The omitted percentage is filled in so the two add up to 100%.
How percentages are resolved
| You write | Result |
|---|---|
| No percentages | 50% of each color |
| One percentage, such as `white 30%` | The other color gets `70%` |
| Two percentages that sum to 100% | Used as written |
| Two percentages that sum above 100% | Scaled down proportionally to 100% |
| Two percentages that sum below 100% | Scaled up if needed, and the result is partly transparent |
The last row catches people out. If you write color-mix(in oklch, red 20%, blue 20%), the result has an alpha of 0.4, so the mix is translucent. Make sure the percentages sum to 100% when you want an opaque color.
The color space changes the result
The in keyword chooses where the blend happens. Mixing blue and white in srgb and in oklch gives different intermediate colors. In srgb, blue mixed with white often drifts toward a purple tint. In oklch, the lightness increases steadily and the hue stays closer to blue.
Pick oklch when you want tints and shades that look natural. Pick srgb when you need to match what an older tool produced.
Hue interpolation
Hue is an angle, so there are two ways around the color wheel between two hues. By default, color-mix() takes the shorter path. You can state it explicitly:
.longer {
background-color: color-mix(in oklch longer hue, red, blue);
}The shorter hue, longer hue, increasing hue, and decreasing hue options apply only to cylindrical spaces like oklch and hsl. Mixing red and blue the short way passes through magenta. Mixing the long way passes through yellow and green.
If one color is achromatic (chroma 0, such as white or gray), its hue is treated as missing. The result then takes the hue of the other color. That is why mixing a blue with white keeps the color blue.
Building a derived palette
Define one brand color, then derive every other shade from it. This keeps a single source of truth.
:root {
--brand: oklch(55% 0.2 260);
--brand-tint: color-mix(in oklch, var(--brand), white 40%);
--brand-shade: color-mix(in oklch, var(--brand), black 30%);
--brand-muted: color-mix(in oklch, var(--brand), gray 50%);
}
.button {
background-color: var(--brand);
color: white;
border: 2px solid var(--brand-shade);
}
.button:hover {
background-color: var(--brand-tint);
color: black;
}
.button:disabled {
background-color: var(--brand-muted);
}Changing --brand updates every derived color. The hover state switches the text to black, because a 40% tint is light and white text on it would have poor contrast.
Transparent mixes
To make a color translucent, mix it with transparent:
.overlay {
background-color: color-mix(in oklch, var(--brand), transparent 60%);
}The result keeps the brand color at 40% opacity. This is useful when the base color lives in a custom property and you cannot write the alpha channel inline. The / alpha syntax works only when you write the color channels directly.
Relative color syntax
A newer feature, relative color syntax, reads the channels of an existing color and lets you change them with calc():
.lighter {
background-color: oklch(from var(--brand) calc(l + 0.15) c h);
}Here l, c, and h are the channels of --brand. This gives exact control over a single channel, which color-mix() cannot do. Browser support is newer than for oklch() and color-mix(), so check a compatibility table before relying on it.
Browser support and fallbacks
Current versions of all major browsers support oklch() and color-mix(). Older browsers ignore a declaration they do not understand. Use that behavior to supply a fallback:
.button {
background-color: #2f5bd4;
background-color: oklch(55% 0.2 260);
}The browser uses the last declaration it can parse. This pattern does not work when the value comes from a custom property. A custom property accepts any text, so the failure only shows up at the point of use, and there the declaration becomes invalid with no fallback. For custom properties, wrap the definitions in a feature query:
:root {
--brand: #2f5bd4;
}
@supports (color: oklch(0% 0 0)) {
:root {
--brand: oklch(55% 0.2 260);
}
}Common errors
- Writing hue first. The order is lightness, chroma, hue.
oklch(260 0.2 55%)is invalid. A percentage belongs on lightness, and the hue is a plain number. - Forgetting the
inkeyword.color-mix(oklch, ...)is invalid. Writecolor-mix(in oklch, ...). - Assuming L is a contrast ratio. Equal lightness means similar brightness. It does not guarantee a WCAG ratio of 4.5:1 for text. Always check the actual pair.
- Percentages that sum below 100%. This produces a translucent result without warning.
- Using the alpha slash on a variable.
var(--brand) / 0.5is invalid. Usecolor-mix()withtransparent, or relative color syntax.
Exercise
A stylesheet defines --accent: oklch(60% 0.18 30);. Write a declaration that sets a button's hover background to a lighter version of --accent made by mixing in 25% white in the OKLCH space, and explain what happens if you write color-mix(in oklch, var(--accent) 25%, white 25%) instead.
Show answer
Use background-color: color-mix(in oklch, var(--accent), white 25%);. The accent gets the remaining 75%. In the second version the percentages sum to 50%, so the browser keeps those proportions but gives the result an alpha of 0.5. The button becomes half transparent instead of a solid lighter shade.
Exercise
You build four badge colors with oklch(65% 0.15 H) and the hues 25, 80, 145, and 250. Why is this more predictable than the same palette written with hsl(H 70% 50%), and what must you still check before using black text on them?
Show answer
In OKLCH the lightness channel follows perceived brightness, so all four colors look about equally bright. In hsl(), equal lightness values produce very different perceived brightness across hues, such as yellow against blue. You must still measure the contrast ratio of black text on each badge against WCAG (4.5:1 for normal text), because equal lightness does not guarantee that ratio.