Sub topic 7
Practical enhancement patterns
This chapter assumes you know basic CSS syntax, how source order and specificity resolve conflicts, and how @supports and its not, and and or operators work. Here you will apply them to three common patterns: a grid layout, a translucent blurred panel, and a :has() parent style. You will also learn a checklist for deciding whether a new feature needs a guard at all.
The shape of every pattern
Each pattern in this chapter has the same three parts.
- A baseline that works in every browser you support. It is plain CSS with no guard.
- An enhancement, placed inside
@supports, that replaces or refines part of the baseline. - A check that the baseline is acceptable without the enhancement.
The third part matters most. If the page is broken without the enhancement, the enhancement is not an enhancement. It is a requirement, and you should treat it that way.
Pattern 1: flexbox baseline, grid enhancement
A gallery of cards needs to wrap onto several rows. Flexbox does this in every current browser, so it is the baseline. Grid gives tidier columns, so it is the enhancement.
.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));
}
}The @supports block comes after the baseline. It has the same specificity as the rule above it, so source order decides, and the enhancement wins wherever it applies.
The leftover flex declarations
When the grid rule applies, the flex: 1 1 12rem declaration on the children is still in the stylesheet. Grid items ignore flex properties, because those only apply to children of a flex container. The leftover declaration is harmless here. Be careful with properties that do have an effect in the other layout mode. Examples are float, width and margin. A width set for the flex baseline can shrink a grid item inside its cell.
If a baseline property does leak into the enhanced layout, reset it inside the @supports block.
.sidebar-item {
float: left;
width: 50%;
}
@supports (display: grid) {
.sidebar-item {
float: none;
width: auto;
}
}Why test display: grid at all
Grid has been available in all major browsers since 2017. Many teams skip the guard for basic grid. The guard still earns its place in two cases. The first is when your support policy includes older browsers. The second is when the baseline is a different layout that would conflict with grid properties. Decide by your support policy, not by habit. Section "When not to use @supports" below gives the criteria.
Pattern 2: opaque baseline, translucent enhancement
A frosted-glass panel needs backdrop-filter. Without it, a translucent background lets the content behind the panel show through unblurred, and the text on the panel becomes hard to read. So the baseline must be nearly opaque, and only the enhanced version may be translucent.
.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);
}
}The ordering of the idea is what matters here. The risky property, the low alpha background, lives only inside the guard. It is never applied without the blur that makes it legible.
Why the condition uses or
Some browsers shipped backdrop-filter only with a -webkit- prefix for a long time. The condition accepts either form with or. Inside the block, the prefixed declaration comes first and the unprefixed one last. A browser that understands both uses the standard property, because it is later in source order.
Notice that the condition has only one operator, or, between two parenthesized tests. That keeps the parentheses simple. If you add and to the same level, you must group explicitly, as the previous chapter explained.
The text contrast trap
You can only guard what the browser can detect. @supports tells you that backdrop-filter parses. It does not tell you that the blur is cheap on a low-end phone, or that the content behind the panel is calm enough for 0.6 alpha to stay readable. Check contrast against the worst background the panel can sit over. If it fails, raise the alpha in the enhanced rule.
Pattern 3: a :has() enhancement with selector()
The :has() pseudo-class selects an element based on its descendants. The selector() function tests whether the browser can parse a selector.
.card {
border: 1px solid #ccc;
}
@supports selector(:has(img)) {
.card:has(img) {
border-color: rebeccapurple;
}
}Why this one needs a guard
An unsupported selector invalidates the whole rule that contains it. That is true of a selector list too. Compare these two rules:
.card:has(img),
.card.featured {
outline: 2px solid gold;
}A browser that cannot parse :has() drops the entire rule, including .card.featured, which it could otherwise have matched. Keep the unsupported selector in its own rule, either inside @supports or alone, so that it does not take supported selectors down with it.
.card.featured {
outline: 2px solid gold;
}
@supports selector(:has(img)) {
.card:has(img) {
outline: 2px solid gold;
}
}Repeating the declaration is the price of the split. If the repetition is long, move the shared values into custom properties.
The enhancement must be cosmetic
The :has() example changes a border color. Nothing breaks if it is missing. Be careful using :has() to reveal or hide content, switch layouts, or enable form controls. In those cases the baseline needs its own way to reach the same state, or the unsupported browsers get a broken page.
Putting the patterns side by side
| Pattern | Baseline | Guard | Enhancement | Risk if baseline is skipped |
|---|---|---|---|---|
| Gallery | Flexbox wrap | `(display: grid)` | Auto-fill grid columns | Layout works, but columns are less even |
| Panel | 0.95 alpha background | `(backdrop-filter: blur(8px)) or (-webkit-backdrop-filter: blur(8px))` | 0.6 alpha with blur | Unreadable text over busy content |
| Card border | Gray border | `selector(:has(img))` | Colored border when an image is present | None, purely visual |
The table shows the question to ask of any pattern: what does the user see if the guard fails? In the gallery and card cases the answer is "something slightly plainer". In the panel case the answer could be "something unreadable", which is why the panel's baseline was designed first.
Using the cascade instead of @supports
Not every enhancement needs a block. The cascade handles single-property cases on its own. A browser drops a declaration it cannot parse, so you can write the fallback and the enhanced value one after the other.
.card {
width: 300px;
width: min(100%, 300px);
}A browser without min() discards the second line as invalid and keeps 300px. This costs less than an @supports block. It fails in one situation: when the fallback and enhanced values are both valid but the enhanced one has no effect. A property the browser recognizes, with a value it does not, is dropped, which is what you want. A property the browser recognizes and a value it accepts but ignores, because a related property is unsupported, is not dropped. Use @supports when the enhancement depends on several declarations working together, such as display: grid plus grid-template-columns.
When not to use @supports
Add a guard only if it protects something. Use these checks.
- Is the feature supported by every browser you support? If yes, write it directly. A guard that is always true adds noise.
- Does a single declaration fall back through the cascade? If yes, use the two-line pattern.
- Does the enhancement span multiple declarations that must apply together? If yes, use
@supports. - Does the unsupported selector sit in a list with other selectors? If yes, split the rule, and guard the new selector.
- Does the baseline stay readable and usable? If no, redesign the baseline before writing the enhancement.
The not form for targeted workarounds
You can write the guard the other way around, to give only the old browsers a workaround.
@supports not (display: grid) {
.layout {
float: left;
width: 50%;
}
}This works, but it makes the unsupported browsers the special case. Over time those workarounds pile up and are hard to remove. A baseline plus an enhancement reads better and lets you delete the baseline when your support policy moves on. Reserve not for cases where the workaround is small and clearly temporary.
A worked example: a toolbar
The toolbar below combines two conditions. It should use flexbox with gap when both are available. Browsers that have flexbox but not gap for flex containers get a margin-based spacing from the baseline.
.toolbar {
display: flex;
}
.toolbar > * + * {
margin-inline-start: 0.5rem;
}
@supports (display: flex) and (gap: 0.5rem) {
.toolbar {
gap: 0.5rem;
}
.toolbar > * + * {
margin-inline-start: 0;
}
}There is a trap in this example. (gap: 0.5rem) tested on its own passes in browsers that support gap for grid but not for flexbox. @supports checks whether the property and value parse, not whether they work in a specific layout mode. In those browsers the guard passes, the margin is removed, and the toolbar items touch each other. The check is only as precise as the property parsing.
When a property's support differs by context, a property check cannot tell the contexts apart. Test in the real browsers, or choose a baseline that stays correct whether or not the enhancement takes effect. Here, keeping the margin and not removing it is the safe choice, at the cost of slightly larger spacing in modern browsers.
Debugging an enhancement that does not apply
When the enhanced style does not show up, work through this list in order.
- Open the browser's developer tools and find the rule inside the
@supportsblock. If it is not listed, the condition failed. - Copy the condition into the console as
CSS.supports('...')and see whether it returnstrue. - Check for a missing space or parenthesis.
@supports (display:grid)is valid, but@supports display: gridwithout parentheses is not. - Check source order. The enhancement must come after the baseline unless its specificity is higher.
- Check for mixed
andandorwithout grouping parentheses. The whole condition is invalid, so the block is skipped.
Summary of the habits
- Design the baseline so that it is fine on its own.
- Put risky values, such as low-alpha backgrounds, only inside the guard.
- Keep unsupported selectors in separate rules.
- Use the cascade for single-property fallbacks and
@supportsfor groups of declarations. - Treat a parse check as weaker evidence than a real test.
Exercise
A stylesheet has .note:has(a), .note.important { color: crimson; } and no guard. In a browser that does not support :has(), the .note.important elements are not crimson. Why does this happen, and how do you fix it while keeping the :has() behavior in modern browsers?
Show answer
A browser that cannot parse :has() treats the whole selector list as invalid and drops the entire rule, so .note.important is dropped too. Split the rule: put .note.important { color: crimson; } on its own, and put .note:has(a) { color: crimson; } inside @supports selector(:has(a)) { ... }.
Exercise
You want a card to use a semi-transparent background of rgb(0 0 0 / 0.4) with backdrop-filter: blur(6px). Where should the semi-transparent background go, and what should the baseline background be?
Show answer
The semi-transparent background belongs only inside an @supports (backdrop-filter: blur(6px)) or (-webkit-backdrop-filter: blur(6px)) block, together with the blur. The baseline should be a nearly opaque background, such as rgb(0 0 0 / 0.9), so text stays readable when the blur is unavailable.