Modern Colour Functions — CSS

oklch(L C H) describes a colour with three numbers: lightness, chroma (how strong the colour is) and hue (the position on the colour wheel). The same…

Compare hsl(60 100% 50%) (yellow) and hsl(240 100% 50%) (blue). Both have a lightness of 50%. Do you think they look equally bright on screen?

Keep your guess for now: the answer is in the second section. CSS now has new colour functions, besides hex, rgb() and hsl(). oklch() describes a colour in a way that matches how our eyes see light and dark. color-mix() mixes two colours. light-dark() picks a colour for light mode or dark mode.

The three numbers in oklch()

This rule gives a box a background colour with oklch():

.box {
  background: oklch(70% 0.15 200);
}

The name oklch comes from the three numbers it holds: L, C and H. The OK comes from the name of its colour space, Oklab (a colour space is one way of writing colours as numbers).

The result is a bright turquoise box.

Why not just hsl()?

The lightness in hsl() is the result of a maths formula, not the brightness the eye feels. This is the answer to the guess at the start of the lesson: hsl(60 100% 50%) yellow looks far brighter than hsl(240 100% 50%) blue, even though the lightness number is the same.

In oklch(), the same lightness looks equally bright for any colour. So you can build a palette (a set of colours used together) by changing only the hue, and the brightness stays even. If you raise L, every colour looks lighter by the same amount.

Mix colours with color-mix()

This rule mixes blue with white:

.pale {
  background: color-mix(in oklch, blue 30%, white);
}

The result is a pale blue. The smaller the share of blue, the paler the result. Replace white with black for a darker version, for example for a button's colour on hover.

Derived colours and light-dark()

A relative colour (a derived colour) builds a new colour from another one. Here, the border colour is derived from the --color variable:

.card {
  border-color: oklch(from var(--color) calc(l - 0.2) c h);
}

from var(--color) splits the original colour into l, c and h. The chroma and hue are used as they are. The lightness is lowered by 0.2 with calc(l - 0.2), so the border is darker than the original colour. Here l runs from 0 to 1, so 0.2 means 20%. If you change --color, the border colour changes too.

light-dark() picks one of two colours depending on the mode of the visitor's device, light or dark:

:root { color-scheme: light dark; }
body { color: light-dark(black, white); }

color-scheme: light dark says that this page supports light mode and dark mode. In light-dark(), the first colour is for light mode, and the second is for dark mode.

⚠️ Is the colour different from what you wrote?

TL;DR: oklch(L C H) describes a colour with three numbers: lightness, chroma (how strong the colour is) and hue (the position on the colour wheel). The same lightness looks equally bright across all hues, unlike hsl(). color-mix(in oklch, color 30%, white) mixes two colours: 30% of that colour, and white for the rest. oklch(from ...) derives a new colour from another one. light-dark(light, dark) picks a colour for light mode or dark mode, as long as color-scheme: light dark is set.

⚡ The three numbers in oklch

Write:

.box {
  background: oklch(70% 0.15 200);
}

Meaning: oklch() describes a colour with three numbers. 70% is the lightness (L): 0% is black, 100% is white. 0.15 is the chroma (C), or how strong the colour is. 200 is the position on the colour wheel (H, the hue). Chroma is a small number, usually 0 to about 0.4, not a percentage.

⚡ Equally bright to the eye

Guess first: hsl(60 100% 50%) yellow and hsl(240 100% 50%) blue have the same lightness, 50%. Do they look equally bright?

No. The yellow looks far brighter. The lightness in hsl() is only the result of a formula, not the brightness the eye sees.

Meaning: In oklch(), the same lightness looks equally bright for any colour. So you can build a palette (a set of colours) by changing only the hue, and they all look equally bright.

⚡ Mixing two colours

Write:

.pale {
  background: color-mix(in oklch, blue 30%, white);
}

Meaning: color-mix() mixes two colours, and in oklch sets how the mix is calculated. Blue gets 30% and white gets the rest, so the result is a pale blue. Replace white with black for a darker version.

What you will learn