CSS · Layout and visual systems
How does native CSS nesting work?
Short answer
Native CSS nesting lets you write a nested rule inside another, using & to refer to the parent. It reduces repetition but should stay shallow because it increases coupling to structure.
CSS nesting is now native, so you do not need a preprocessor for basic nesting. The & selector represents the parent selector, and nested rules can include pseudo-classes, media queries and other at-rules.
.card {
padding: 1rem;
& .title {
margin: 0;
}
&:hover {
border-color: #2563eb;
}
@media (min-width: 40rem) {
padding: 2rem;
}
}What to remember
- & refers to the parent selector list
- Nested selectors inherit the specificity behaviour of their parent list, similar to :is()
- Nested at-rules such as @media apply to the rules inside them
- Declarations can appear before or after nested rules in current implementations
- Deep nesting makes selectors harder to override and debug
Nesting is convenient for component-scoped styles, but it is not a reason to recreate a deep Sass tree. Two or three levels is usually enough. If a selector is hard to read, a class on the target element is often clearer.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1What does & mean in native CSS nesting?
- 2How does nesting affect specificity?
- 3Can you nest media queries?
- 4Why should you avoid deep nesting?
Say it in the interview
Native nesting lets me write child rules inside a parent rule, using & to refer to the parent. It removes repetition for component styles and supports nested at-rules like media queries. The specificity follows CSS's rules, which are not identical to Sass. I keep nesting shallow because deep structures become hard to override and debug.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live CSS mock interview and get scored feedback on depth, clarity and confidence.