Sass Tutorial menu

Sass Nesting

Nesting lets you write CSS rules inside other rules, in the same shape as your HTML.

Basic Nesting

In plain CSS you must repeat the parent selector again and again: nav ul, nav li, nav a. In Sass, you write the child rules inside the parent:

Example

scss
nav {
  background-color: #222;

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

  li {
    display: inline-block;
  }

  a {
    color: white;
    padding: 10px 15px;
    text-decoration: none;
  }
}
Output
nav {
  background-color: #222;
}
nav ul {
  margin: 0;
  padding: 0;
  list-style: none;
}
nav li {
  display: inline-block;
}
nav a {
  color: white;
  padding: 10px 15px;
  text-decoration: none;
}

Each nested selector becomes a descendant selector in the CSS, with a space between the parent and the child.

The Parent Selector &

The & symbol means "the parent selector itself". Use it when you do not want a space, for example for pseudo-classes like :hover, extra classes and name endings.

Example

scss
.btn {
  background-color: #cc6699;
  color: white;

  &:hover {
    background-color: #b34d80;
  }

  &.disabled {
    opacity: 0.5;
  }

  &-large {
    font-size: 20px;
  }
}
Output
.btn {
  background-color: #cc6699;
  color: white;
}
.btn:hover {
  background-color: #b34d80;
}
.btn.disabled {
  opacity: 0.5;
}
.btn-large {
  font-size: 20px;
}
You writeYou getMeaning
&:hover.btn:hoverThe button when hovered
&.disabled.btn.disabledA button that also has class disabled
&-large.btn-largeA new class name that starts with btn
:hover (without &).btn :hoverAny hovered element inside the button (usually a bug)

Child, Sibling and Media Queries

You can nest selectors that start with > or +, and you can even nest a media query. Sass moves the media query out and puts the right selector inside it.

Example

scss
.card {
  padding: 20px;

  > h2 {
    margin-top: 0;
  }

  + .card {
    margin-top: 16px;
  }

  @media (max-width: 600px) {
    padding: 10px;
  }
}
Output
.card {
  padding: 20px;
}
.card > h2 {
  margin-top: 0;
}
.card + .card {
  margin-top: 16px;
}
@media (max-width: 600px) {
  .card {
    padding: 10px;
  }
}

Keeping the phone style right next to the normal style makes the code easier to follow.

Property Nesting

Properties that share a first word, such as font-family, font-size and font-weight, can be nested too. This is used less often, but you may see it:

Example

scss
.title {
  font: {
    family: Georgia, serif;
    size: 24px;
    weight: bold;
  }
}
Output
.title {
  font-family: Georgia, serif;
  font-size: 24px;
  font-weight: bold;
}

Do Not Nest Too Deep

Every level of nesting makes the CSS selector longer and stronger. Deep nesting like .page .content .sidebar .menu ul li a is hard to override later and ties your CSS tightly to the HTML.

A good rule: do not nest more than three levels deep. If you need more, give the element its own class.

Exercise

Exercise

Inside .link { ... }, what CSS selector does &:hover produce?

Learn Sass with a teacher

Join the Web Development course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp