Sub topic 4
Writing your first @supports rule
This chapter assumes you know basic CSS syntax, how the cascade and source order settle conflicts, and how an at-rule such as @media is written. You should also be comfortable with the baseline-first approach from the earlier chapters. Here you write a working @supports rule.
What @supports does
@supports is a conditional at-rule. It holds a condition and a block of CSS rules. The browser applies the rules only if the condition is true. Otherwise it skips the whole block.
The condition is a property and value pair in parentheses:
@supports (display: grid) {
/* rules here apply only if the browser understands display: grid */
}The browser does not look at its name or version. It checks whether it can parse that declaration. If it can, the condition is true.
A baseline plus an upgrade
The pattern is the same each time. Write a baseline that works everywhere. Then put the enhancement inside @supports.
.gallery {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
.gallery > * {
flex: 1 1 12rem;
}
@supports (display: grid) {
.gallery {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(12rem, 1fr));
}
}A browser without grid support ignores the @supports block and keeps the flexbox layout. A browser with grid support applies the block. The .gallery rule inside it overrides display: flex from the baseline.
The syntax rules
The condition has a strict shape:
- The declaration needs its own parentheses, so
(display: grid)is valid. - The parentheses go around the whole declaration, not just the value.
- You do not end the declaration with a semicolon inside the condition.
- The block holds ordinary rule sets, including selectors. It does not hold bare declarations.
This version is invalid, and the browser discards the whole block:
@supports display: grid {
.gallery {
display: grid;
}
}An invalid condition is not an error you can see. The block silently never applies, which makes this a common source of confusion.
What the test actually checks
The browser checks the declaration in the condition against its own parser. This has three consequences.
- Value matters, not only property.
(display: grid)is true in browsers that know thegridkeyword.(display: subgrid)can be false in a browser that hasgridbut notsubgrid. Test the exact value you plan to use. - Parsing is not rendering. A true result means the browser accepts the syntax. It does not mean the feature works well in every situation, such as an old layout engine with bugs.
- Vendor prefixes are separate properties.
-webkit-backdrop-filterandbackdrop-filterare different names, so you test each one if you need both. Combining them is covered in the next chapter.
Source order and specificity
@supports adds no specificity. A rule inside the block competes with the baseline by the usual cascade rules. If both selectors have equal specificity, the later one wins. Put the @supports block after the baseline it upgrades.
/* Wrong order: the baseline overrides the enhancement */
@supports (display: grid) {
.gallery {
display: grid;
}
}
.gallery {
display: flex;
}In this example the grid rule is overridden in every browser. The flexbox rule comes later and has the same specificity, so it wins even where the condition is true.
Checking your work
Test both paths. In your browser's developer tools you can see which rules are active in the Styles panel. Rules inside a true @supports block appear under the @supports header. For the false path, temporarily change the property to a misspelling, such as (display: gridd). Confirm that the baseline still looks acceptable. Then restore the correct value.
Exercise
A stylesheet has .card { width: 300px; } followed by @supports display: flex { .card { display: flex; } }. The flex rule never applies, even in a modern browser. What is wrong, and how do you fix it?
Show answer
The condition is missing its parentheses. @supports needs (display: flex), and an invalid condition makes the browser drop the whole block. Write @supports (display: flex) { .card { display: flex; } }.
Exercise
You write .nav { display: grid; } and then, in a later @supports (display: grid) block, .nav { display: flex; }. Which layout do browsers with grid support show, and why?
Show answer
They show flex. The @supports block is true, and its rule comes later with equal specificity, so it overrides display: grid. @supports adds no priority of its own. Put the intended enhancement in the block, and the baseline outside it.