Sass Nesting

Beginner
9 min read
Updated: Nov 2026
5 examples
Dart Sass

Definition and Usage

Nesting means writing one style rule inside another. Sass combines the outer selector with the inner one—by default with a descendant combinator (a space)—so you avoid repeating parents.

Official sample idea.

nav { ul { ... } } compiles to nav ul { ... }.

Mirrors HTML hierarchy.

Nesting is great for reading structure—nav → ul / li / a—without copying nav on every line.

Keep nests shallow.

Deep nesting generates long selectors that cost bandwidth and make overrides harder. Prefer about two or three levels.

Syntax

SCSS
// Default: descendant join (a space)
nav {
  ul { margin: 0; }   // => nav ul
  a { display: block; } // => nav a
}

// Selector lists expand pairwise
.alert, .warning {
  ul, p { ... } // => .alert ul, .alert p, .warning ul, .warning p
}

// Combinators on outer, inner, or alone
ul > { li { ... } }   // => ul > li
h2 { + p { ... } }    // => h2 + p

// Need &:hover / &.is-active? See parent selector &

Pattern: open an outer rule → nest related children → Sass writes the combined CSS selectors.

Lists & Combinators

How nesting expands beyond a single pair:

PatternResult idea
Plain nestnav { ul } → nav ul
Selector listsEach side expands, then commas recombine
> / + / ~Combinator can sit on outer, inner, or between
Parent &For pseudo-classes and more—not a plain space join

SCSS Examples

Open View Compiled CSS for verified Dart Sass output (matches official Sass docs samples where shown).

Example 1 — Official Nav Nesting

Nest ul, li, and a under nav.

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

  1. Outer nav wraps related children.
  2. Each nest becomes a descendant selector.
  3. No repeated nav in the SCSS source.
  4. Matches the official Sass docs sample.

Example 2 — Selector Lists

Comma-separated selectors nest pairwise, then recombine.

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

How It Works

  1. Two parents × two children = four selectors.
  2. Sass expands each complex selector separately.
  3. One rule in the CSS output.
  4. Matches the official selector-list sample.

Example 3 — Nested Combinators

Place >, +, or ~ on the outer, inner, or middle.

SCSS
ul > {
  li {
    list-style-type: none;
  }
}

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

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

How It Works

  1. Child combinator on the outer: ul >.
  2. Adjacent sibling on the inner: + p.
  3. General sibling alone between: ~.
  4. Matches the official combinator samples.

Example 4 — Shallow Component Nest

One parent, two children—readable without deep trees.

SCSS
.card {
  padding: 1rem;

  .title {
    font-weight: 700;
  }

  .body {
    color: #444;
  }
}

How It Works

  1. Parent keeps shared padding.
  2. Children become .card .title / .card .body.
  3. Two levels stay easy to override.
  4. Prefer this depth for most UI blocks.

Example 5 — Nesting with &

Plain nesting adds a space; & attaches a pseudo-class instead.

SCSS
.btn {
  color: #111;

  &:hover {
    color: #000;
  }
}

How It Works

  1. Without &, :hover alone would be invalid here.
  2. & stands in for .btn.
  3. Result is .btn:hover, not .btn :hover.
  4. More patterns on parent selector.

Edge Cases & Pitfalls

Most nesting bugs are too much depth or forgetting & for states.

depth

Deep nests grow selectors fast

Each level multiplies specificity and file size. Prefer shallow structures.

&

Pseudo-classes need &

&:hover attaches; a bare nested :hover is not the same as a descendant join.

lists

Lists multiply

Two parents and two children become four selectors—check the compiled CSS.

properties

Property nesting is different

Hyphenated property namespaces (like font-) live on property declarations, not this page.

Sass Support

Selector nesting is core Sass on Dart Sass.

TopicStatus
Style-rule nestingSupported — default descendant join
Selector listsExpanded pairwise then recombined
Combinators>, +, ~ nestable
Advanced nestingUse & for non-space joins

Compile with the Dart Sass CLI.

Key Takeaways

  • Nest rules to avoid repeating parent selectors.
  • Default join is a space (descendant).
  • Lists & combinators nest too—check compiled CSS.
  • Stay shallow; use & for states and prefixes.

One line: nest style rules to combine selectors—keep depth low and reach for & when a plain space is not enough.

Compile Notes

Practical checklist:

checklist.txt
1. Nest related selectors under one parent
2. Expect a descendant join unless you add a combinator or &
3. Expand lists carefully (N × M selectors)
4. Prefer 2–3 levels of nesting
5. Use & for :hover, .is-active, and similar

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.

Next: Sass Numbers

Work with numbers, units, and arithmetic in Sass.

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.

8 people found this page helpful