Performance: Variable interpolation with #{}
This affects CSS generation time and the final CSS file size, impacting page load speed.
$state: active; .button-#{$state} { color: red; } .button-#{$state}:hover { color: blue; }
$state: active; .button-#{$state} { color: red; } .button-#{$state}:hover { color: blue; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Excessive interpolation generating many selectors | No direct DOM impact | No reflows | Higher paint cost due to complex styles | [X] Bad |
| Minimal interpolation with pseudo-classes | No direct DOM impact | No reflows | Lower paint cost with simpler styles | [OK] Good |
#{} syntax do in Sass?#{} in Sass#{} syntax is used to insert or interpolate variables inside strings or selectors, making styles dynamic.#{} around the variable name to insert its value inside selectors..#{$size} correctly. Options A, C, and D miss the # or curly braces or both, so they are invalid.$color: red;
$size: large;
.button-#{$size} {
color: #{$color};
}$size is 'large', so .button-#{$size} becomes .button-large. The variable $color is 'red', so color: #{$color} becomes color: red..button-large { color: red; }.$name: "header";
.#$name {
font-weight: bold;
}.$name but misses #{} for interpolation, so Sass treats it as a literal string, causing an error..#{$name} to insert the variable value inside the selector.$sizes: small, medium, large;
@each $size in $sizes {
.btn-#{$size} {
font-size: if($size == small, 1rem, if($size == medium, 1.5rem, 2rem));
}
}.btn-large in the generated CSS?large, the condition $size == small is false, $size == medium is false, so it uses the else value 2rem.Pass the test and this pattern moves unknown