Testing CSS colour methods

Here are some of the ways you can apply CSS to create colours:

Named colour

A set of predefined keywords, like white, red or royalblue. The most common way beginners give an element colour, but limited to a fixed list of names.

background-color: royalblue;

See full list of colour names.

Hex

A hexadecimal value representing red, green and blue. Compact and everywhere, but not easy to read or adjust by eye.

background-color: #2764d7;

Hex can also be written as a 3-character shorthand. Each character is doubled to make the full 6-digit value, so #06f expands to #0066ff.

RGB

Red, green and blue written out as separate numbers. Easier to reason about than hex, though it still doesn't match how we perceive colour.

background-color: rgb(39, 100, 215);

RGBA

The same as RGB, with an added alpha value for transparency.

background-color: rgba(39, 100, 215, 0.7);

HSL

Hue, saturation and lightness. More intuitive to adjust than RGB, but its lightness value doesn't line up with how bright a colour actually looks.

background-color: hsl(217, 69%, 50%);

HSLA

The same as HSL, with an added alpha value for transparency.

background-color: hsla(217, 69%, 50%, 0.7);

HWB

Hue, whiteness and blackness. Built by mixing a hue with white and black, which some people find a more natural way to think about colour.

background-color: hwb(217 15% 16%);

LAB

Based on the CIE Lab colour space. Designed for perceptual uniformity, but predates modern wide-gamut displays.

background-color: lab(48% -4 -45);

LCH

The cylindrical form of LAB, using lightness, chroma and hue instead of LAB's harder-to-read coordinates.

background-color: lch(48% 45 264);

Oklab

A newer, more accurate take on perceptual uniformity than LAB. It's the colour space that OKLCH is built on.

background-color: oklab(50% -0.03 -0.14);

OKLCH

The cylindrical form of Oklab. More perceptually accurate than HSL, and able to describe colours beyond the sRGB gamut.

background-color: oklch(50% 0.14 264);

OKLCH with alpha

The same as OKLCH, with an added alpha value for transparency.

background-color: oklch(50% 0.14 264 / 50%);