Sass Nesting

Beginner
⏱️ 12 min read
📚 Updated: Jul 2026
🎯 5 Examples
nested rules

What You’ll Learn

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

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.
  • Over-nesting creates long, brittle selectors; keep nests shallow.
  • For hover states and BEM suffixes, use the parent selector &.
💡
Beginner tip

Nesting is organization sugar. If you cannot picture the final selector, flatten the nest or write the CSS selector out loud before compiling.

📝 Basic Nesting Syntax

A classic site navigation pattern from the Sass Basics guide:

styles.scss
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.

⚠️ 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.

📋 Selector Lists

When you nest comma-separated selectors, Sass nests each complex selector separately, then combines them back into one list.

styles.scss
.alert, .warning {
  ul, p {
    margin-right: 0;
    margin-left: 0;
    padding-bottom: 0;
  }
}
// → .alert ul, .alert p, .warning ul, .warning p

🔗 Selector Combinators

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

🔢 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 &.

styles.scss
.button {
  padding: 0.5rem 1rem;

  &:hover { background: #024; }   // .button:hover
  &.is-large { font-size: 1.125rem; } // .button.is-large
}

⚡ Quick Reference

SCSS nestCompiled CSS
nav { a { } }nav a { }
.card { .title { } }.card .title { }
ul > { li { } }ul > li { }
h2 { + p { } }h2 + p { }
.a, .b { span { } }.a span, .b span { }
.btn { &:hover { } }.btn:hover { }

Examples Gallery

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 navul / 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;
  }
}

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.

styles.scss
.card {
  padding: 1rem;
  border: 1px solid #ddd;

  .title {
    font-size: 1.25rem;
    margin: 0 0 0.5rem;
  }

  .body {
    color: #555;
    line-height: 1.5;
  }

  .footer {
    margin-top: 1rem;
    font-size: 0.875rem;
  }
}

How It Works

Parent properties stay on .card. Nested classes become descendants. Two levels keep the CSS clear and reusable.

📈 Lists, Combinators & Parent Selector

Richer nests without repeating selectors by hand.

Example 3 — Nesting Selector Lists

Comma lists expand into every outer × inner combination.

styles.scss
.alert, .warning {
  ul, p {
    margin-right: 0;
    margin-left: 0;
    padding-bottom: 0;
  }
}

How It Works

Two outer selectors × two inner selectors = four compiled selectors in one rule.

Example 4 — Nesting Combinators

Child, adjacent sibling, and general sibling—three placement styles.

styles.scss
ul > {
  li {
    list-style-type: none;
  }
}

h2 {
  + p {
    border-top: 1px solid gray;
  }
}

p {
  ~ {
    span {
      opacity: 0.8;
    }
  }
}

How It Works

Combinators can live on the outer rule, the inner rule, or alone in between. Sass still emits normal CSS combinator selectors.

Example 5 — Nesting with & for States

When you need more than a descendant space, reach for the parent selector.

styles.scss
.button {
  padding: 0.5rem 1rem;
  background: #036;
  color: #fff;

  &:hover {
    background: #024;
  }

  &.is-large {
    padding: 0.75rem 1.5rem;
    font-size: 1.125rem;
  }
}

How It Works

Without &, nesting :hover would become .button :hover (descendant). With &:hover, you get .button:hover. Full details on the Parent Selector page.

🚀 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.

⚠️ 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.

💡 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

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.

Conclusion

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.

Continue with Sass Numbers or Sass Parent Selector.

Next: Sass Numbers

Learn units and numeric math for style values.

Sass Numbers →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful