Performance: Why nesting mirrors HTML structure
This affects CSS selector matching speed and browser style calculation during page rendering.
ul {
li {
a {
span {
color: red;
}
}
}
}div ul li a span {
color: red;
}| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Deep flat selector (div ul li a span) | High (many elements checked) | Multiple on style change | Moderate | [X] Bad |
| Nested selector mirroring HTML (ul li a span) | Lower (follows DOM tree) | Fewer reflows | Lower | [OK] Good |
nav element containing ul and li elements?nav { ul { li { ... } } }.ul and li together with a comma.header {
nav {
ul {
li {
color: red;
}
}
}
}What is the equivalent CSS output?header { nav { ul { li { ... } } } } become header nav ul li in CSS..container {
.item {
color: blue;
}
.item {
font-weight: bold;
}
}.item blocks nested inside .container, causing repeated CSS rules..container .item, which can be combined for cleaner code.section with multiple article elements, each containing h2 and p. How does nesting in Sass help you write this cleanly?section contains multiple article elements, each with h2 and p inside.article inside section, then h2 and p inside article groups styles clearly and matches HTML.article, h2, and p inside section, reflecting HTML structure -> Option DpPass the test and this pattern moves unknown