Performance: Avoiding deep nesting
Deep CSS nesting affects how quickly browsers match styles to elements, impacting page rendering speed.
Jump into concepts and practice - no test required
.nav-link { color: red; }
nav ul li a span strong em { color: red; }| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Deeply nested selector (e.g., nav ul li a span strong em) | High (many DOM levels checked) | 0 (no layout change) | Low | [X] Bad |
| Shallow class selector (e.g., .nav-link) | Low (direct match) | 0 | Low | [OK] Good |
<a> be with this CSS?nav ul li a { color: green; }
nav ul li a span { color: red; }<nav>
<ul>
<li><a>Link <span>Text</span></a></li>
</ul>
</nav>a tag text is green by nav ul li a { color: green; }.span inside a has color red from nav ul li a span { color: red; }, overriding green..container .header .nav .item .link {
color: blue;
}.nav-link reduces nesting and keeps CSS clear..nav-link instead of chaining many classes -> Option A.nav-link instead of chaining many classes [OK].card .header .title {
font-size: 1.5rem;
}
.card .header .subtitle {
font-size: 1rem;
}.card-header-title and .card-header-subtitle and use simple selectors like .card-header-title { font-size: 1.5rem; }..card-header-title and .card-header-subtitle with simple selectors -> Option C.card-header-title and .card-header-subtitle with simple selectors [OK]