Sub topic 7
Gamut limits and fallbacks
This chapter assumes you know basic CSS syntax, custom properties, and how oklch() takes lightness, chroma and hue. You should also be comfortable with the @supports rule at a basic level. Here you will learn what happens when an oklch() color cannot be shown on a screen, and how to write CSS that still works in older browsers.
What a gamut is
A gamut is the range of colors that a display can produce. Most screens use the sRGB gamut. Newer phones, laptops and monitors can show wider gamuts such as Display P3, which includes more vivid greens, reds and blues.
The oklch() function is not tied to any one gamut. It can describe colors that no screen can show. Chroma has no fixed upper limit in the syntax, so oklch(60% 0.4 140) is valid CSS even though no display can reproduce it.
What happens to out-of-range colors
When a color falls outside the display's gamut, the browser maps it to the closest color the display can show. This is called gamut mapping. The CSS Color specification describes a method that keeps lightness and hue and reduces chroma until the color fits. The result is still valid, but it is duller than the number you wrote.
Gamut mapping has two practical effects:
- Two different high-chroma values can end up looking identical on the same screen, because both are reduced to the same limit.
- The same CSS can look different on a P3 display and on an sRGB display.
The maximum chroma that fits also depends on lightness and hue. A blue at oklch(45% 0.25 265) may fit in sRGB, while a yellow at the same chroma may not. There is no single chroma value that is safe for every color.
Staying inside sRGB on purpose
If your design must look the same everywhere, keep chroma modest. Values up to about 0.15 stay inside sRGB for many hues at mid lightness, but you should not treat that as a rule. Check the colors you actually use.
Browser developer tools help here. Several of them show a gamut boundary line in the color picker for oklch() values, so you can see whether a color is inside sRGB or only inside P3.
Wide-gamut colors are still a good choice when you want more vivid accents on capable screens. You can combine both approaches using a media query:
:root {
--accent: oklch(65% 0.15 250);
}
@media (color-gamut: p3) {
:root {
--accent: oklch(65% 0.24 250);
}
}The color-gamut: p3 media feature matches when the display can show at least the Display P3 gamut. Other screens keep the safer sRGB value.
Fallbacks for browsers without oklch()
Browsers that do not understand oklch() treat the declaration as invalid and ignore it. CSS then uses an earlier declaration for the same property, if one exists. This gives you a simple fallback pattern: write the older color first and the oklch() color after it.
.button {
background: #3b82f6;
background: oklch(62% 0.19 255);
}The order matters. If you reverse the two lines, every browser uses the hex value, because the later valid declaration wins.
Fallbacks and custom properties
The two-line pattern does not work when the value comes from a custom property. A declaration such as background: var(--brand) is always accepted at parse time, because the browser cannot check the variable's contents yet. If --brand holds an oklch() value that the browser does not understand, the declaration becomes invalid at computed-value time, and the earlier fallback line is not used. The property falls back to its inherited or initial value instead.
Use @supports to choose the token values:
:root {
--brand: #3b82f6;
}
@supports (color: oklch(0% 0 0)) {
:root {
--brand: oklch(62% 0.19 255);
}
}
.button {
background: var(--brand);
}Browsers without oklch() support skip the @supports block and keep the hex token.
Fallbacks for color-mix()
The same idea applies to color-mix(). Test for it with @supports and give the plain rule first:
.button:hover {
background: #2f6fd8;
}
@supports (color: color-mix(in oklch, red, blue)) {
.button:hover {
background: color-mix(in oklch, var(--brand), black 15%);
}
}Support for oklch() and color-mix() arrived in current browsers at similar times, but support for each feature is separate. Test each one you use.
Common mistakes
- Writing the fallback after the modern value, so the fallback always wins.
- Expecting a two-line fallback to protect
var()values. - Using a very high chroma and assuming the screen shows it. The browser quietly reduces it.
- Picking a hex fallback that looks noticeably different from the
oklch()color. Convert the color with a tool, or read the sRGB value from your browser's color picker.
Exercise
A stylesheet sets --brand: oklch(62% 0.19 255); in :root and uses background: #3b82f6; background: var(--brand); on a button. In a browser without oklch() support, the button has no background color. Why does the hex fallback not apply, and how do you fix it?
Show answer
A declaration that uses var() is valid at parse time, so it replaces the hex line. At computed-value time the browser cannot read the oklch() value, so the declaration becomes invalid and the hex line is not used. Fix it by defining the token in two steps: set --brand: #3b82f6; in :root, then override it with the oklch() value inside @supports (color: oklch(0% 0 0)) { :root { --brand: oklch(62% 0.19 255); } }.
Exercise
You write oklch(70% 0.4 150) on an sRGB display. What does the browser show, and which part of the color changes?
Show answer
The color is outside the sRGB gamut, so the browser gamut-maps it. Lightness and hue are kept as closely as possible, and chroma is reduced until the color fits, so the result is less vivid than written.