Chepter 5: Sass Nested

Sass: Nesting (also called Nested Rules or Selector Nesting).

I’m going to explain this like we’re pair-programming a small component together — very detailed, with real-world examples, comparisons to plain CSS and native CSS nesting (2026 reality), best practices, the famous & parent selector, common pitfalls, and when to stop nesting.

1. What is Nesting in Sass? (Super Simple First)

In plain CSS, selectors are flat and repetitive:

CSS

In Sass (SCSS), you can nest rules the way your HTML is nested:

SCSS

Sass compiler automatically combines the selectors:

  • Outer selector + space + inner selector
  • Result → same CSS as above, but you wrote it cleaner and more visually like your HTML tree.

Biggest benefit:

  • Less repetition
  • Easier to read/maintain
  • Matches HTML structure → easier mental mapping

2. How Nesting Works – Basic Rules (2026 Sass Behavior)

Sass combines selectors with a space (descendant combinator) by default.

SCSS

Compiles to:

CSS

3. The Star of the Show: & – The Parent Selector

& = reference to the parent selector (invented by Sass, genius!)

Without &, nesting always adds a descendant space. With &, you can:

  • Add pseudo-classes (:hover, :focus)
  • Add modifier classes (BEM style)
  • Put selector before parent
  • Concatenate directly

Classic examples:

SCSS

Compiles to:

CSS

Magic! No repetition of .btn.

4. Real-World Example – Card Component with BEM-ish Nesting

SCSS

This is very common in 2026 component libraries — clean, scoped, easy to find related styles.

5. Nesting Combinators (> + ~ etc.)

You can place combinators anywhere:

SCSS

6. 2026 Reality: Sass Nesting vs Native CSS Nesting

Native CSS nesting landed ~2023–2024 and is fully supported in 2026.

But they are NOT the same — Sass team decided not to fully match native for compatibility reasons.

Key differences:

Feature / Behavior Sass Nesting (2026) Native CSS Nesting Winner for you?
Multiple parents .foo, .bar { .baz { } } → .foo .baz, .bar .baz :is(.foo, .bar) .baz (lower specificity) Sass (predictable specificity)
& at beginning &–modifier { } works perfectly Requires @nest or separate rule in some cases Sass
Overly deep nesting Allowed (but warns you) Allowed Tie
Mixing declarations after nested rules Deprecated / breaking changes in recent Dart Sass Wrapped in implicit @nest Native more forgiving now
No build step Requires Sass compiler Works directly in browser Native

Verdict in 2026:

  • Small projects / quick prototypes → native CSS nesting is fine (no build tool)
  • Medium/large projects, component libraries, design systems → Sass nesting still preferred because:
    • Better specificity control
    • Full & flexibility (suffixes, prepending selectors)
    • Richer ecosystem (mixins, maps) works seamlessly with it
    • Predictable output

7. Best Practices & Warnings (Don’t Over-Nest!)

  • Rule of thumb: Max 3–4 levels deep → .card .body .text p is already pushing it
  • Over-nesting creates:
    • High specificity → hard to override
    • Heavy selectors → slower (rarely, but possible)
    • Hard-to-read code
  • Use BEM / utility classes + shallow nesting instead
  • Use nesting for:
    • Pseudo-classes (&:hover)
    • Modifiers (&–active)
    • Child elements in components (__title, __footer)

8. Quick Challenge – Try This!

Create style.scss:

SCSS

Compile → play with HTML <nav class=”menu menu–vertical”> → see how clean it feels!

You’ve now got nesting down solid — one of Sass’s killer features since day one.

Next — want to combine nesting with mixins for reusable button families? Or dive into @use + nesting in modular files? Or compare with native @nest more deeply?

Just tell me — class is always in session 😊

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *