Performance: Why logic in stylesheets is needed
MEDIUM IMPACT
Using logic in stylesheets affects how quickly styles are generated and applied, impacting page load and rendering speed.
$colors: (blue, green); @each $color in $colors { .button-#{$color} { color: $color; } } // Generates styles dynamically with less repetition
$primary-color: blue; $secondary-color: green; .button-blue { color: blue; } .button-green { color: green; } // Repeated styles for each color variant
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Repeated static styles | No extra DOM ops | No extra reflows | Higher paint cost due to larger CSS | [X] Bad |
| Logic-generated concise styles | No extra DOM ops | No extra reflows | Lower paint cost due to smaller CSS | [OK] Good |