Performance: Avoiding over-nesting
This affects CSS selector matching speed and browser rendering performance by reducing complexity in style calculations.
.parent { color: blue; } .child { color: green; } .grandchild { color: red; }
& .parent { & .child { & .grandchild { color: red; } } }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Deeply nested selectors | No extra DOM nodes but complex matching | 0 | Higher due to slower style calculation | [X] Bad |
| Flat selectors with minimal nesting | No extra DOM nodes, simple matching | 0 | Lower due to faster style calculation | [OK] Good |
.menu { color: blue; .item { font-size: 1rem; } } nests 1 level inside .menu, which is acceptable. Options B, C, and D nest 2, 3, and 4 levels respectively..menu { color: blue; .item { font-size: 1rem; } } stays within 2 levels of nesting, avoiding over-nesting..menu { color: blue; .item { font-size: 1rem; } } [OK].card {
background: white;
.title {
font-weight: bold;
}
}.nav {
ul {
li {
a {
color: blue;
}
}
}
}.section { .card { .button { background: green; } } } is discouraged; flat or shallow nesting is better..section { .card { &-button { background: green; } } }'s use of &-button.section { .card { &-button { background: green; } } } nests only 2 levels and uses the parent selector & to create .card-button, keeping nesting shallow and naming clear..section-card-button { background: green; } flattens all into one class, losing semantic structure. .section { .card-button { background: green; } } nests only 1 level but creates a combined class that may not reflect HTML structure well.Pass the test and this pattern moves unknown