Sub topic 8
Combining @supports with @media and @layer
This chapter assumes you can write @media queries and know how the cascade resolves conflicts by origin, specificity and source order. It also assumes you have written a basic @supports (property: value) rule. You don't need to have used @layer before. The chapter introduces what you need.
By the end you will be able to:
- nest
@supportsand@mediain either order, - place enhancements in cascade layers without losing to the baseline,
- recognize where each combination fails in older browsers.
Conditional group rules
@media and @supports are both conditional group rules. Each has a condition and a block of other rules. The browser evaluates the condition, and if it is true, the rules inside take part in the cascade as if they were written at the top level.
@layer is not a condition. It is a grouping rule that assigns the rules inside it to a named layer, and layers change which rule wins in the cascade. The three at-rules answer different questions:
| At-rule | Question it answers | Effect when the answer is "no" |
|---|---|---|
| `@media` | Is the environment (viewport, input type, user preferences) right? | Rules inside are skipped |
| `@supports` | Does this browser understand this CSS? | Rules inside are skipped |
| `@layer` | Which tier of the cascade do these rules belong to? | Not applicable. Layers are always applied, but they rank rules by tier |
Because conditional group rules can contain other conditional group rules, you can stack them. Because @layer can contain conditional rules and the reverse, you can arrange layers and conditions in whichever order reads best.
Nesting @supports and @media
You can put one condition inside the other. The inner rules apply only when every enclosing condition is true. This example needs a wide viewport and grid support before it switches on a two-column page:
.page {
max-width: 60rem;
margin-inline: auto;
}
@media (min-width: 40rem) {
@supports (display: grid) {
.page {
display: grid;
grid-template-columns: 16rem 1fr;
}
}
}Below 40rem the media condition is false, so the block is ignored. On a wide screen in a browser without grid, the @supports condition is false, so the block is ignored. In both cases the user gets the plain single-column baseline.
The order of nesting does not change the result, because both conditions must be true either way. This version is equivalent:
@supports (display: grid) {
@media (min-width: 40rem) {
.page {
display: grid;
grid-template-columns: 16rem 1fr;
}
}
}Pick the order that makes your file easiest to read. Many teams put @supports on the outside when a whole group of rules depends on one feature, and @media on the outside when a responsive breakpoint has several enhancements.
Why you cannot merge them into one condition
There is no syntax for writing @media (min-width: 40rem) and (display: grid). Media queries test the environment, and @supports tests the parser. They have separate grammars, so and inside @media accepts only media features. If you write a property and value pair there, the whole media query is invalid. An invalid media query evaluates to "not all", so its rules never apply, and the browser gives no error. When you need both kinds of test, nest the two rules.
Nesting inside a style rule
Browsers that support CSS nesting let you write conditional rules inside a style rule. The selector is then implied:
.page {
max-width: 60rem;
margin-inline: auto;
@media (min-width: 40rem) {
@supports (display: grid) {
display: grid;
grid-template-columns: 16rem 1fr;
}
}
}This keeps all the rules for .page in one place. It also adds a dependency: a browser without CSS nesting support does not understand the nested block. Such a browser still applies the declarations that come before it, so the baseline survives. The enhancement is skipped, which is the outcome you want from an enhancement anyway. Remember, though, that you now rely on nesting support as well as grid support, and a browser may support one without the other.
How the browser evaluates a stack of conditions
The following diagram shows the path a rule takes from your stylesheet to the final computed style.
Note that the conditions only decide whether a rule participates. They add nothing to the rule's weight. A selector inside @supports has the same specificity as the same selector outside it. If an enhancement does not take effect, the cause is almost always one of the later steps (layer, specificity or order), not the condition.
Adding cascade layers
A cascade layer is a named group of rules. When two rules conflict, the browser compares layers before it compares specificity. A rule in a later layer beats a rule in an earlier layer, however specific the earlier selector is. You declare the order once, at the top of the stylesheet:
@layer reset, base, components, enhancements;This statement creates four layers in that order, from lowest to highest priority. Later @layer blocks with the same names add rules to the existing layers, and the block's position in the file does not change the layer's rank.
Layers suit progressive enhancement because they let you reserve a high-priority tier for upgrades. The enhancement then wins by layer order, so it does not need a more specific selector or a careful position in the source.
@layer reset, base, components, enhancements;
@layer base {
.gallery {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
.gallery > * {
flex: 1 1 12rem;
}
}
@layer enhancements {
@supports (display: grid) {
.gallery {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(12rem, 1fr));
}
}
}The baseline lives in base and the grid upgrade lives in enhancements. When grid is supported, the .gallery rule in enhancements wins over the one in base, because that layer ranks higher. The flex: 1 1 12rem declaration on the children stays in effect but has no visible effect on grid items, so it is harmless to leave in.
Putting @layer inside @supports
You can also write the nesting the other way round, with the layer inside the condition:
@layer base, enhancements;
@layer base {
.panel {
background: rgb(255 255 255 / 0.95);
}
}
@supports (backdrop-filter: blur(8px)) or (-webkit-backdrop-filter: blur(8px)) {
@layer enhancements {
.panel {
background: rgb(255 255 255 / 0.6);
-webkit-backdrop-filter: blur(8px);
backdrop-filter: blur(8px);
}
}
}Both forms give the same result in a browser that supports layers. The first form keeps all rules for a layer together. The second form keeps all rules for a feature together. Choose one and use it consistently in a project.
Keep the @layer base, enhancements; statement at the top level, outside any condition. A layer name that first appears inside a false condition is never created, so its position in the order would depend on which conditions happen to pass. Declaring the order up front makes the ranking identical in every browser.
Gotchas when layers meet baselines
Unlayered styles beat every layer
Rules that are not in any layer are treated as if they sit in a final implicit layer, which ranks above all named layers. This breaks a common setup:
@layer base, enhancements;
.card {
border: 1px solid #ccc;
}
@layer enhancements {
@supports selector(:has(img)) {
.card:has(img) {
border-color: rebeccapurple;
}
}
}The enhancement never wins here. The baseline .card rule is unlayered, so it outranks everything in enhancements, even though the enhancing selector is more specific. The fix is to put the baseline in a layer (base) as well. The rule for projects that use layers is to put all of your own styles in layers, or none of them. Reserve the unlayered position for deliberate overrides.
Browsers without @layer drop the whole block
A browser that does not understand @layer skips the entire @layer base { ... } block, including the baseline inside it. In that browser the page has no baseline styles at all, which defeats the purpose of progressive enhancement. The @layer support gap is small today because layers are available in all current major browsers. If you must support older browsers, check your audience's browser data before you wrap baseline styles in layers. Putting only enhancements in layers, and leaving the baseline unlayered, is not a fix, because of the previous gotcha. For that situation, either avoid layers or provide a separate fallback stylesheet.
Layer order inverts for !important
Declarations marked !important reverse the layer order. An important declaration in an earlier layer beats an important declaration in a later one. Avoid !important in enhancements. If you need it, check which layer holds the declaration that you want to win.
A false @supports leaves no trace
When the condition is false, the rules inside are discarded. The layer statement at the top still ranks the layers, so the order stays stable. Nothing in your baseline layers changes, which is what you want from a failed enhancement.
A combined example
This stylesheet puts every idea together. The baseline is a single column. A wide viewport with grid support gets a sidebar layout, and a browser with backdrop-filter support gets a frosted sidebar. All the rules for upgrades sit in the enhancements layer.
@layer base, enhancements;
@layer base {
.page {
max-width: 60rem;
margin-inline: auto;
}
.sidebar {
background: rgb(255 255 255 / 0.95);
padding: 1rem;
}
}
@layer enhancements {
@media (min-width: 40rem) {
@supports (display: grid) {
.page {
display: grid;
grid-template-columns: 16rem 1fr;
gap: 1rem;
}
}
}
@supports (backdrop-filter: blur(8px)) or (-webkit-backdrop-filter: blur(8px)) {
.sidebar {
background: rgb(255 255 255 / 0.6);
-webkit-backdrop-filter: blur(8px);
backdrop-filter: blur(8px);
}
}
}Three behaviors to check:
- On a narrow screen, the media condition fails, so the page stays a single column while the sidebar can still be frosted.
- In a browser with no backdrop-filter, the sidebar keeps its nearly opaque background, which stays readable.
- A later edit to the
baselayer, even with a more specific selector, cannot override the enhancements, because layer order decides first.
Debugging combined conditions
When an enhancement does not apply, work from the outside in:
- Check each condition on its own. In browser developer tools, a rule inside a false conditional block is shown greyed out or not at all.
- Check the syntax of the condition. A mixed
and/orwithout parentheses makes the@supportscondition invalid, so its block never applies. - Check the layer. If your styles use layers, confirm that the baseline and the enhancement are both layered, and that the enhancement's layer is declared later.
- Check specificity and order only after those steps, because they matter only between rules in the same layer.
Exercise
A stylesheet declares @layer base, enhancements;. It contains an unlayered .btn { padding: 0.5rem; } rule and, inside @layer enhancements { @supports (gap: 1rem) { ... } }, a .btn { padding: 1rem; } rule. The browser supports gap. Which padding does the button get, and what is the smallest change that makes the enhancement win?
Show answer
The button gets 0.5rem. Unlayered rules rank above all named layers, so the baseline beats the enhancement regardless of selector or order. The smallest fix is to move the baseline .btn rule into @layer base { ... }, so that the later enhancements layer wins.
Exercise
Why can you not write @media (min-width: 40rem) and (display: grid) to test for a wide viewport and grid support in one rule, and how should you write it instead?
Show answer
Media queries accept only media features, so (display: grid) makes the whole query invalid and the block never applies. Nest the rules instead: @media (min-width: 40rem) { @supports (display: grid) { ... } }. The inverse order works too.