Sub topic 11
Browser support and progressive enhancement
This chapter assumes you know basic CSS syntax and selectors and can use custom properties. It also assumes you have read the earlier chapters on oklch(), color-mix() and relative color syntax. Here you will learn how to ship those features to browsers that may not understand them, without breaking the page.
What "support" means for color features
The three features in this tutorial arrived at different times.
| Feature | What it needs from the browser | Typical rollout |
|---|---|---|
| `oklch()` | Parsing a new color function | Widely available since 2023 |
| `color-mix()` | Parsing a function and mixing in a named color space | Widely available since 2023 |
| Relative color syntax (`oklch(from ...)`) | Parsing the `from` keyword and channel keywords such as `l`, `c`, `h` | Later than the other two, with Firefox last |
Treat the rollout column as a rough guide. Exact version numbers change as browsers ship, so check a compatibility table such as caniuse.com or MDN's browser compatibility data before you decide on your support policy.
The practical result is that some visitors have oklch() and color-mix() but not relative colors. A few have none of them. Your CSS should degrade in steps, not fail all at once.
Progressive enhancement in one sentence
Progressive enhancement means you write a working baseline first, then layer better features on top. Browsers that understand the newer feature use it. Browsers that don't keep the baseline.
For color, the baseline is a hex or rgb() value. The enhancement is oklch(), color-mix() or relative color syntax.
How CSS handles values it does not understand
CSS has a built-in fallback rule. When the parser meets a declaration it cannot parse, it drops that declaration and continues. The earlier declaration of the same property stays in effect.
.button {
background: #3b82f6;
background: oklch(62% 0.19 255);
}An old browser cannot parse oklch(62% 0.19 255), so it throws that line away and uses #3b82f6. A modern browser reads both lines, and the later one wins by source order.
Three rules follow from this:
- Put the fallback first and the enhanced value second.
- Keep both declarations in the same rule, or in rules of equal specificity, so source order decides.
- Choose a fallback that is close to the enhanced color. It does not need to be identical, but it should keep the same role and enough contrast.
You can get a close hex value by converting your oklch() color with a tool, or by reading the computed value in DevTools. Most browsers show an equivalent sRGB value in the color picker.
The trap: fallbacks do not work with `var()`
The two-line pattern breaks when the enhanced declaration contains var(). A declaration with var() in it is accepted at parse time, because the browser cannot know what the variable holds yet. It is checked later, when the variable is substituted. If the result is not a valid color, the declaration becomes invalid at computed-value time. The browser does not go back to the earlier declaration. It treats the property as unset, which for background usually means transparent.
:root {
--brand: #3b82f6;
}
.button {
background: #2563eb;
/* In a browser without color-mix(), this line is accepted at parse
time, then fails when var() is substituted. The hex fallback above
is NOT restored. */
background: color-mix(in oklch, var(--brand), black 15%);
}The same happens if --brand itself holds an oklch() value and the browser does not understand it. The var() call is the problem, not the function name.
Guarding with `@supports`
@supports asks the browser whether it can parse a declaration. If it can't, the whole block is skipped. This works with var() because the test uses literal values, not variables.
:root {
--brand: #3b82f6;
}
.button {
background: var(--brand);
}
@supports (color: oklch(62% 0.19 255)) {
:root {
--brand: oklch(62% 0.19 255);
}
}
@supports (color: color-mix(in oklch, red, blue)) {
.button:hover {
background: color-mix(in oklch, var(--brand), black 15%);
}
}This is the pattern to prefer in token-based stylesheets:
- Define the token with a safe value at the top.
- Override the token inside an
@supportsblock when the better syntax exists. - Write components against the token and let them stay unaware of support.
The hover rule is skipped in browsers without color-mix(), so those users simply get no hover color change. That is an acceptable degradation. You can also add a plain hover rule before the @supports block to give them a fixed hover color.
Testing each feature
Write the test with the smallest valid example of the feature.
/* oklch() */
@supports (color: oklch(0.6 0.15 250)) {
/* ... */
}
/* color-mix() */
@supports (color: color-mix(in oklch, red, blue)) {
/* ... */
}
/* relative color syntax */
@supports (color: oklch(from red l c h)) {
/* ... */
}Test relative color syntax separately. A browser that passes the oklch() test may still fail the third one. Do not use one query to stand in for all three.
You can also invert a test with not, which is useful for a legacy-only block.
@supports not (color: oklch(from red l c h)) {
:root {
--brand-dark: #1d4ed8;
}
}Layering three tiers
A real stylesheet often has three tiers. The next diagram shows how a browser moves through them.
Here is that structure as CSS.
:root {
/* Tier 1: baseline, works everywhere */
--brand: #3b82f6;
--brand-hover: #2563eb;
--brand-tint: #e8f0fe;
}
/* Tier 2: oklch() and color-mix() */
@supports (color: oklch(0.6 0.15 250)) {
:root {
--brand: oklch(62% 0.19 255);
--brand-hover: oklch(52% 0.19 255);
--brand-tint: color-mix(in oklch, var(--brand) 15%, white);
}
}
/* Tier 3: relative color syntax */
@supports (color: oklch(from red l c h)) {
:root {
--brand-hover: oklch(from var(--brand) calc(l - 0.1) c h);
}
}
.button {
background: var(--brand);
color: white;
}
.button:hover {
background: var(--brand-hover);
}
.card {
background: var(--brand-tint);
}Every @supports block only overrides tokens. The components at the bottom never change. That keeps the support logic in one place, and you can delete a tier later when your browser targets move forward.
There is a subtle point in tier 2. The --brand-tint token uses var(--brand) and color-mix() together. Because the whole override is inside the color-mix() test, a browser without color-mix() never sees it. But the oklch() test passes for a browser that has oklch() and lacks color-mix(). If you need to support that combination, give color-mix() its own @supports block.
@supports (color: color-mix(in oklch, red, blue)) {
:root {
--brand-tint: color-mix(in oklch, var(--brand) 15%, white);
}
}Out-of-gamut colors and display capability
Support is also about hardware. An oklch() color can describe a color the screen cannot show. The browser then maps it into the display's gamut, which is the range of colors a screen can produce. The earlier chapter on gamut covers how this mapping works. Here the concern is deciding what to ship.
The color-gamut media query tells you whether the display can show wide-gamut colors.
:root {
--accent: oklch(65% 0.2 145);
}
@media (color-gamut: p3) {
:root {
--accent: oklch(70% 0.27 145);
}
}The first value stays inside sRGB for most displays. The second value has higher chroma and is only used when the display can show Display P3 colors. Both values are valid on any browser that supports oklch(), so this block is about the screen, not about parsing.
Be careful with the high-chroma value. On a screen that matches p3, the color may still be outside the display. If so, the browser maps it back. Check the result on real hardware, not just in the editor.
Build tools as an alternative
You do not have to write every fallback by hand. Tools can compile modern color syntax into older syntax during your build.
- PostCSS plugins such as
postcss-preset-envcan convertoklch()to a fallbackrgb()line. They work best when the input colors are static values. - Lightning CSS can transform colors based on the browser targets you give it.
These tools handle the simple case well. They cannot compute a color that depends on a runtime value such as a custom property that changes under a theme class or a media query. For color-mix(in oklch, var(--brand), black 15%), the build tool does not know what --brand will be. In that case you still need the @supports approach, or you need to precompute tokens.
A reasonable decision rule:
| Situation | Approach |
|---|---|
| Static colors, many legacy browsers | Build tool adds fallbacks |
| Token-based theme with runtime variables | `@supports` token overrides |
| Modern-only audience | Use the new syntax directly |
Checking what a browser actually does
Do not trust assumptions. Verify each tier.
- Open the page in a browser that supports everything and confirm the tier 3 values apply. In DevTools, look at the computed value of
--brand-hover. - Test with an older browser, or a recent browser with the feature behind a flag, if you can.
- Temporarily change the
@supportstest to something invalid, such as(color: oklch(nonsense)), and reload. This forces your fallback path in any browser. Remember to revert it. - Check that text on every tier still has enough contrast. A fallback hex that is lighter than the
oklch()value can quietly fail accessibility checks. The previous chapter on contrast explains how to measure it.
Common mistakes
- Putting the fallback after the modern declaration. The later line always wins in a browser that understands both.
- Relying on declaration-order fallback when the modern line contains
var(). Use@supportsinstead. - Testing only
oklch()and then usingcolor-mix()or relative colors in the same block. - Forgetting that
@supportsonly tests parsing. It does not tell you the color will look the same on every display. - Using a very different fallback color, so that the baseline page has a visibly different brand.
- Leaving old
@supportsblocks forever. When your support policy drops the legacy browsers, remove the baseline and the tests.
Exercise
A stylesheet has .button { background: #2563eb; background: color-mix(in oklch, var(--brand), black 15%); }. In a browser that does not support color-mix(), the button has no background. Why does the hex fallback not apply, and how do you fix it?
Show answer
The second declaration contains var(), so the browser accepts it at parse time and only finds it invalid when it substitutes the variable. A declaration that is invalid at computed-value time does not restore the earlier declaration. The property becomes unset instead. The fix is to wrap the color-mix() rule in @supports (color: color-mix(in oklch, red, blue)) and keep the hex background as a normal rule outside the block, so unsupported browsers never see the var() declaration.
Exercise
Write CSS that sets --surface to #f1f5f9 by default, then to oklch(96% 0.01 250) in browsers that support oklch(). Then add a second block that sets --surface-strong to oklch(from var(--surface) calc(l - 0.08) c h) only where relative color syntax is supported.
Show answer
| :root { --surface: #f1f5f9; --surface-strong: #dbe3ec; } @supports (color: oklch(0.96 0.01 250)) { :root { --surface: oklch(96% 0.01 250); } } @supports (color: oklch(from red l c h)) { :root { --surface-strong: oklch(from var(--surface) calc(l - 0.08) c h); } }
Exercise
Why can a build tool such as PostCSS not fully replace @supports for color-mix(in oklch, var(--brand), black 15%)?
Show answer
The build tool runs before the page loads, so it cannot know the final value of --brand, which may change per theme or per media query at runtime. It can only convert colors it can compute statically. A runtime-dependent mix needs either an @supports override or precomputed tokens.