TExcelTAD
Home › Sass › Sass Nesting

Sass Nesting

HTML is a tree, and Sass lets your styles follow the same shape. Instead of repeating a parent selector in every rule, you nest child rules inside it. The compiler joins the selectors for you.

Basic nesting

scss · try it yourself
nav {
  background: #2d2a32;

  ul {
    list-style: none;
    margin: 0;
    padding: 0;
    display: flex;
  }

  a {
    color: white;
    padding: 8px 12px;
    display: block;
    text-decoration: none;
  }
}
nav {
  background: #2d2a32;
}
nav ul {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
}
nav a {
  color: white;
  padding: 8px 12px;
  display: block;
  text-decoration: none;
}

Each nested selector becomes a descendant selector (nav ul, nav a).

The parent selector &

& stands for the full parent selector. Use it to attach pseudo-classes, modifiers or suffixes instead of creating a descendant:

scss · try it yourself
.btn {
  background: #cc6699;
  color: white;

  &:hover { background: #b35283; }
  &:focus-visible { outline: 3px solid #f3c1da; }
  &.is-disabled { opacity: 0.5; }
  &--large { font-size: 1.25rem; }   // suffix: .btn--large
  .dark-theme & { color: #111; }     // parent placed later: .dark-theme .btn
}
.btn {
  background: #cc6699;
  color: white;
}
.btn:hover {
  background: #b35283;
}
.btn:focus-visible {
  outline: 3px solid #f3c1da;
}
.btn.is-disabled {
  opacity: 0.5;
}
.btn--large {
  font-size: 1.25rem;
}
.dark-theme .btn {
  color: #111;
}
Written inside .btn Compiles to
&:hover .btn:hover
&.is-disabled .btn.is-disabled (same element, both classes)
& .icon or just .icon .btn .icon (descendant)
&--large .btn--large (suffix, great for BEM)
.dark-theme & .dark-theme .btn

Note: &.active (no space) and & .active (space) mean different things: the first matches one element with both classes, the second a descendant.

Combinators

You can start a nested selector with >, + or ~:

scss · try it yourself
.menu {
  > li { display: inline-block; }
  li + li { margin-left: 8px; }
  h2 ~ p { color: gray; }
}
.menu > li {
  display: inline-block;
}
.menu li + li {
  margin-left: 8px;
}
.menu h2 ~ p {
  color: gray;
}

Nested properties

Properties that share a prefix, like font-family, font-size and font-weight, can be nested under the prefix followed by a colon:

scss · try it yourself
.title {
  font: {
    family: Georgia, serif;
    size: 1.5rem;
    weight: 700;
  }
  margin: {
    top: 0;
    bottom: 0.5em;
  }
}
.title {
  font-family: Georgia, serif;
  font-size: 1.5rem;
  font-weight: 700;
  margin-top: 0;
  margin-bottom: 0.5em;
}

This feature exists but is rarely used — the shorthand properties (font: …, margin: …) are usually clearer.

Don't nest too deep

Every nesting level adds a selector to the output, which raises specificity and couples your CSS to the exact HTML structure. A common rule of thumb: no more than three levels.

scss
// Avoid: compiles to .page .sidebar .widget ul li a span
.page { .sidebar { .widget { ul { li { a { span { color: red; } } } } } } }

// Prefer: a dedicated class
.widget-link__label { color: red; }

Sass nesting vs native CSS nesting

Modern browsers now support nesting in plain CSS too. The syntax is similar, but Sass resolves the nesting at compile time, so the output works in every browser, and Sass supports suffixes like &--large, which native CSS nesting does not.

Test yourself

What does .card { &:hover { color: red; } } compile to?

  1. .card :hover { color: red; }
  2. .card:hover { color: red; }
  3. .card &:hover { color: red; }
  4. &:hover .card { color: red; }

Inside .tab, which selector produces .tab--active?

  1. & .--active
  2. .--active
  3. &--active
  4. &.--active

Why should you avoid nesting many levels deep?

  1. Sass refuses to compile it
  2. It increases specificity and ties CSS to HTML structure
  3. It makes the CSS file smaller
  4. Browsers can't read it