oklch()

Writes a colour through lightness, chroma, and hue in a space matched to the eye. Colours of the same lightness look equally light.

On this page

Example

#
CSS
:root {
  --primary: oklch(62% 0.22 295);
  --success: oklch(62% 0.15 155);
  --danger: oklch(62% 0.21 25);
}

.btn { color: #fff; }
.btn.primary { background: var(--primary); }
.btn.success { background: var(--success); }
.btn.danger { background: var(--danger); }
Preview

All three colours have the same lightness L = 62%, so white text has similar contrast on each of them.

Definition and usage

#

The oklch() function describes a colour with three numbers. L is lightness from 0% to 100% (or from 0 to 1), C is chroma, the intensity of the colour, from 0 upwards, in practice up to about 0.37, and H is the hue in degrees on the colour wheel. It works similarly to hsl(), but the colour space is built around how the human eye perceives lightness.

The biggest advantage: the same L value really does look equally light in every hue. oklch(70% 0.12 90) (yellow) and oklch(70% 0.12 260) (blue) look similarly light to the eye, whereas in HSL at equal lightness the yellow would be clearly lighter. This makes it easier to build palettes and predict text contrast against the background.

OKLCH also covers colours outside the sRGB range. Newer screens, for example with a P3 gamut in phones and laptops, show more saturated colours than you can describe in rgb() or hex. If you give a chroma the screen can’t display, the browser picks the nearest possible colour itself.

You add transparency after a slash: oklch(65% 0.17 300 / 0.5). Hues are distributed differently than in HSL: red falls around 29, green around 142, and blue around 264 degrees.

Syntax

#
Syntax
oklch(<lightness> <chroma> <hue> [/ <alpha>]?)

Colour variants

#
  • oklch(65% 0.17 300)

    base colour

    A purple of medium lightness and a clear but not excessive chroma.
  • oklch(45% 0.17 300)

    lightness 45%

    Lower L with the same chroma and hue. The colour gets darker but stays the same purple.
  • oklch(65% 0.07 300)

    chroma 0.07

    Low chroma gives a muted, subdued purple. With C equal to 0 you would get pure grey of the same lightness.
  • oklch(65% 0.17 150)

    hue 150

    A different hue at the same lightness. The green looks as light as the purple from the first example, because L hasn’t changed.
  • oklch(65% 0.17 300 / 0.4)

    alpha 0.4

    Transparency after a slash, just like in rgb() and hsl().

Colour variants in practice

#

oklch(65% 0.17 300)

A purple of medium lightness and a clear but not excessive chroma.
.demo { background: oklch(65% 0.17 300); }

oklch(45% 0.17 300)

Lower L with the same chroma and hue. The colour gets darker but stays the same purple.
.demo { background: oklch(45% 0.17 300); }

oklch(65% 0.07 300)

Low chroma gives a muted, subdued purple. With C equal to 0 you would get pure grey of the same lightness.
.demo { background: oklch(65% 0.07 300); }

oklch(65% 0.17 150)

A different hue at the same lightness. The green looks as light as the purple from the first example, because L hasn’t changed.
.demo { background: oklch(65% 0.17 150); }

oklch(65% 0.17 300 / 0.4)

Transparency after a slash, just like in rgb() and hsl().
.demo { background: oklch(65% 0.17 300 / 0.4); }

More examples

#
HSL versus OKLCH at the same lightness
CSS
.hsl span {
  background: hsl(var(--h) 90% 50%);
}

.oklch span {
  background: oklch(70% 0.12 var(--h));
}
Preview

In the top row the yellow-green glows clearly stronger than the blue, even though all the colours have L = 50%. In the bottom row they look similarly light.

Browser support

#

Widely available · since 2023

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • Build palettes by changing only L while keeping C and H constant. Successive shades will be evenly lighter and won’t shift in hue.
  • Keep chroma moderate, for example from 0.1 to 0.2. Very high values go beyond what many screens can show and will be clipped.
  • Create new colours from existing ones with color-mix(in oklch, …). Mixing in this space gives nicer intermediate colours than in sRGB.

Related entries

#

Spotted a mistake or missing an example? Write to us.