Sub topic 12
Browser support and fallbacks
This chapter assumes you can write nested rules with &, including the relaxed form that omits it. It covers which browsers parse nesting, what older ones do with it, and three ways to handle the gap: accept it, detect it, or compile it away.
Where native nesting is supported
Nesting shipped in two stages. The first stage was the original syntax, where a nested selector starting with a type name such as a or p needed a leading &. The second stage was the relaxed syntax, where a { ... } can sit directly inside a rule.
| Stage | Chrome and Edge | Safari | Firefox |
|---|---|---|---|
| Original syntax (`& a`, `.x`, `&:hover`, nested at-rules) | 112 | 16.5 | 117 |
| Relaxed syntax (`a { }` without `&`) | 120 | 17.2 | 117 |
Every engine had the relaxed syntax by December 2023, and nesting has been in Baseline since then. The 30-month point for "widely available" has passed by now. Check a current compatibility table such as caniuse or MDN for your own cutoff, because versions shift and the table above only covers the first release of each stage.
Two details matter for real projects:
- Chrome 112 to 119 and Safari 16.5 to 17.1 understand nesting but not the relaxed syntax. A rule like
.nav { a { padding: 0.5rem; } }is dropped there, while.nav { & a { padding: 0.5rem; } }works. - Around late 2024 (Chrome 130, Firefox 132, Safari 18.2) engines changed how declarations written after a nested rule are handled. Older engines hoisted them as if they came first. Newer ones keep source order. If you mix declarations and nested rules, test in both generations.
What an unsupporting browser does
A browser with no nesting support follows ordinary CSS error recovery. It doesn't know a style rule can contain another rule, so it reads each nested block as a malformed declaration and discards it.
.card {
padding: 1rem;
border: 1px solid #ccc;
& .title {
font-weight: 600;
}
color: #333;
}In an older browser the result is:
paddingandborderapply, because they come before the first nested rule.& .title { ... }is discarded.color: #333can be discarded too. There is no;after the nested block's closing brace, so the parser may treat everything up to the next;or}as one invalid declaration.
The rule that follows is that declarations after a nested rule are the most likely to disappear in an old browser. Put every declaration of the parent first, then the nested rules.
The failure is silent. The stylesheet loads without any error, and the page looks partly unstyled.
Choosing a strategy
There are three practical options, and the right one depends on your audience and how badly the page breaks without the nested rules.
Option 1: Ship it as written
If your analytics or support policy puts every visitor on Chrome 120, Safari 17.2, Firefox 117 or newer, you can ship nested CSS directly. Define the cutoff in your browserslist file or team documentation, so the decision is written down and not assumed.
Option 2: Progressive enhancement
Write the essential styles flat, so the page works without nesting. Add nested rules for refinements that can safely be missing, such as hover polish or a wide-screen layout tweak.
.card {
padding: 1rem;
border: 1px solid #ccc;
}
.card .title {
font-weight: 600;
}
@supports selector(&) {
.card {
& .title {
letter-spacing: 0.01em;
}
&:hover {
border-color: #666;
}
}
}The flat rules cover every browser. The nested block is wrapped in @supports selector(&), so only engines that accept & as a selector read it. That wrapper is a convenience and not a safeguard. An engine that doesn't know selector() ignores the whole @supports block, and an engine that knows @supports skips it as well. In both cases the nested rules never reach the old parser's error recovery.
Be careful with the inverse. @supports not selector(&) { ...flat fallback... } works in browsers that understand selector() but not nesting. Browsers too old to understand selector() at all treat the condition as false, so they skip your fallback too. For a flat fallback, write the flat rules unconditionally, as above.
selector(&) tells you the engine supports nesting, but it can't tell you whether the engine supports the relaxed syntax. A feature query for the relaxed form isn't reliable. If you depend on relaxed nesting, decide by browser version, or test it in script as shown next.
Detecting support in JavaScript
You can ask the CSS parser directly. Parse a small stylesheet and check whether the nested rule survived.
function parses(css) {
try {
const sheet = new CSSStyleSheet();
sheet.replaceSync(css);
const rule = sheet.cssRules[0];
return Boolean(rule && rule.cssRules && rule.cssRules.length === 1);
} catch {
return false;
}
}
const hasNesting = parses(".a { & .b { color: red; } }");
const hasRelaxedNesting = parses(".a { b { color: red; } }");
document.documentElement.classList.toggle("no-nesting", !hasNesting);
document.documentElement.classList.toggle("no-relaxed-nesting", !hasRelaxedNesting);
console.log({ hasNesting, hasRelaxedNesting });The test relies on the object model. A browser that understands nesting exposes the nested rule through cssRules on the parent rule. A browser that doesn't has no such property, so the check returns false. A browser with the original syntax but not the relaxed one drops the second stylesheet's rule, which makes hasRelaxedNesting false while hasNesting is true.
This runs in browsers that support constructable stylesheets (Chrome 73, Firefox 101, Safari 16.4). The try block handles anything older.
Detection in script is mainly useful for logging or for loading an alternative stylesheet. For layout it comes too late, because the page can paint before the script runs. Prefer a compile step when the styling matters.
Option 3: Compile nesting away
A build tool reads your nested source and writes flat CSS that works everywhere. This is the safest option when you must support older browsers, and it lets you author with nesting today.
Given this source:
.card {
padding: 1rem;
border: 1px solid #ccc;
& .title {
font-weight: 600;
}
&:hover {
border-color: #666;
}
}the compiled output is:
.card {
padding: 1rem;
border: 1px solid #ccc;
}
.card .title {
font-weight: 600;
}
.card:hover {
border-color: #666;
}Two common tools do this.
Lightning CSS reads a browser target and decides whether nesting needs to be compiled:
npx lightningcss --targets ">= 0.25%" input.css -o output.cssPostCSS with the postcss-nesting plugin uses a small config file:
// postcss.config.js
module.exports = {
plugins: [require("postcss-nesting")()],
};Sass also writes flat CSS, so a Sass build already produces output that any browser reads. Keep in mind that Sass nesting has its own rules, covered in the Sass chapter.
Gotchas when compiling
- Selector lists and
:is(). Native nesting treats a parent selector list like:is(#main, .note). A compiler must reproduce the same specificity, so it may emit:is()in the output. That works in browsers from about 2020 onward, which is far older than nesting support. If your target is older still, check whether the tool expands the list into separate selectors, because that changes the specificity. - Source order. Flattening moves child rules after the parent. Declarations mixed in after a nested rule are the case most likely to change behavior, so keep the parent's declarations first.
- At-rules.
@mediaand@supportsnested inside a rule are unwrapped into top-level at-rules containing the full selector. Check the output for one nested@mediato confirm the tool handles it. - Source maps. Generate them. Debugging flat output in dev tools is much harder if you can't map it back to the nested source.
Verifying your choice
Don't rely on a table of versions alone. Open the page in the oldest browser you promise to support, or run a cross-browser testing service, and check three things:
- The base layout renders correctly with no nested rules applied.
- No declaration that you expected from the parent rule is missing.
- The enhanced styles apply in a current browser.
Exercise
A stylesheet contains .menu { color: #333; a { color: blue; } padding: 1rem; }. A visitor uses Chrome 118. Which of the three declarations apply, and why? What two changes make the rule behave the same in Chrome 118 as in a current browser?
Show answer
Chrome 118 has original nesting but not the relaxed syntax, so a { color: blue; } is invalid there and is discarded. color: #333 applies because it comes before the nested rule. padding: 1rem may be lost, because the parser can treat it as part of the malformed nested declaration. The fix is to write the nested selector as & a and to move padding: 1rem above the nested rule: .menu { color: #333; padding: 1rem; & a { color: blue; } }.
Exercise
Your team must support Safari 15 and wants to author with nesting. Which strategy fits, and why would @supports not selector(&) not be a good place for the flat fallback?
Show answer
Compile the nesting in the build, for example with Lightning CSS using a Safari 15 target, or with PostCSS and postcss-nesting. The output is flat CSS that Safari 15 reads. @supports not selector(&) is a poor fallback gate because browsers too old to understand selector() treat the whole condition as false and skip the fallback, so the browsers that need it most may not get it.