Performance: Variable scope (global vs local)
Variable scope affects how and when styles are compiled, impacting CSS output size and compilation speed.
$color: red; .button { color: $color; } .card { $local-color: blue; color: $local-color; }
$color: red; .button { color: $color; } .card { $color: blue; color: $color; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Global variable overuse | No direct DOM impact | No reflows | Larger CSS can slow paint | [X] Bad |
| Local variable usage | No direct DOM impact | No reflows | Smaller CSS speeds paint | [OK] Good |
$color: red; declared outside any block means:$color: red; is declared outside any selector or block, making it global.$font-size inside a nested block?!global after the value.$font-size: 2rem !global; uses $font-size: 2rem !global; which is correct Sass syntax for updating global variables inside blocks.p text be in this Sass code?$color: blue;
p {
$color: red;
color: $color;
}p block, a local $color is set to red.color: $color;$padding: 10px;
.container {
$padding: 20px;
padding: $padding;
}$padding: 20px; inside .container block without !global, so it creates a local variable instead of updating the global one.$padding inside the block, !global must be added: $padding: 20px !global;.$margin after compilation?$margin: 5px;
.wrapper {
$margin: 10px !global;
.content {
$margin: 15px;
}
}
body {
margin: $margin;
}$margin is 5px globally. Inside .wrapper, $margin: 10px !global; updates the global variable to 10px..content.content, $margin: 15px; is local to that block and does not affect the global variable.bodybody selector uses the global $margin, which was updated to 10px.Pass the test and this pattern moves unknown