Performance: Using variables
Using CSS variables affects the style calculation and paint stages by enabling reuse of values without repeating code.
Jump into concepts and practice - no test required
:root { --main-color: #3498db; }
.button { color: var(--main-color); }
.header { background-color: var(--main-color); }
.footer { border-color: var(--main-color); }:root { }
.button { color: #3498db; }
.header { background-color: #3498db; }
.footer { border-color: #3498db; }| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Repeating color values | None | Multiple style recalculations if changed | Moderate paint cost if styles change | [X] Bad |
| Using CSS variables | None | Single style recalculation on variable change | Lower paint cost due to fewer recalculations | [OK] Good |
:root { --main-color: #ff0000; } p { color: var(--main-color); }:root { --font-size 16px; } h1 { font-size: var(--font-size); }