Nesting in SCSS: Writing Cleaner and Structured Code
Nesting in SCSS: Writing Cleaner and Structured Code

What is Nesting in SCSS?
Nesting in SCSS allows you to write CSS rules inside other rules, following the structure of your HTML. This helps organize styles in a more readable way. Instead of repeating parent selectors, you can place child elements inside them. It makes the stylesheet look clean and logical. Nesting reduces code duplication. It is especially useful for complex layouts. Overall, it improves code structure and readability.
Example:
nav {
ul {
list-style: none;
}
li {
display: inline-block;
}
}
Why Use Nesting?
Nesting helps reduce repetitive code and keeps related styles together. It improves maintainability by grouping styles logically. Developers can easily understand the relationship between elements. It mirrors the HTML structure, making debugging easier. It also saves time while writing styles. However, it should be used carefully to avoid deep nesting. Clean code leads to better performance and scalability.
Example:
.header {
h1 {
font-size: 24px;
}
p {
color: gray;
}
}
Parent Selector (&) Usage
The & symbol in SCSS refers to the parent selector. It is useful for pseudo-classes and modifiers. This avoids rewriting the full selector. It helps create hover, focus, or active states easily. It keeps the code compact and readable. It is widely used in modern SCSS practices. Makes styling dynamic and efficient.
Example:
.button {
background: blue;
&:hover {
background: darkblue;
}
}
Nesting with Classes and IDs
You can nest classes and IDs inside parent selectors. This helps in targeting specific elements within a section. It avoids writing long selectors repeatedly. It improves readability and structure. It is useful for component-based design. Helps manage styles in large projects. Keeps code organized.
Example:
.card {
.title {
font-weight: bold;
}
#price {
color: green;
}
}
Avoid Over-Nesting
While nesting is powerful, too much nesting can make code complex. Deep nesting increases CSS specificity. It can lead to performance issues and difficult debugging. Best practice is to limit nesting to 3–4 levels. Keep your structure simple and clean. Avoid unnecessary parent-child chains. Always write maintainable code.
Bad Example:
.container {
.row {
.col {
.box {
.text {
color: red;
}
}
}
}
}
Nesting with Media Queries
SCSS allows nesting media queries inside selectors. This keeps responsive styles close to the component. It improves readability and organization. You don’t need separate media sections. It makes responsive design easier. Keeps everything in one place. Very useful in modern UI development.
Example:
.box {
width: 100%;
@media (max-width: 768px) {
width: 50%;
}
}
Real-Time Use Case
In real projects, nesting is used in components like navigation bars, cards, and forms. It helps structure styles clearly. Developers can easily manage UI sections. It reduces code duplication. Makes updates faster and easier. Improves team collaboration. Ensures scalable design.
Example:
.navbar {
background: #333;
ul {
list-style: none;
li {
display: inline-block;
a {
color: white;
&:hover {
color: yellow;
}
}
}
}
}
Final Tip
Use nesting wisely—keep it shallow, meaningful, and structured. Combine it with variables and mixins for even better SCSS development.

