Sass nesting lets you write style rules inside other rules so selectors follow the same visual hierarchy as your HTML. Sass combines them into flat CSS. This page covers basic nests, shallow-nesting habits, selector lists, combinators, and five compiled examples.
01
Basics
Rule in a rule
02
Output
Joined selectors
03
Shallow
Avoid deep nests
04
Lists
Comma selectors
05
Combinators
> + ~
06
Practice
5 examples
Concept
What Is Nesting?
HTML has a clear nested structure. Plain CSS does not—you often repeat parent selectors. Sass lets you nest CSS selectors so the source mirrors that hierarchy. By default, Sass joins outer and inner selectors with a descendant combinator (a space).
Write related rules in one place instead of repeating nav everywhere.
Compiled CSS is still flat—browsers never see nested rules from Sass.
nav {
ul {
margin: 0;
padding: 0;
list-style: none;
}
li { display: inline-block; }
a {
display: block;
padding: 6px 12px;
text-decoration: none;
}
}
Compiles to nav ul, nav li, and nav a—readable in SCSS, standard in CSS.
Important
⚠️ Keep Nesting Shallow
Nested rules are helpful, but deep nests make it hard to see how much CSS you generate. The deeper you go, the larger the stylesheet and the more work for the browser. Official docs say: keep those selectors shallow.
📈
Practical rule of thumb
Aim for about 2–3 levels for components. If you are nesting .page .section .card .header .title span, stop and refactor—use a class or the parent selector instead of stacking descendants.
Lists
📋 Selector Lists
When you nest comma-separated selectors, Sass nests each complex selector separately, then combines them back into one list.
You can nest selectors that use combinators. Put the combinator at the end of the outer selector, at the start of the inner selector, or alone between the two.
> — direct child
+ — adjacent sibling
~ — general sibling
Next Step
🔢 Beyond Plain Descendants
Default nesting always inserts a space (descendant). For pseudo-classes, modifiers, or putting the parent elsewhere in the selector, use the parent selector &.
Each example is verified with Dart Sass. Open View Compiled CSS for output.
📚 Getting Started
Nest to match HTML structure without repeating parents.
Example 1 — Navigation Nesting
Classic nav → ul / li / a pattern from Sass Basics.
styles.scss
nav {
ul {
margin: 0;
padding: 0;
list-style: none;
}
li { display: inline-block; }
a {
display: block;
padding: 6px 12px;
text-decoration: none;
}
}
📤 Compiled CSS:
nav ul {
margin: 0;
padding: 0;
list-style: none;
}
nav li {
display: inline-block;
}
nav a {
display: block;
padding: 6px 12px;
text-decoration: none;
}
How It Works
Each inner rule is joined to nav with a space. You write the hierarchy once; Sass expands it to three flat rules.
Example 2 — Component Nesting
Group a card’s child pieces under one parent class.
Without &, nesting :hover would become .button :hover (descendant). With &:hover, you get .button:hover. Full details on the Parent Selector page.
Applications
🚀 Real-World Use Cases
Navigation & menus — nest ul / li / a under nav.
Cards & media objects — keep title, body, and footer styles together.
Forms — group label, input, and hint under .field.
Layout sections — shallow nests for header / main / footer regions.
Component states — nest with &:hover / &.is-open.
🧠 How Compilation Works
1
Read nested rules
Find style rules written inside other style rules.
Parse
2
Combine selectors
Join outer + inner (space, or combinator / &).
Join
3
Expand lists
Cross each outer complex selector with each inner one.
Lists
4
✓
Emit flat CSS
Output normal CSS rules browsers already understand.
Watch Out
⚠️ Common Pitfalls
Nesting too deep — long selectors that are hard to override and maintain.
Forgetting & for states — .btn { :hover { } } becomes .btn :hover, not .btn:hover.
Copying HTML depth 1:1 — HTML can nest five levels; your CSS usually should not.
Surprise list explosion — many commas on both sides multiply selectors quickly.
Assuming CSS nesting is the same — native CSS nesting exists now, but Sass nesting has its own rules and & behavior.
Pro Tips
💡 Best Practices
✅ Do
Nest related component pieces 2–3 levels deep
Use nesting to avoid repeating a short parent selector
Prefer a clear class over another nest level
Use & for pseudo-classes and modifiers
Check compiled CSS when nests feel clever
❌ Don’t
Mirror every HTML tag in a deep nest
Build specificity walls you cannot override later
Nest media queries so deep the file becomes unreadable
Skip & when you mean “same element”
Assume more nesting always means cleaner code
Summary
Key Takeaways
🗃️01
Rule in rule
Sass joins them
Core
📄02
Flat CSS
browsers see flat
Output
⚖️03
Stay shallow
2–3 levels
Quality
📋04
Lists expand
every combo
Lists
🔗05
Use &
for states
Advanced
❓ Frequently Asked Questions
Nesting means writing one style rule inside another. Sass automatically combines the outer selector with the inner one (usually with a space), so you can mirror HTML structure without repeating parent selectors.
It becomes nav ul { } in CSS. The inner ul is nested under nav, so Sass joins them as a descendant selector.
Deep nests produce long selectors that are harder to override, larger to download, and slower for browsers to match. Prefer two or three levels for most UI, not five or six.
Each complex selector between commas nests separately, then Sass recombines them. .alert, .warning { ul, p { } } becomes .alert ul, .alert p, .warning ul, .warning p.
Yes. Put the combinator on the outer selector (ul > { li { } }), on the inner selector (h2 { + p { } }), or alone between them (p { ~ { span { } } }).
Use & when you need more than a plain descendant join—for example &:hover, &.is-active, or [dir=rtl] &. See the Sass Parent Selector page for details.
Did you know?
Modern CSS also supports nesting natively in browsers. Sass nesting still shines for older tooling, mixins, and the powerful parent selector patterns documented on this site—and the same advice applies everywhere: keep nests shallow.
Sass nesting organizes selectors the way HTML is structured, then compiles to flat CSS. Start with shallow component nests, learn how lists and combinators expand, and reach for & when you need states or modifiers.