Performance: Why programmatic color control matters
Programmatic color control affects CSS bundle size and rendering speed by reducing repetitive color declarations and enabling efficient reuse.
$primary-color: #3498db; .button { background-color: $primary-color; } .header { border-color: $primary-color; } .alert { color: $primary-color; }
$primary-color: #3498db; .button { background-color: #3498db; } .header { border-color: #3498db; } .alert { color: #3498db; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Repeated color hex codes | N/A | Multiple style recalculations | Higher paint cost due to complex styles | [X] Bad |
| Color variables in Sass | N/A | Single style recalculation | Lower paint cost with simpler styles | [OK] Good |
$primary-color: #3498db;. Others use incorrect syntax from other languages or CSS.$base-color: #ff0000;
$light-color: lighten($base-color, 20%);
.button {
background-color: $light-color;
}lighten() function makes a color lighter by the given percentage.$main-color: #00ff00
.header {
color: $main-color;
}$main-color: #00ff00 misses the semicolon.Pass the test and this pattern moves unknown