Sub topic 10
Common pitfalls and how to debug them
This chapter assumes you know basic CSS, the cascade and source order, and at-rules such as @media. You have seen how @supports tests a property and value pair, and how not, and and or combine conditions. Here you will learn the mistakes that make an @supports block silently do nothing, and how to find them.
Why failures are silent
CSS does not report errors. A browser that cannot parse an @supports condition treats the condition as false and skips the whole block. The page still loads and no warning appears in the console. Most debugging comes down to asking one question: did the condition evaluate to true, and did the rules inside win the cascade?
Pitfall 1: Mixing operators without parentheses
You can chain and with and, or or with or, without extra parentheses. Mixing the two operators in one level is invalid, so the whole condition is dropped.
/* Invalid: and and or at the same level */
@supports (display: grid) and (gap: 1rem) or (display: flex) {
.toolbar {
display: flex;
}
}
/* Valid: each group is wrapped */
@supports ((display: grid) and (gap: 1rem)) or (display: flex) {
.toolbar {
display: flex;
}
}The same rule applies to not. It needs its own parentheses when combined, as in not (display: grid). Writing not display: grid is invalid.
Pitfall 2: Missing parentheses around the declaration
The test must be a full declaration in parentheses. @supports display: grid and @supports (display) are both invalid. Include the property, the colon and a value.
Pitfall 3: Testing the wrong thing
@supports checks that the browser parses a property and value. It does not check that the feature behaves well. Two common consequences follow.
- A property can parse but have partial support. For example,
gapparsed in flexbox later than in grid. Testing(gap: 1rem)does not tell you which layout mode supports it. - Vendor prefixes need their own test. A browser may support only
-webkit-backdrop-filter, so test both forms withor, as the panel example earlier in this tutorial does.
Pitfall 4: Wrong order or specificity
@supports adds no specificity. A rule inside the block has the same weight as the same selector outside it, so source order decides. If the enhancement comes before the baseline, the baseline wins.
/* Wrong: the baseline overrides the enhancement */
@supports (display: grid) {
.gallery {
display: grid;
}
}
.gallery {
display: flex;
}Put the baseline first and the @supports block after it. The same ordering rule applies to the width: 300px; width: min(100%, 300px); fallback: the modern declaration must come second.
Pitfall 5: Overusing `not`
@supports not (...) is correct but tends to grow into a list of workarounds. If you find yourself writing several not blocks, restructure so that the baseline is the default and a single @supports block holds the enhancement. Baseline plus enhancement is easier to read and to remove later.
Pitfall 6: Relying on old browsers understanding `@supports`
Browsers that predate @supports ignore the entire at-rule, which is usually what you want, since they get the baseline. The risk is the reverse. If you put essential styles only inside @supports, those browsers lose them. Keep core layout and readability outside.
Debugging checklist
- Check the syntax. Look for balanced parentheses, a colon in each declaration and no unparenthesized
and/ormix. - Test the condition in the browser console with
CSS.supports(). Paste the condition string, for exampleCSS.supports('(display: grid) and (gap: 1rem)'). Afalseresult means the condition is invalid or unsupported. - Open the developer tools and inspect the element. Look at the Styles panel. A rule from a skipped block does not appear. A rule that appears but is crossed out lost the cascade.
- Check the order in the stylesheet, and whether a later rule or a more specific selector overrides the enhancement.
- Check the computed value. If the property shows the expected value, the rule applied and the problem lies elsewhere, such as the layout of a parent element.
const condition = '((display: grid) and (gap: 1rem)) or (display: flex)';
console.log(condition, '=>', CSS.supports(condition));
// Without the inner parentheses the condition is invalid, so this logs false
console.log(CSS.supports('(display: grid) and (gap: 1rem) or (display: flex)'));Exercise
This rule never applies in any browser: @supports (display: grid) and (gap: 1rem) or (display: flex) { .toolbar { display: flex; } }. What is wrong, and how do you fix it?
Show answer
The condition mixes and and or at the same level without parentheses, which makes it invalid, so the browser skips the block. Wrap each group: @supports ((display: grid) and (gap: 1rem)) or (display: flex) { ... }.
Exercise
Your @supports (display: grid) block appears in the browser's Styles panel but .gallery is still laid out with flexbox. What is the most likely cause?
Show answer
The condition is true, but the rule lost the cascade. A later declaration of display: flex on .gallery, or a more specific selector, overrides it. Move the @supports block after the baseline or fix the specificity.