Performance: Universal selector
The universal selector affects the style calculation and layout phases by applying styles to all elements, which can slow down rendering.
Jump into concepts and practice - no test required
body, h1, p, ul, li { margin: 0; padding: 0; }* { margin: 0; padding: 0; }| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Universal selector (*) | All elements matched | Triggers reflow for all elements | High paint cost due to many styled nodes | [X] Bad |
| Specific element selectors | Only targeted elements matched | Reflow limited to fewer elements | Lower paint cost | [OK] Good |
* do?* symbol in CSS means "all elements" without exception.* targets every element on the page, regardless of tag, class, or ID.* without any prefix like dot or hash.* { margin: 0; }.* { padding: 10px; }
p { padding: 5px; }<p>) element?* applies padding 10px to all elements, but the p selector is more specific.p selector is more specific than *, the paragraph's padding will be 5px, overriding the universal selector.* { font-size: 16px }* { font-size: 16px } is syntactically valid; a trailing semicolon is optional at the end of a rule.<a>) with no margin and 5px padding. Which CSS achieves this?* { margin: 0; padding: 0; } sets margin and padding to zero for all elements.a { padding: 5px; } specifically sets padding to 5px for all <a> elements, overriding the universal selector.