Sub topic 10
Common pitfalls and how to debug them
This chapter assumes you can already write a basic @supports rule, combine conditions with not, and and or, and use selector(). It also assumes you know that source order decides between declarations of equal specificity. The chapter lists the mistakes that cause most @supports bugs. It then gives a routine for finding the cause when a rule doesn't apply.
The core idea: a bad condition fails silently
@supports never reports an error. A condition is true, false, or unparseable. The browser treats an unparseable condition as false and skips the block. Because nothing is logged, a typo looks the same as a browser that lacks the feature. Most bugs in this chapter come from that.
Pitfall 1: Mixing operators without parentheses
You can chain several conditions with the same operator. You can't mix and with or at one level, and you can't follow not with another operator at the same level.
/* Invalid: and and or at the same level. The block never applies. */
@supports (display: grid) and (gap: 1rem) or (display: flex) {
.toolbar {
display: flex;
gap: 0.5rem;
}
}
/* Valid: each group is wrapped explicitly. */
@supports ((display: grid) and (gap: 1rem)) or (display: flex) {
.toolbar {
display: flex;
gap: 0.5rem;
}
}The same rule applies to not. Write not ((a) and (b)) or (not (a)) and (b). Don't write not (a) and (b).
Using a single operator type across a whole condition never needs the extra parentheses. When you combine operator types, add parentheses even where you think you could skip them.
Pitfall 2: Missing parentheses and missing spaces
Each declaration test needs its own parentheses.
/* Invalid: the declaration is not wrapped. */
@supports display: grid {
.layout { display: grid; }
}
/* Valid */
@supports (display: grid) {
.layout { display: grid; }
}Keywords need whitespace before the parenthesis. not(display: grid) is read as a function-like token named not, not as the keyword followed by a condition, so the condition is invalid. Write not (display: grid). Do the same for and ( and or (.
The declaration inside the parentheses must be a complete property and value pair. (display) alone is invalid. (display: grid) is valid.
Pitfall 3: Wrong order between baseline and enhancement
@supports adds no specificity. Rules inside it are ordinary rules, so they follow the usual cascade. If the enhancement comes before a baseline rule of equal specificity, the baseline wins and the enhancement seems to do nothing.
/* Bug: the later baseline overrides the enhancement. */
@supports (display: grid) {
.gallery {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(12rem, 1fr));
}
}
.gallery {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}In this example .gallery is display: flex in every browser, including those that support grid. Put the baseline first and the @supports block after it.
.gallery {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
@supports (display: grid) {
.gallery {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(12rem, 1fr));
}
}The same ordering rule applies to fallbacks that rely on invalid declarations. In a pair like width: 300px; width: min(100%, 300px); the fallback must come first. If you reverse them, a browser that understands min() still works, but you've lost the fallback. If you later reorder these lines for tidiness, you can break older browsers without noticing.
A related trap is a more specific selector in the baseline. If the baseline is .page .gallery and the enhancement is .gallery, the baseline wins regardless of order. Keep both selectors the same, or raise the enhancement's specificity deliberately.
Pitfall 4: Testing the property when you need the value
@supports (display: grid) tells you the browser parses display: grid. It says nothing about newer grid features. A browser can support grid and not support subgrid or masonry. Test the exact value your code depends on.
.card-list {
display: grid;
grid-template-columns: repeat(3, 1fr);
}
.card {
display: block;
}
@supports (grid-template-rows: subgrid) {
.card {
display: grid;
grid-row: span 3;
grid-template-rows: subgrid;
}
}If your enhancement uses two features, test both with and. A condition that checks only one of them gives you a half-enhanced layout in browsers that have one feature but not the other.
Support is not the same as correct behavior
@supports answers one question: does the browser parse this declaration? It does not tell you whether the implementation is complete or free of bugs. A browser can parse a property and still render it wrongly in some cases. When you hit a known bug, @supports can't isolate the affected browsers. Fix it in the baseline, or make the design tolerant of the difference.
Pitfall 5: Forgetting prefixed forms
Some properties exist in a prefixed form in one browser and an unprefixed form in another. A condition that checks only one form leaves the other browser on the baseline.
.panel {
background: rgb(255 255 255 / 0.95);
}
@supports (backdrop-filter: blur(8px)) or (-webkit-backdrop-filter: blur(8px)) {
.panel {
background: rgb(255 255 255 / 0.6);
-webkit-backdrop-filter: blur(8px);
backdrop-filter: blur(8px);
}
}Inside the block, declare both forms. Browsers ignore the one they don't understand. Check current browser support before adding prefixes, because they go out of date and you should remove them when they are no longer needed.
The baseline here is nearly opaque, so text stays readable when the blur is missing. Choose the baseline so that the page works when the enhancement is absent. If the enhanced rule makes text unreadable without the blur, the check is protecting the wrong thing.
Pitfall 6: Relying on `@supports not` for old browsers
@supports not (...) applies a workaround only when the feature is missing:
@supports not (display: grid) {
.layout {
float: left;
width: 50%;
}
}A browser that doesn't know @supports at all ignores the entire block, including the not case. Those are often the same old browsers you meant to help. Internet Explorer 11 is a well-known example. The workaround never runs there.
The reliable pattern is a baseline plus an enhancement. Put the old-browser styles in the plain rule, and let @supports (display: grid) override them. A browser without @supports stays on the baseline, which is what you want. Reserve not for cases where you must remove a workaround because a modern feature replaces it.
.layout {
float: left;
width: 50%;
}
@supports (display: grid) {
.layout {
float: none;
width: auto;
display: grid;
}
}Remember to reset every baseline property that conflicts with the enhancement. A leftover float or width is a common source of odd layouts.
Pitfall 7: Mismatch between CSS and JavaScript checks
CSS.supports() uses the same parser, so the answers agree, but the call shape matters. The two-argument form takes a property and a value without parentheses. The one-argument form takes a condition, so it needs parentheses around each declaration and the selector() function for selectors.
// Two-argument form: property, then value.
console.log(CSS.supports('display', 'grid'));
// One-argument form: a full condition with parentheses.
console.log(CSS.supports('(display: grid) and (gap: 1rem)'));
// Selectors use the selector() function.
console.log(CSS.supports('selector(:has(a))'));
// A typo returns false, not an error.
console.log(CSS.supports('display', 'gird'));A script and a stylesheet can disagree if you test different things. If the script adds a class when display: grid is supported but the stylesheet uses subgrid, the class is true in browsers where the CSS fails. Test the same condition string in both places.
A debugging routine
When an @supports rule seems to have no effect, work through the causes from cheapest to most expensive.
- Test the condition in the console. Run
CSS.supports('(your condition)')in the browser console. Afalseresult means the browser doesn't support it or the condition is invalid. Remove operators one at a time until it returnstrue, and you've found the broken part. - Add a marker to the block. Put a visible declaration such as
outline: 3px solid red;inside the block. If the outline appears, the condition passes and the problem lies in your actual declarations. If it doesn't, the block isn't being applied. - Inspect the cascade. In your browser's DevTools Styles pane, find the element and look at which declaration wins. A struck-through declaration means another rule overrode it. Compare the order and specificity of the baseline and the enhancement. How DevTools shows rules from a block with a false condition varies between browsers, so an absent rule is a hint to check the condition and not proof of a bug.
- Check the surrounding syntax. An unclosed brace earlier in the stylesheet can swallow the block. A validator or your editor's formatter shows this quickly.
- Test with the feature off. Temporarily change the condition to something false, such as
(display: not-a-value). The page should still work with the baseline alone. If it doesn't, the baseline depends on the enhancement.
Symptom table
| Symptom | Likely cause | Fix |
|---|---|---|
| Block never applies, console check is false | Mixed `and`/`or` without parentheses | Wrap each group in parentheses |
| Block never applies, console check is false | `not(` with no space, or a missing declaration parenthesis | Add a space after the keyword and wrap the declaration |
| Enhancement is overridden everywhere | Baseline placed after the `@supports` block | Move the baseline first |
| Enhancement works in one browser only | Only one prefixed form tested | Test both forms with `or` |
| Half-enhanced layout | Only the property was tested, not the value | Test the exact value the code needs |
| Workaround missing in very old browsers | Workaround written inside `@supports not` | Put it in the baseline and override it in an enhancement |
| Script and stylesheet disagree | Different conditions in JavaScript and CSS | Use the same condition string |
Exercise
This rule never applies in any browser. Find the problem and rewrite it so it applies where both grid and gap are supported, or where flexbox is supported. @supports (display: grid) and (gap: 1rem) or (display: flex) { .toolbar { gap: 0.5rem; } }
Show answer
The condition mixes and with or at one level, so it is invalid and treated as false. Wrap the groups: @supports ((display: grid) and (gap: 1rem)) or (display: flex) { .toolbar { gap: 0.5rem; } }
Exercise
A page has a flex baseline for .gallery in a rule that appears after an @supports (display: grid) block that sets display: grid on .gallery. The page always renders as flex. Why, and what is the fix?
Show answer
Both rules have the same specificity, and @supports adds none, so the later baseline wins by source order. Move the baseline rule above the @supports block.
Exercise
You write @supports not (display: grid) { ... } to give float styles to old browsers. Why might those styles never reach the oldest browsers, and what is a more reliable structure?
Show answer
Browsers that don't support @supports at all skip the whole block, including the not case. Put the float styles in the plain baseline rule and override them inside @supports (display: grid).