Performance: Importance of order
The order of CSS rules affects how quickly the browser applies styles and can impact layout stability and rendering speed.
Jump into concepts and practice - no test required
/* CSS with ordered rules to avoid overrides and recalculations */ .container { display: block; } .container { display: flex; } .button { color: blue; } .button { color: red; font-weight: bold; }
/* CSS with unordered rules causing overrides and recalculations */ .button { color: blue; } .button { color: red; font-weight: bold; } .container { display: flex; } .container { display: block; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Unordered CSS rules with frequent overrides | Multiple style recalculations | Multiple reflows triggered | Higher paint cost due to layout shifts | [X] Bad |
| Ordered CSS rules minimizing overrides | Single style recalculation | Minimal reflows | Lower paint cost with stable layout | [OK] Good |
div when both rules have the same specificity?<p> appear?p { color: green; } p { color: orange; } p { color: blue; }.box { color: red; } .box { color: blue; }button to have a green background normally, but red when hovered. You write:button { background-color: green; } button:hover { background-color: red; } button { background-color: blue; }