Sub topic 3
Writing colors with oklch()
This chapter assumes you know basic CSS selectors, custom properties (var(--name)), and how hex, rgb() and hsl() colors are written. You should also know why hsl() can look uneven, which the previous chapters covered.
The syntax
oklch() takes three components, then an optional alpha value after a slash. The components are separated by spaces, not commas.
:root {
--brand: oklch(62% 0.19 255);
--brand-translucent: oklch(62% 0.19 255 / 0.5);
}oklch( L C H / alpha )The first line defines a blue. The second line is the same blue at 50% opacity.
What each component means
| Component | Range | Meaning |
|---|---|---|
| L (lightness) | `0%` to `100%`, or `0` to `1` | How light the color looks. `0%` is black and `100%` is white. |
| C (chroma) | `0` up to about `0.4` | How colorful it is. `0` is a pure gray. |
| H (hue) | Degrees, `0` to `360` | Position on the color wheel. |
| alpha | `0` to `1`, or a percentage | Opacity. |
Some details to keep in mind:
- Lightness is perceptual. A blue and a yellow with the same L look about equally light, which is not true in
hsl(). - Chroma has no fixed upper limit. In practice, screens can show only about
0.37at most, and far less for some hues. A percentage maps100%to0.4. - Hue is an angle. Approximate positions are red near
30, yellow near100, green near145, blue near260and pink near350. These differ from thehsl()hue wheel, so you cannot copy hue numbers across. - At
Cof0, hue has no visible effect.
CSS also accepts angle units such as 0.5turn for hue.
Adjusting one component at a time
The main benefit of oklch() is that you can change one component and the others stay stable. A hover state needs only a lower lightness:
.button {
background: oklch(60% 0.15 250);
}
.button:hover {
background: oklch(52% 0.15 250);
}The hover color is darker, but it is not more or less colorful, and it has not shifted toward purple or green. With hsl(), lowering lightness often changes how saturated a color looks.
The same idea works for a set of related colors. This example gives three badges the same lightness and chroma and varies only the hue, so they look like a matched set:
.badge {
--l: 70%;
--c: 0.14;
padding: 0.25em 0.75em;
border-radius: 999px;
color: oklch(20% 0.05 var(--h));
background: oklch(var(--l) var(--c) var(--h));
}
.badge-info { --h: 250; }
.badge-ok { --h: 145; }
.badge-warn { --h: 80; }
.badge-error { --h: 25; }Custom properties can hold a single component, such as --h, because they are substituted before the color is parsed.
Common errors
- Using
Las a plain number above 1.oklch(60 0.15 250)reads60as a number on the 0 to 1 scale. It clamps to full lightness and gives white. Write60%or0.6. - Using commas.
oklch(60%, 0.15, 250)is invalid, and the whole declaration is dropped. - Reusing
hsl()hue values. A hue of220inhsl()is a blue, but inoklch()it is closer to teal. - Setting chroma too high. Not every combination can be shown on a screen. The browser maps such a color to the nearest displayable one, so the result can look duller than you expected. A later chapter covers this.
Alpha
Alpha goes after a slash and accepts a number or a percentage. These two colors are the same:
.overlay-a {
background: oklch(20% 0.02 260 / 0.6);
}
.overlay-b {
background: oklch(20% 0.02 260 / 60%);
}Exercise
A teammate writes background: oklch(45, 0.2, 280 / 0.8); and the background does not appear. Name the two mistakes and write the corrected declaration for a purple that is 45% light.
Show answer
Commas are not allowed between components, and the lightness 45 is a bare number on a 0 to 1 scale, so it would clamp to white even without the commas. The corrected declaration is background: oklch(45% 0.2 280 / 0.8);.
Exercise
You have --brand: oklch(62% 0.19 255). Write a rule that gives .link:hover the same hue and chroma but 8 percentage points less lightness. Use a literal oklch() value.
Show answer
.link:hover { color: oklch(54% 0.19 255); }