Sub topic 12
Browser support and fallbacks
This chapter assumes you can write nested rules with &, including the relaxed form without a leading &. It also assumes you know how @media nests inside a style rule. The goal is to decide when native nesting is safe to ship and what to do when it isn't.
Which browsers support what
Native nesting shipped in two stages. The stages matter because the first one had a stricter grammar.
| Stage | What it allows | Chrome and Edge | Safari | Firefox |
|---|---|---|---|---|
| Original (strict) syntax | Nested selectors must start with a symbol. A type selector such as `a` needs `&` in front. | 112 | 16.5 | not applicable |
| Relaxed syntax | `a { }` works with no leading `&`. | 120 | 17.2 | 117 |
Firefox shipped nesting with the relaxed grammar from the start. Version numbers change as the data is corrected, so check MDN or caniuse for the current table before you decide on a baseline.
The practical rule: if you write .nav { a { ... } }, you need Chrome and Edge 120+, Safari 17.2+, and Firefox 117+. If you always write & a, you can reach back to Chrome 112 and Safari 16.5.
What an unsupporting browser does
A browser without nesting does not fail loudly. Its parser reads & .title { ... } inside a rule as an invalid declaration and discards it. The outer rule's own declarations still apply.
.card {
padding: 1rem;
border: 1px solid #ccc;
& .title {
font-weight: 600;
}
}In an old browser, .card gets its padding and border, but .title is not bold. The same happens to nested @media and @supports blocks: they are dropped, along with any declarations inside them.
Choosing a strategy
You have three options.
- Ship native nesting. This is the simplest choice when your analytics show nearly all visitors on supporting versions.
- Compile it away. Tools such as Lightning CSS or the
postcss-nestingplugin read nested CSS and output flat rules. Your source stays nested, and old browsers receive CSS they understand. - Layer it as an enhancement. Keep the styles a page cannot work without in flat rules, and put polish in nested rules.
Progressive enhancement in practice
Option 3 works because unsupported browsers keep everything outside the nested blocks. Layout-critical rules must therefore not live inside a nested block.
.panel {
display: block;
padding: 1rem;
}
@media (min-width: 40rem) {
.panel {
display: grid;
grid-template-columns: 1fr 2fr;
}
}
.panel {
& .badge {
background: gold;
}
&:hover {
outline: 2px solid currentColor;
}
}The two-column layout is a flat @media rule, so every browser applies it. The badge color and hover outline are nice to have, so losing them in an old browser is acceptable.
Feature detection
You can also gate nested code behind a test, using @supports selector() with &.
@supports selector(&) {
.alert {
& strong {
color: darkred;
}
}
}Use this with care. A browser that parses selector(&) as true is not guaranteed to handle every nested form you use. For example, a browser with the strict grammar may pass this test and still drop .alert { strong { } }. For that reason, a build step is usually more reliable than runtime detection.
Checklist before shipping
- Confirm your minimum browser versions against current support data.
- Keep layout-critical rules and critical
@mediaqueries flat unless you compile. - Prefix type selectors with
&if you must support the strict-syntax versions. - Test once in the oldest browser you support. Look for missing styles, because you will not see errors.
Exercise
Your site must work in Safari 16.5 and in current Chrome. A teammate writes .menu { li { margin: 0; } }. What happens in Safari 16.5, and how do you change it so it works in both?
Show answer
Safari 16.5 supports only the original syntax, so li without a leading symbol makes the nested rule invalid and the browser drops it. Change it to .menu { & li { margin: 0; } }. That form is valid in both the strict and relaxed grammars. Alternatively, compile the nesting to flat CSS in a build step.