Sub topic 8
Combining @supports with @media and @layer
This chapter assumes you can write a basic @supports rule and know how source order resolves conflicts in the cascade. You should also be comfortable with @media. Here you will combine these rules. You will also see how @layer keeps the baseline and the enhancement from fighting over specificity.
Conditional group rules nest
@media and @supports are both conditional group rules. Each one holds other rules and applies them only when its condition is true. Because of that, you can put one inside the other. The inner rule applies only when both conditions are true.
@media (min-width: 40rem) {
@supports (display: grid) {
.page {
display: grid;
grid-template-columns: 16rem 1fr;
}
}
}This sidebar layout needs two things. The viewport must be at least 40rem wide, and the browser must understand grid. A narrow screen or an old browser gets the baseline styles instead.
The nesting order does not change the result. Putting @supports outside and @media inside gives the same behavior. Choose the order that reads best. Many teams put @media on the outside, because it groups the rules by breakpoint.
Why not just use `and`?
@media and @supports have separate condition syntaxes. You cannot write @supports (display: grid) and (min-width: 40rem), because min-width is a media feature and not a property that @supports can test. Nesting is the way to combine the two kinds of condition.
Adding @layer
Progressive enhancement relies on the enhancement overriding the baseline. Normally source order and specificity decide which one wins. As a stylesheet grows, a baseline rule with a more specific selector can beat an enhancement that comes later.
Cascade layers fix this. A layer declared later wins over an earlier layer, whatever the specificity of the selectors inside them.
@layer base, enhancements;
@layer base {
.gallery {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
main .gallery > * {
flex: 1 1 12rem;
}
}
@layer enhancements {
@supports (display: grid) {
.gallery {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(12rem, 1fr));
}
}
}The first line sets the layer order. The enhancements layer comes last, so its declarations beat the base layer. The simple .gallery selector in the enhancement overrides display: flex even though it is less specific than main .gallery > *. Without layers, a more specific baseline selector could silently win.
The @supports block sits inside the layer. The reverse also works: you can put @layer inside @supports. Putting the condition inside the layer keeps all of the enhancement rules in one place.
Gotchas
- Declare the layer order in a statement at the top of the stylesheet, outside any condition. If the first mention of a layer sits inside a false
@supportsor@mediablock, the browser skips that block. The layer then gets its position from a later mention, and the order may not be what you intended. - Unlayered styles beat all layered styles. If some of your baseline rules are unlayered, they can override your enhancement layer. Put all of your rules in layers, or deliberately leave only the overrides outside.
- A layer is not a feature test. Browsers without
@layersupport ignore the whole block. This means the layered baseline would be lost on those browsers. Check your browser support targets before you layer the baseline.
Choosing a structure
| Situation | Structure |
|---|---|
| Enhancement depends on viewport and feature | `@media` containing `@supports` |
| Enhancement must beat a specific baseline selector | Put the enhancement in a later `@layer` |
| Enhancement depends on one feature only | A plain `@supports` block |
Start with the simplest structure in the table. Add more only when a real conflict appears.
Exercise
A stylesheet has @layer base, enhancements; followed by a base rule main .card { display: block; } and an enhancements rule .card { display: grid; } inside @supports (display: grid). A browser supports grid and @layer. Which display value does .card get, and why?
Show answer
It gets grid. Layer order beats specificity, and enhancements is declared after base, so its rule wins even though main .card is more specific.
Exercise
Why can you not write @supports (display: grid) and (min-width: 40rem) to test both grid support and viewport width?
Show answer
@supports tests properties, values and selectors, and min-width: 40rem in that form is a media feature. Nest an @supports rule inside an @media rule (or the reverse) to require both.