Performance: Common CSS anti-patterns
This affects page load speed, rendering time, and visual stability by causing unnecessary style recalculations and layout thrashing.
Jump into concepts and practice - no test required
.link:hover { color: red; }
div.container > ul li.item > a.link:hover { color: red; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Complex selectors | High (many elements matched) | Medium | Medium | [X] Bad |
| Simple class selectors | Low | Low | Low | [OK] Good |
| Layout-triggering CSS changes | Medium | High (multiple reflows) | High | [X] Bad |
| Transform-based animations | Low | Low | Low | [OK] Good |
| Universal selectors | High | Medium | Medium | [X] Bad |
| Targeted selectors | Low | Low | Low | [OK] Good |
| Frequent !important use | Medium | Medium | Medium | [X] Bad |
| Proper cascade and specificity | Low | Low | Low | [OK] Good |
!important!important forces styles to override others, which can cause confusion and difficulty in debugging.!important overuse is a known anti-pattern.!important excessively to override styles -> Option B!important = Anti-pattern [OK]!important unless absolutely necessary [OK]!important is always good for quick fixesbutton {
width: 300px;
padding: 1rem;
background-color: lightblue;
}Consider the anti-pattern of fixed widths.
.container {
color: red !important;
}
.container {
color: blue;
}color: red !important; overrides any later declarations without !important.color: blue; is ignored because the first has !important, causing an anti-pattern of forced overrides.!important overrides later rules [OK]