Performance: Minimizing nesting depth
This affects CSS parsing speed, style calculation, and rendering performance by reducing selector complexity and browser workload.
Jump into concepts and practice - no test required
.parent { color: red; } .child { color: red; } .grandchild { color: red; }
.parent { .child { .grandchild { color: red; } } }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Deeply nested Sass selectors | No extra DOM nodes but complex selectors | Triggers multiple reflows if styles change | Higher paint cost due to slower style matching | [X] Bad |
| Flat Sass selectors with minimal nesting | No extra DOM nodes, simple selectors | Single reflow on style change | Lower paint cost with faster style matching | [OK] Good |
.card {
border: 1px solid #ccc;
.title {
font-weight: bold;
}
&.featured {
border-color: gold;
}
}.menu {
ul {
li {
a {
color: red;
}
}
}
}