Performance: Nesting depth and best practices
This affects CSS parsing speed, style calculation, and layout performance in the browser.
$button-color: blue; .button { color: $button-color; } .button-icon-path { fill: red; }
$button-color: blue; .button { color: $button-color; .icon { svg { path { fill: red; } } } }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Deeply nested selectors | No extra DOM nodes but complex matching | Multiple reflows if styles change | Higher paint cost due to complex styles | [X] Bad |
| Flat, simple selectors | No extra DOM nodes, simple matching | Single reflow on style change | Lower paint cost | [OK] Good |
.button class inside a .container class?.nav {
ul {
li {
color: red;
}
}
}What is the compiled CSS output?.card {
.header {
.title {
font-weight: bold;
}
}
.footer {
color: gray;
}
.content {
padding: 10px;
.text {
font-size: 14px;
}
}
}.nav {
ul {
li {
a {
color: black;
}
ul {
li {
a {
color: gray;
}
}
}
}
}
}Pass the test and this pattern moves unknown