0
0
SASSmarkup~3 mins

Why Basic selector nesting in SASS? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

Discover how nesting selectors can turn messy CSS into neat, easy-to-manage code!

The Scenario

Imagine you are styling a website menu. You write CSS rules for the menu container, then for each menu item, and then for links inside those items, all separately.

The Problem

Writing each selector fully every time means repeating long lines like .menu ul li a again and again. It's easy to make mistakes or forget parts, and updating styles means changing many places.

The Solution

Basic selector nesting lets you write CSS rules inside each other, matching the HTML structure. This keeps your code shorter, clearer, and easier to update.

Before vs After
Before
.menu ul li a {
  color: blue;
}
.menu ul li a:hover {
  color: red;
}
After
.menu {
  ul {
    li {
      a {
        color: blue;
        &:hover {
          color: red;
        }
      }
    }
  }
}
What It Enables

You can write CSS that looks like your HTML, making styles easier to read, maintain, and change.

Real Life Example

When building a blog, you can nest styles for article titles, paragraphs, and links inside the article container, so all related styles stay grouped together.

Key Takeaways

Writing selectors manually is repetitive and error-prone.

Nesting selectors matches HTML structure and reduces repetition.

Nesting makes CSS easier to read and maintain.