Performance: Variable declaration with $
This affects CSS compilation speed and final CSS file size, impacting page load speed.
$primary-color: #3498db; .button { background-color: $primary-color; border: 1px solid $primary-color; }
$primary-color: #3498db; .button { background-color: #3498db; border: 1px solid #3498db; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Using repeated color literals | 0 | 0 | Slightly higher due to larger CSS | [X] Bad |
| Using $ variables for colors | 0 | 0 | Smaller CSS size, no runtime cost | [OK] Good |
$ symbol to distinguish them from other values.$ is used for variables in Sass.$, followed by the name, a colon, then the value, ending with a semicolon.$main-color: #3498db;. Others use wrong symbols or order.$btn-bg: #ff0000;
button {
background-color: $btn-bg;
}$btn-bg is assigned the color #ff0000, which is red.$btn-bg, so it will be red.$font-size 16px;
: between the name and value, like $font-size: 16px;.$spacing: 1rem;
@media (min-width: 600px) {
$spacing: 2rem;
}
.container {
padding: $spacing;
}$spacing inside @media does not work; Sass processes variables before CSS is generated.Pass the test and this pattern moves unknown