Performance: First-child and last-child
LOW IMPACT
This affects the browser's style calculation and layout performance when selecting elements based on their position in the DOM.
li { color: black; }
li:first-child { color: red; }
li:last-child { color: blue; }li { color: black; }
li:first-child { color: red; }
li:last-child { color: blue; }
li:nth-child(n) { color: black; }| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| :first-child and :last-child on small lists | Minimal DOM traversal | 0 | Low | [OK] Good |
| :first-child and :last-child on large lists repeatedly | High DOM traversal per element | 0 | Medium | [!] OK |
| Using JavaScript to add classes instead | Extra DOM manipulation | 1 | Low | [!] OK |
| Avoiding positional selectors entirely | Minimal DOM traversal | 0 | Low | [OK] Good |