Performance: Why logic in stylesheets is needed
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 |
$is-primary: true;
button {
@if $is-primary {
background-color: blue;
} @else {
background-color: gray;
}
}$font-size: 16px
body {
font-size: $font-size;
}$theme. Which approach best uses logic to handle multiple themes?Pass the test and this pattern moves unknown