Performance: Descendant selector
This affects how quickly the browser matches CSS rules to elements, impacting style calculation and rendering speed.
Jump into concepts and practice - no test required
.menu-link { color: red; }
div ul li a span { color: red; }| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Deep descendant selector (e.g., div ul li a span) | High (checks multiple ancestors) | 0 (no layout change) | Low | [X] Bad |
| Class selector (e.g., .menu-link) | Low (direct match) | 0 (no layout change) | Low | [OK] Good |
div p do?div and p means select p elements inside div elements at any depth.> and selects only direct children, but here it's a space, so all nested p inside div are selected.<span> elements inside <section> elements using a descendant selector?<div>
<article>
<p>Hello</p>
</article>
<p>World</p>
</div>div p { color: red; }<p> elements will be red?div p selects all p elements inside any div, at any depth.p elements are inside the div: one inside article (nested), one directly inside div.ul li a { text-decoration: none; }ul li a correctly targets a inside li inside ul.<em> elements inside <article> elements, but only if they are inside a <section> inside that <article>. Which CSS selector correctly targets this?em is inside section, which is inside article. So the order is article then section then em.article section em which matches em inside section inside article. section article em reverses order incorrectly. article > section > em uses child selectors which may be too strict. article em section is invalid order.