Sub topic 7
Gamut Limits and Fallbacks
This chapter assumes you can write oklch() colors, use custom properties, and read @media and @supports rules. Here you will learn what happens when a color is too vivid for the screen, and what to ship for browsers that do not know oklch() at all.
What a gamut is
A gamut is the set of colors a display or color space can produce. Every screen has physical limits. The oldest and most common gamut on the web is sRGB. Many current phones, laptops and monitors can show Display P3, which covers roughly a quarter more colors than sRGB, mostly in saturated reds and greens. Rec2020 is larger still, and few screens reach it.
Hex, rgb() and hsl() are all defined in sRGB, so every color you write with them is inside the sRGB gamut. oklch() is different. It describes colors by how people perceive them, not by the three sRGB channel values. Its coordinates can therefore name colors that no screen can show.
Two things follow from this:
oklch()can reach P3 colors, which hex cannot. This is an advantage.oklch()also lets you write colors that no display can show, and you need to know what the browser does with them.
How much chroma fits
Chroma is the "amount of color" axis. In CSS it is a number with no fixed upper bound. In practice, real colors stay below about 0.4. A percentage also works, where 100% equals 0.4.
The chroma you can use depends on lightness and hue. Near white (lightness 100%) and near black (0%), almost no chroma is available. The most vivid colors sit at different lightness values for different hues. Blue is most vivid when fairly dark, and yellow when very light.
The sRGB primaries show this clearly. Approximate values:
| sRGB color | Lightness | Chroma | Hue |
|---|---|---|---|
| Pure red | 62.8% | 0.258 | 29 |
| Pure green | 86.6% | 0.295 | 142 |
| Pure blue | 45.2% | 0.313 | 264 |
A chroma of 0.25 is fine for red at 63% lightness. The same chroma on blue at 90% lightness is far outside sRGB, because very light blues are nearly pastel. You cannot pick one "safe" chroma for all lightness and hue combinations. Light and dark shades of a hue usually need less chroma than mid tones.
What the browser does with out-of-gamut colors
Suppose you write this:
.alert {
background: oklch(90% 0.3 260);
}No display can show a light blue that saturated. The browser does not reject the declaration. It keeps the value as written and converts it to something the display can show when it paints. This conversion is called gamut mapping.
The CSS Color 4 specification describes a gamut mapping algorithm that works in OKLCH. It keeps lightness and hue and lowers chroma step by step until the color fits the display gamut, allowing a difference too small to notice. This keeps the hue and lightness you chose, which a plain clip of channel values does not. Clipping each RGB channel to the 0 to 1 range is faster, but it can shift hue and flatten distinct colors into the same value.
Treat the mapping as a safety net, not as a design tool. There are three reasons:
- Different browsers and versions may map in slightly different ways. Older implementations clip.
- Two different out-of-gamut colors can map to nearly the same on-screen color. A ramp such as
oklch(70% 0.30 150)tooklch(70% 0.35 150)may look like one flat swatch on an sRGB screen. - Contrast and visual rhythm in your palette are calculated for the colors you wrote, not for the mapped ones.
The same color on a P3 screen will look more vivid than on an sRGB screen. Both are correct.
Staying inside sRGB on purpose
If your brand colors must look the same on every screen, keep them inside sRGB. Two practical ways:
- Pick colors in a tool that shows the sRGB boundary, then copy the
oklch()value. - Convert an existing hex brand color to
oklch()and keep its L, C and H as the source of truth. A hex value is always in gamut, so a conversion is also in gamut.
When you build tints and shades from one base, lower chroma as you move toward white or black. A common rule is to reduce chroma by a fraction as lightness moves away from the middle. This avoids out-of-range values that the browser would have to map.
Using wider gamuts when the screen has them
You can offer more vivid colors only on screens that can show them. The color-gamut media feature tests this. It matches srgb, p3 or rec2020 and is true when the display can show roughly that range.
:root {
--accent: oklch(70% 0.2 150);
}
@media (color-gamut: p3) {
:root {
--accent: oklch(75% 0.25 150);
}
}Every screen gets a green that is inside sRGB. Screens that can show P3 get a more vivid one. The base value is the default and the media query only upgrades it, so the rule order matters: put the @media block after the default.
Note that color-gamut tests the display, not the browser's color support. A browser that does not understand oklch() at all still needs a separate fallback, covered next.
Fallbacks for browsers without oklch()
Browsers that predate oklch() ignore declarations they cannot parse. The CSS rule that an invalid declaration is dropped gives you a simple fallback. Write a hex value first and the oklch() value second:
.button {
background: #3b82f6;
background: oklch(62% 0.19 255);
}An old browser drops the second line and keeps the hex. A modern browser uses the later declaration, because later declarations win at equal specificity.
Why this fails with var()
The two-line trick breaks when the value comes from a custom property:
:root {
--brand: oklch(62% 0.19 255);
}
.button {
background: #3b82f6;
background: var(--brand);
}A browser without oklch() support accepts --brand as any text. It also accepts background: var(--brand) as syntactically valid, because the browser cannot tell at parse time what a var() will contain. Only when it substitutes the value does it find that oklch(...) is not a valid color. At that point the declaration is "invalid at computed-value time". The browser does not go back to the hex line. It uses the property's inherited or initial value, which for background is transparent.
The fix is to choose the value in the custom property itself with @supports:
:root {
--brand: #3b82f6;
}
@supports (color: oklch(0% 0 0)) {
:root {
--brand: oklch(62% 0.19 255);
}
}
.button {
background: var(--brand);
}The @supports condition asks whether the browser can parse oklch(0% 0 0) as a color. If it can, --brand is overridden. If it cannot, the whole block is skipped. The rest of the stylesheet uses var(--brand) and does not need to care.
Guarding color-mix() and relative colors
Support for oklch(), color-mix() and relative color syntax came in different browser versions, so one check does not cover all three. Test the feature you use:
:root {
--brand: #3b82f6;
--brand-hover: #2563eb;
}
@supports (color: color-mix(in oklch, red, blue)) {
:root {
--brand-hover: color-mix(in oklch, var(--brand), black 15%);
}
}
@supports (color: oklch(from red l c h)) {
:root {
--brand-border: oklch(from var(--brand) l c h / 0.4);
}
}Each block falls back independently. If a block is skipped, that variable stays at the value you defined before it, or undefined if you did not define one. Define a plain default for every variable you override, or the dependent rules will break.
There is a related trap: --brand here is already the oklch() or hex value chosen by the first block, and color-mix() accepts either. A relative color accepts either too, but the channels it reads (l, c, h) are those of whatever color it receives. That is fine, but remember that a hex fallback and its oklch() replacement are not the same color to the last digit.
Checking support from JavaScript
You sometimes need the same checks in script, for example to pick a canvas fill color. Two APIs cover it:
const supportsOklch = CSS.supports("color", "oklch(60% 0.2 250)");
const hasP3 = window.matchMedia("(color-gamut: p3)").matches;
console.log({ supportsOklch, hasP3 });CSS.supports() returns true if the browser can parse the value for that property. matchMedia returns the same answer as the @media rule. The matches flag can change if the user moves the window to another monitor, so listen to the change event on the returned list if that matters for your page.
Common mistakes
- Assuming a large chroma is more vivid on every screen. Past the gamut edge, extra chroma changes nothing on an sRGB display. You gain nothing and lose predictability.
- Using the same chroma for every shade. A 0.2 chroma at 95% lightness is usually out of gamut. Reduce chroma toward the ends of the lightness scale.
- Relying on
var()for a declaration fallback. As shown above, it leaves you with a transparent background in old browsers. - Testing only on a P3 laptop. Check the sRGB result too. Use your browser's developer tools to emulate a different gamut if it supports that, or look at the page on a standard monitor.
- Treating the fallback as a design target. The hex is a safety net. Pick it by converting your
oklch()base color and keep the two in sync when the brand changes.
Exercises
Exercise
A stylesheet has --brand: oklch(62% 0.19 255); in :root, and a rule .btn { background: #3b82f6; background: var(--brand); }. A user opens the page in a browser that does not support oklch(). What background does the button get, and how would you fix it?
Show answer
The button background is transparent (the initial value), not the hex. The browser accepts var(--brand) at parse time, finds an invalid color only when it substitutes the value, and does not return to the hex line. Fix it by defining --brand: #3b82f6; first, then overriding it with oklch(62% 0.19 255) inside @supports (color: oklch(0% 0 0)) { :root { ... } }, and using only background: var(--brand) in the rule.
Exercise
You write oklch(95% 0.25 260) for a very light blue background. On an sRGB display, what does the browser do, and what should you change in your design?
Show answer
The color is outside the sRGB gamut, so the browser applies gamut mapping. It lowers chroma while keeping lightness and hue, then paints the closest displayable color. You should lower the chroma yourself, for example to about 0.03 to 0.05 for a very light tint, so the color you wrote is the color users see, and so contrast checks apply to the real value.