Sass Nesting
Sass Nesting
Sass Nesting allows you to write CSS selectors inside other selectors, following the same hierarchy as your HTML structure.
This makes your code cleaner, more readable, and better organized compared to plain CSS.
Basic Nesting Example
Sass (.scss)
Compiled CSS
Nesting Reflects HTML Structure
HTML
✔ Sass nesting matches the DOM hierarchy naturally.
Nesting with Classes
Parent Selector (&)
The & symbol refers to the parent selector.
Hover Example
Modifier Example (BEM style)
Output:
Nesting Pseudo-Classes
Nesting Media Queries
✔ Keeps responsive styles near the main styles.
Nesting Properties (Optional Syntax)
Compiles to:
❌ Over-Nesting (Bad Practice)
⚠️ Avoid nesting more than 3 levels.
✅ Best Practices
✔ Nest only when logical
✔ Use & wisely
✔ Avoid deep nesting
✔ Keep code readable
📌 Summary
-
Sass nesting mirrors HTML structure
-
Reduces repetition
-
Improves readability
-
Over-nesting should be avoided
