Skip to main content Skip to navigation
All Posts All Tags

Mastering sass nesting: Benefits, drawbacks, and my top tips

Sass nesting can either streamline or complicate your stylesheets. Let's explore its advantages, challenges, and some best practices I've learnt over the years.

Understanding Nesting in Sass

Sass nesting simply means placing selectors inside each other.

It's a way of writing CSS selectors that is a little shorter, and places more emphasis on the parent-child relationships. Many people find it easier to read because it provides a clear visual hierarchy to stylesheets much like HTML, thanks to the indentation and general syntax.

You can nest selectors in all sorts of ways using Sass. It's super flexible!

Demo: Nested Selectors

Consider the following example in Sass (scss):

/*  _card.scss  */

.card {
// card styles here

h2 {
// card heading styles here
}

.button {
// card button styles here

&:focus-visible {
// card button's keyboard focus styles here
}
}
}

Compiled to CSS, this becomes:

/*  main.css  */

.card {
/* card styles here */
}

.card h2 {
/* card heading styles here */
}

.card .button {
/* card button styles here */
}

.card .button:focus-visible {
/* card button's keyboard focus styles here */
}

Demo: Nested BEM Class Names

Some tutorials suggest nesting partial class names according to the BEM (Block Element Modifier) naming convention (see glossary). Here's an example:

/*  _card.scss  */

.card {
// card styles here...

&__heading {
// card heading styles here
}

&__button {
// card button styles here...

&:focus-visible {
// card button's keyboard focus styles here
}
}
}

Compiled to CSS, this becomes:

/*  main.css  */

.card {
/* card styles here */
}

.card__heading {
/* card heading styles here */
}

.card__button {
/* card button styles here */
}

.card__button:focus-visible {
/* card button's keyboard focus styles here */
}

However, this Sass example can be harder to read and maintain over time. I recommend you avoid nesting partial class names like this.

Benefits of Nesting

Nesting with Sass offers several advantages:

  1. Readability: Structuring styles according to HTML hierarchy enhances understanding.

  2. Reduced Repetition: Nested selectors eliminate the need for repeating parent selectors.

  3. Modularity: Nesting encourages modular styling by isolating styles within their components.

  4. Easier Targeting: Nested selectors allow targeting specific elements within parent contexts easily.

  5. Efficient Compilation: Sass compiles nested styles into optimised CSS output, merging nested selectors when applicable.

  6. Selector Flexibility: Nested selectors in Sass can help you construct more complex and expressive selectors if you want to.

  7. Time Savings: By reducing repetition, nesting can save a little time spent typing.

Drawbacks of Nesting

However, there are potential downsides to excessive nesting:

  1. Specificity Issues: When beginners first delve into Sass, they often encounter the issue of specificity. Overusing nesting can result in selectors that are overly specific, creating significant hurdles when trying to override styles later and obscuring understanding of how styles have been applied.

  2. Selector Repetition: Intensive nesting might necessitate repeating selectors within a single SCSS file. For instance, this might be done to alter an element's appearance based on its parentage or to circumvent specificity conflicts in the CSS cascade. Such repetition can lead to considerable confusion.

  3. Bloated CSS: Deep nesting can generate large, complex CSS output, impacting performance.

  4. Complicated Selectors: Excessive nesting can result in complex and hard-to-maintain selectors.

  5. Maintenance Challenges: Deeply nested styles may be difficult to adjust or refactor as projects evolve.

  6. Limited Modularity: Over-nesting can hinder modularity by tightly coupling styles to a specific HTML structure.

  7. Overrides and Conflicts: Deeply nested selectors can lead to conflicts during overrides.

My Best Practice Advice

To mitigate these issues, consider the following practices:

  1. Limit nesting depth: Keep nesting levels shallow for readability and simplicity.

  2. Use single class selectors: Maintain low CSS specificity by using single class selectors as much as possible and avoiding excessive nesting.

  3. Selective Nesting: Reserve nesting for pseudo states (like :hover), pseudo elements (like :before), media queries, and ARIA attributes.

  4. Check CSS Output: Your compiled CSS should look exactly the same as if you'd written it without any preprocessor at all. If there are any unexpected or long selector chains in there, refactor the Sass.

  5. Regular Review and Refactoring: Periodically review and refactor styles to maintain clarity and organization.

Taken all together, this should result in highly readable, easy to maintain CSS with low specificity.

By embracing these practices, you can harness the benefits of Sass nesting while minimizing its potential drawbacks. This will result in more readable, maintainable, and efficient code.


Conclusion

Experiment with these strategies, particularly selective nesting, and observe how they impact the legibility and maintainability of your code. I hope that taking these approaches to heart helps your Sass workflow.