Performance: Complex selector combinations
This affects how fast the browser matches CSS rules to HTML elements during style calculation, impacting page load and rendering speed.
Jump into concepts and practice - no test required
.highlighted-strong { color: red; }
div > ul li:first-child > a:hover span.highlighted strong
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Complex descendant and pseudo-class selectors | Many DOM traversals | 0 | Low | [X] Bad |
| Single class or ID selectors | Minimal DOM checks | 0 | Low | [OK] Good |
<li> elements that are direct children of <ul> elements?> means direct child in CSS selectors.ul > li selects only li elements that are immediate children of ul.<p> element immediately following an <h2> element?+ selector targets the element immediately after the first sibling.h2 + p selects the p that comes right after an h2.<div> <p class="intro">Hello</p> <span>World</span> <p>Again</p> </div>
<p> element inside the <div>?div, there are two p elements separated by a span.div > p + p selects a p immediately following another p as a direct child of div. Here, the second p is not immediately after the first p (there is a span in between), so this selector matches nothing.div p ~ p selects any p siblings after a p inside div. The second p is a sibling after the first p, so this matches the second p.div p ~ p correctly selects the second p element.section > article + p<p> elements that are direct children of section?section > article + p means: select any p element that is immediately after an article which is a direct child of section.p elements that are direct children of section. This selector only selects p elements that come immediately after an article child, missing other p children.<li> elements that are inside a <ul> with class menu, but only if the <li> is not the first child. Which CSS selector achieves this?li elements inside ul.menu but exclude the first child li.ul.menu > li:not(:first-child) selects all direct li children of ul.menu except the first one.ul.menu li + li selects every li immediately following another li, which also works but only for adjacent siblings.ul.menu > li ~ li selects all li siblings after the first li as direct children, which also works.ul.menu > li:not(:first-child).