Sub topic 11
Browser support and progressive enhancement
This chapter assumes you know basic CSS syntax, custom properties, and the oklch(), color-mix() and relative color syntax forms from earlier chapters. It shows how to ship them to browsers that may not understand them yet.
How CSS handles values it does not understand
A browser that cannot parse a declaration drops it and keeps the previous valid one. That rule is the basis of the simplest fallback: write the old value first and the modern value after it.
.button {
background: #3b82f6;
background: oklch(62% 0.19 255);
}An old browser ignores the second line and uses the hex color. A browser with support uses the second line because it comes later in the cascade.
Support differs by feature
The three features were not released together, so check each one on its own.
| Feature | What to guard against |
|---|---|
| `oklch()` | Older browsers drop the declaration |
| `color-mix()` | Older browsers drop the declaration |
| Relative color syntax (`from`) | Often arrives later than the other two |
Check a current compatibility table such as caniuse.com or MDN before you decide how far back to support. Your analytics and your project's browser targets should drive the decision.
The custom property trap
The two-line fallback has a limit when you put colors in custom properties. A custom property accepts almost any value, so --brand: oklch(62% 0.19 255); is valid even in a browser that cannot render it. The failure shows up later, when var(--brand) is substituted into a property. The declaration then becomes invalid at computed-value time. The browser does not go back to your earlier fallback line. The property falls back to its inherited or initial value, often transparent for background.
:root {
--brand: #3b82f6;
}
@supports (color: oklch(0% 0 0)) {
:root {
--brand: oklch(62% 0.19 255);
}
}
.button {
background: var(--brand);
}Here the token holds the hex value by default. Only browsers that support oklch() replace it. Every rule that uses var(--brand) then gets a color the browser can render.
Guarding rules with @supports
@supports tests whether the browser can parse a declaration. Use it for features where a one-line fallback does not work, such as color-mix().
.button {
background: #3b82f6;
background: oklch(62% 0.19 255);
}
@supports (color: color-mix(in oklch, red, blue)) {
.button:hover {
background: color-mix(in oklch, var(--brand), black 15%);
}
}Without support, the hover rule is skipped and the button keeps its base color. The page still works, only without the hover effect. That is progressive enhancement: the baseline works everywhere, and capable browsers get more.
Testing relative color syntax
Relative color syntax needs its own check, because a browser may support oklch() and not the from keyword.
@supports (color: oklch(from red l c h)) {
.button {
background: oklch(from var(--brand) calc(l - 0.1) c h);
}
}Place the base style outside the block so the element still has a color when the test fails.
Colors outside the display gamut
Support for the syntax is separate from support for the color. If a browser parses oklch(70% 0.35 150) but your screen cannot show that chroma, the browser maps it to the nearest displayable color. This is called gamut mapping. The result is usually fine, but two different out-of-range values can end up looking alike. Keep chroma within a range your target displays can show, and compare the result on a real screen.
Common mistakes
- Putting the fallback after the modern value. The later declaration wins, so the old browser would still see only the hex value, and a modern browser would use the fallback.
- Assuming
@supportsfor one feature covers another. Testoklch(),color-mix()andfromseparately. - Using a modern color only inside a custom property and expecting the fallback line to apply.
- Skipping a test in at least one older browser or with a browser's developer tools.
Exercise
A button uses background: var(--brand); and --brand is set to oklch(62% 0.19 255) with no other rules. In a browser that cannot parse oklch(), what happens, and how would you fix it?
Show answer
The custom property is valid, but when var(--brand) is substituted, the background declaration becomes invalid at computed-value time. The browser does not use any earlier fallback, so the button gets the initial value, which is transparent. Fix it by defining --brand as a hex value first, then overriding it inside @supports (color: oklch(0% 0 0)) { :root { --brand: oklch(62% 0.19 255); } }.
Exercise
Why might you wrap a color-mix() hover rule in @supports instead of using a two-line fallback?
Show answer
The hover color often depends on var(), so a two-line fallback inside the rule would not help in a browser that lacks color-mix(). With @supports, the whole hover rule is skipped, and the element keeps its base color.