Performance: Child 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
div > ul > li { color: red; }div ul li { color: red; }| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Descendant selector (e.g., div ul li) | High (matches many elements) | 0 (no layout change) | Low | [✗] Bad |
| Child selector (e.g., div > ul > li) | Low (matches fewer elements) | 0 (no layout change) | Low | [✓] Good |
> do?> targets only elements that are direct children of a specified parent.<li> elements that are direct children of a <ul>?> symbol between parent and child tags.<li> elements directly inside <ul>, use ul > li.<div>
<p>First</p>
<section>
<p>Second</p>
</section>
</div>div > p { color: red; }<p> elements will be red?<li> children of <ol> blue:ol > li { color: blue }<li> elements inside nested <ol> too. What is the problem?<ol> inside <li> creates new direct children <li> for the inner ol.<li> get coloredol > li matches direct children of any ol, including nested ones, so all nested li get colored.<span> elements that are direct children of <div class='container'>, but not <span> inside nested elements. Which CSS selector achieves this?<span> elements directly inside <div class='container'> should be styled, excluding nested spans.> selects direct children only, so .container > span matches only spans directly inside the container div.