Sub topic 7
Practical enhancement patterns
This chapter assumes you know basic CSS syntax, how source order and the cascade resolve conflicts, and how @media works. Here you apply @supports to three common cases: grid layout, backdrop-filter, and :has(). Each case follows the same recipe. Write a baseline that works everywhere, then add the enhancement inside a feature query.
The recipe
- Write the baseline rule with no conditions. Every browser reads it.
- Add an
@supportsblock after it. Put only the changes that the new feature needs inside it. - Keep the baseline usable on its own. The enhancement should improve the page, not rescue it.
Pattern 1: flexbox baseline, grid enhancement
A gallery of cards can wrap with flexbox in any modern browser. Grid gives tidier columns, so it is the upgrade.
.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));
}
}In a browser with grid, .gallery becomes a grid container. The flex declarations on the children stay in place, but they have no effect on grid items, so you do not need to undo them.
Pattern 2: opaque panel, frosted-glass enhancement
The backdrop-filter property blurs whatever sits behind an element. A translucent panel with no blur can be hard to read, so the baseline must be nearly opaque.
.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);
}
}Two details matter here. The condition tests both the prefixed and unprefixed property, because some browsers shipped only the prefixed one for a long time. The text also stays legible in both cases, because the background only becomes more transparent when the blur is available to compensate.
Pattern 3: parent styling with :has()
The :has() selector matches an element based on what it contains. A browser that cannot parse it would discard the whole rule, so the rule is safe even without a query. The selector() function makes the intent explicit and keeps related rules together.
.card {
border: 1px solid #ccc;
}
@supports selector(:has(img)) {
.card:has(img) {
border-color: rebeccapurple;
}
}Cards with an image get a purple border where :has() works. Elsewhere, every card keeps the gray border.
Choosing between @supports and the cascade
Not every enhancement needs a feature query. The earlier chapter on the cascade showed that a browser drops a declaration it cannot parse. Use that when one property is enough.
.card {
width: 300px;
width: min(100%, 300px);
}| Situation | Better tool |
|---|---|
| One property with a fallback value | Two declarations in the cascade |
| Several properties change together | `@supports` block |
| A different `display` mode changes how children behave | `@supports` block |
| A new selector | `@supports selector()` |
Use @supports when the enhancement touches several declarations that must succeed or fail as a group. A half-applied enhancement is often worse than none.
Using not for targeted workarounds
You can invert a test with not to patch only the browsers that lack a feature.
@supports not (display: grid) {
.layout {
float: left;
width: 50%;
}
}This works, but it moves the old-browser code into a special case that is easy to forget. A baseline plus an enhancement is usually clearer, because the baseline is the code every visitor can rely on. Reserve not for cases where the workaround would otherwise break the enhanced layout.
Guarding a grouped enhancement
If an enhancement needs two features, test both. Mixing and with or requires parentheses around each group, or the whole condition is invalid and the block never applies.
@supports ((display: grid) and (gap: 1rem)) or (display: flex) {
.toolbar {
display: flex;
gap: 0.5rem;
}
}Exercise
A .banner has background: white; as its baseline. You want a translucent blurred background only when backdrop-filter is supported. Where should the background: white; declaration go, and why?
Show answer
Outside the @supports block, before it. It is the baseline that every browser applies. The @supports (backdrop-filter: blur(8px)) block then overrides the background and adds the blur only in browsers that pass the test. If the baseline were inside the block, unsupported browsers would get no background at all.
Exercise
Which is the better choice for providing a fallback for width: min(100%, 300px): an @supports block or a preceding width: 300px; declaration? Explain.
Show answer
A preceding width: 300px; declaration. Only one property is involved, and a browser that cannot parse min() drops that declaration as invalid and keeps the earlier one. An @supports block adds code without adding any benefit.