Lesson 2 of 30
Sass popularized writing nested selectors two decades ago. Now the browser does it natively — no build step, with one syntax quirk Sass never made you think about.
11 min read
Every option explains itself — why the right one is right, and why each wrong one is wrong. Miss one and it comes back later on a spaced-repetition schedule.
Question 1 of 3
`.card { .title { font-weight: 600; } }` is written using native CSS nesting. What selector does this actually compile down to and match against?
Hints without spoilers, and a plain explanation of why a wrong answer was wrong — answered only from this lesson. Free account, no card.
Sign up freeWhat interviewers actually ask about this, and what a strong answer has to cover.
A strong answer covers
Likely follow-ups
Native CSS nesting lets you write a selector inside another selector's rule block, instead of repeating the parent selector on every line. It ships in the browser itself — no Sass, Less, or PostCSS plugin required. The nested rule still compiles down to exactly the same flat selector you'd have written by hand; nesting is sugar for writing less repetitive CSS, not a new capability.
The one thing that trips people up coming from Sass: native nesting sometimes requires an explicit & (the "nesting selector," standing in for the parent selector) where Sass would have let you omit it. The rule is about ambiguity — if what follows the parent selector could be parsed as the start of a new declaration instead of a new selector, the & is required to disambiguate it.
The old, flat way:
.card { border-radius: 8px; }
.card:hover { box-shadow: 0 4px 12px rgb(0 0 0 / 0.15); }
.card .title { font-weight: 600; }
.card .title:hover { color: var(--color-accent); }
The nested way:
.card {
border-radius: 8px;
&:hover {
box-shadow: 0 4px 12px rgb(0 0 0 / 0.15);
}
.title {
font-weight: 600;
&:hover {
color: var(--color-accent);
}
}
}
Written as nested rules: .card contains &:hover and .title, and .title contains its own &:hover.
&:hover needs the & because :hover alone, nested directly, would be ambiguous with the compound selector shorthand. .title on its own line doesn't need one — nesting a selector that starts with a type, class, or ID selector reads unambiguously as descendant combination, exactly like Sass. Either way, .card .title:hover is the flattened selector the browser actually matches against — nesting is purely a way to write that string more concisely.
.card { .title {...} } produces .card .title, a plain descendant selector with the exact same global reach as if you'd never nested anything.& is required specifically to avoid ambiguity with a plain declaration, not as a stylistic preference — nesting a pseudo-class, an attribute selector, or anything that doesn't unambiguously start a new selector typically needs it.@media or other at-rules also works, but nesting a declaration (a plain property: value line) directly among nested rules can get confusing to read even when it's technically valid — most teams still group declarations before nested rules for readability.@supports runtime fallback, since there's no reasonable degraded experience for "nesting doesn't work.""What's the difference between CSS nesting and Sass nesting?" is a real question, and the sharp answer is the scoping misconception: interviewers are checking whether a candidate understands nesting is purely selector-writing syntax that flattens to global selectors, not a way to isolate a component's styles from the rest of the page.