CSS Module 6 – Colour and theming
OKLCH, Oklab and wide-gamut colour
Write colours in oklch(), build evenly stepped palettes by OKLCH lightness, and use Display P3 colours with an sRGB fallback and color-gamut.
What you will learn
- Write colours in oklch() and explain lightness, chroma and hue
- Build an evenly stepped palette by varying OKLCH lightness
- Predict what an sRGB screen shows for a colour outside its gamut
- Use display-p3 colours with an sRGB fallback and the color-gamut media query
Before you start
What you’ll learn · Before you start
What you will learn
- Write colours in oklch() and explain lightness, chroma and hue
- Build an evenly stepped palette by varying OKLCH lightness
- Predict what an sRGB screen shows for a colour outside its gamut
- Use display-p3 colours with an sRGB fallback and the color-gamut media query
Before you start
On this page
The supplied nine-step palettes need white labels that pass across three hues. At The L coordinate of OKLCH, describing perceptual lightness separately from chroma and hue. 0.55, their recorded ratios are 4.89, 4.63 and 4.87; equal HSL lightness does not line those decisions up. OKLCH separates lightness, A coordinate describing how far a colour is from a neutral colour at its lightness. and hue so you can state the intended edit, while A perceptual colour space with lightness and two rectangular colour coordinates; OKLCH is its polar form. expresses the same space with rectangular colour axes.
Display P3 can show colours outside A colour space used by CSS hex, RGB and HSL values, with a defined range of reproducible colours., but understanding their syntax does not enlarge a screen’s range. Keep an sRGB branch and choose its P3 enhancement with the gamut query, checking each visible pair. The supplied programmes and pixel tables make those decisions concrete.
Read lightness chroma and hue
Wide-gamut colour uses a colour range larger than sRGB. Wide-gamut colour still needs an sRGB fallback when the target display cannot reproduce that range.
This lesson builds the same palette in HSL and in OKLCH and measures its contrast, checks which colours fit which screens, and reads the pixels that Chromium 153, Firefox 155 and WebKit 26.6 painted for a colour an sRGB screen can’t show.
- Lightness is a number from 0 to 1, or a percentage where 100% means 1. 0 is black and 1 is white, whatever the other two numbers say.
- Chroma is 0 for a gray. A percentage is allowed too, with 100% meaning 0.4. The most colourful sRGB colours
reach about 0.32 (the gamut program below finds pure blue
#0000ffatoklch(0.452 0.313 264.1)), and Display P3’s pure green reaches 0.369, so in practice chroma stays below 0.4. - Hue is an angle in degrees. CSS Color 4 gives the landmarks: 0 points to a purplish red, 90 to a mustard
yellow, 180 to a greenish cyan and 270 to a sky blue. The angles are not those of HSL: HSL’s blue is at 240, while
#0000ffsits at 264 in OKLCH.
An alpha comes after a slash, as in every modern colour function: oklch(0.55 0.15 262 / 50%). The keyword none
marks a missing component, which matters mostly when colours are mixed. Commas are not allowed in oklch() at all:
it was never part of the legacy syntax.
Oklab is the same space in rectangular form: oklab(0.55 -0.02 -0.15) gives the lightness, then a, from green
(negative) to a purplish red (positive), and b, from blue (negative) to yellow (positive). OKLCH is easier to edit by hand,
because hue and chroma are separate numbers; Oklab is what browsers use by default when they mix colours, which the
next lesson covers. CSS also has the older CIE Lab and LCH as lab() and lch(). They were the first
perceptual spaces in CSS, but blues in LCH drift towards purple as their chroma falls, a flaw Oklab was designed to
fix. oklab(), oklch(), lab(), lch() and color() are all Baseline widely available.
A script reads an oklch() colour back in its own form, not as rgb(): getComputedStyle() reported
oklch(0.4 0.15 264) for oklch(0.4 0.15 264) in all three engines, with the lightness as a number. Converting it to
sRGB would lose any colour outside sRGB, so CSS Color 4 keeps it.
Color-gamut reports roughly which display range the browser and screen support. It chooses a display branch and does not decide whether a colour notation parses.
The drawing shows the whole scene; its 3 steps are listed below.
Every step as text
- Read the first coordinate. Lightness 0.55 locates this colour between black and white. The coordinate is independent of the optional alpha after a slash.
- Change the second. Chroma 0.15 supplies its colourfulness. Setting chroma to zero makes a neutral gray; it does not make the alpha zero.
- Keep the third. Hue 262 is the blue direction in this space. Holding hue while editing lightness or chroma states which property of the colour should remain.
The original blue uses lightness 0.55, chroma 0.15 and hue 262. Editing one coordinate states which property of the colour should change; alpha remains a separate optional component.
Checkpoint
Check: Read lightness chroma and hue
Build palette steps and trace display gamut Pro: opens with a Pro, Premium or Ultimate pass
Measure every supplied palette step and keep its output range in the decision. The recorded clipping cases separate a valid colour from the pixel an sRGB surface showed.
In this part:
- Check every palette step
- Distinguish syntax and display range
- Build a scale that stays in its chosen range
- Build a nine-step OKLCH scale that every screen can show
- Results of the sample tests
3 diagrams · 3 runnable examples (HTML, JavaScript) · 1 exercise with sample tests · about 14 minutes
Opening this part with your pass…
This part opens with any MySmartCoPilot pass.
- Pro pass · 1 week₹79$2.99
- 30 days₹179$5.99
- 1 year₹1,699$45
One payment; nothing renews unless you choose to. The price shown is the total you pay: no fees or charges are added at checkout.
Passes can’t be bought right now, so this part can’t be opened at the moment. The free part above stands on its own. If you have a pass, enter your licence key.
It also opens every other in-depth part in Learn and every Pro tool. Sales are final, except where the law gives you a right to withdraw, for example in the EU and the UK (refund policy).
Choose and audit both accent branches Pro: opens with a Pro, Premium or Ultimate pass
Select syntax support and display range independently. Keep both original accent colours and every foreground with the branch that actually uses it.
In this part:
- Choose the right fallback branch
- Keep the coordinate starting case in context
- Keep the depth decisions together
1 diagram · 1 runnable example (HTML) · about 6 minutes
Opening this part with your pass…
This part opens with any MySmartCoPilot pass.
- Pro pass · 1 week₹79$2.99
- 30 days₹179$5.99
- 1 year₹1,699$45
One payment; nothing renews unless you choose to. The price shown is the total you pay: no fees or charges are added at checkout.
Passes can’t be bought right now, so this part can’t be opened at the moment. The free part above stands on its own. If you have a pass, enter your licence key.
It also opens every other in-depth part in Learn and every Pro tool. Sales are final, except where the law gives you a right to withdraw, for example in the EU and the UK (refund policy).
Practise: choose coordinates before comparing colours
Practice that adapts to you
Practise this lesson
2 questions about 4 ideas of this lesson. Each answer is explained at once, and what you get right is kept in this browser.
Interview questions
Fresher · What do the three numbers in oklch(0.55 0.15 262) mean, and why use OKLCH instead of HSL for a palette?
The first number is the lightness, from 0 for black to 1 for white; the second is the chroma, how colourful the colour is, from 0 for a gray to about 0.32 for the most vivid sRGB colours; the third is the hue in degrees, where 262 is a blue. OKLCH is built on Oklab, which was designed so that equal steps in its numbers look like equal steps to the eye. HSL’s lightness is only the average of the largest and smallest channel, so the same HSL lightness gives yellow 13 times the luminance of blue. In the supplied chroma-0.1 scale, white text at lightness 0.55 passes for its three hues at 4.89, 4.63 and 4.87. That is a measured starting pattern; changing chroma, mapping, alpha or backing requires a new visible-pair comparison.
-
Mid to senior
A design system built its colour scales by changing HSL lightness in 10% steps. Teams complain that white text on step 50% is readable on the blue scale but not on the green or yellow ones. How would you rebuild the scales, and what rule would you give teams?
-
Mid to senior
Marketing wants a vivid Display P3 green for the main Buy button, but many visitors use sRGB laptops. How do you ship it so that both groups get a good, accessible button?
-
Mid to staff
A button has an sRGB default and a Display P3 enhancement. Its syntax test passes on an sRGB laptop, but a designer calls that proof the P3 branch is accessible. Design a regression matrix that separates syntax, gamut, state and contrast.
Key takeaways
-
oklch(L C H)uses lightness from 0 to 1, nonnegative chroma and hue in degrees; the chroma that fits depends on the colour and gamut, rather than a syntax maximum of 0.37.oklab()uses the same space withaandbaxes. -
Give sRGB screens a colour you chose with
@media (color-gamut: p3), and check contrast for both values of the token. -
Editing lightness, chroma or hue states which coordinate should change while the others remain fixed.
Correction: the contrast starting rows describe the supplied chroma-0.1 trials. They do not approve another chroma, output mapping, alpha, backing or label without checking its actual pair.
Terms in this lesson
- Chroma
- A coordinate describing how far a colour is from an achromatic colour at its lightness.
- Gamut
- The range of colours a colour space or output device can represent.
- OKLCH lightness
- The L coordinate of OKLCH, describing perceptual lightness separately from chroma and hue.
Corrections
- Kept captured clipping separate from general gamut mapping and aligned the scale hint with its chroma ceiling.
References
- CSS Color Module Level 4: Oklab and OkLCh (W3C CSS Working Group)
- CSS Color Module Level 4: specifying Oklab and OkLCh colours (W3C CSS Working Group)
- CSS Color Module Level 4: predefined colour spaces and color() (W3C CSS Working Group)
- CSS Color Module Level 4: gamut mapping (W3C CSS Working Group)
- CSS Color Module Level 4: sample code for colour conversions (W3C CSS Working Group)
- Media Queries Level 4: the color-gamut feature (W3C CSS Working Group)
- A perceptual color space for image processing (Oklab) (Björn Ottosson)
- oklch() (MDN Web Docs) (Mozilla)
- color() (MDN Web Docs) (Mozilla)
- Oklab and OkLCh (web-features explorer) (W3C WebDX Community Group)
- Colors (Tailwind CSS documentation) (Tailwind Labs)
- Web Content Accessibility Guidelines (WCAG) 2.2: Contrast (Minimum) (W3C)
Related tools
Report a problem with this lesson
Kept only in this browser. Your Learn progress