Performance: Token-driven color palettes
This affects CSS rendering speed and style recalculations by controlling how colors are applied and reused across the site.
Jump into concepts and practice - no test required
$color-primary: #3498db; $color-palette: ( primary: $color-primary, secondary: #2ecc71, accent: #e74c3c ); .button { background-color: map-get($color-palette, primary); } .card { border-color: map-get($color-palette, primary); } .alert { color: map-get($color-palette, primary); }
$primary-color: #3498db; .button { background-color: #3498db; } .card { border-color: #3498db; } .alert { color: #3498db; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Hardcoded repeated colors | No extra DOM nodes | Multiple reflows if colors change | Higher paint cost due to style thrashing | [X] Bad |
| Token-driven color palette | No extra DOM nodes | Single reflow on token change | Lower paint cost with stable styles | [OK] Good |
$primary-color: #3498db;. Others use invalid syntax.$color-primary: #ff0000;
.button {
background-color: $color-primary;
}$color-primary is set to #ff0000, which is red.$color-primary, so it will be red.$accent-color #00ff00;
.text {
color: $accent-color;
}$accent-color #00ff00; missing the colon after $accent-color.data-theme attribute on the body, with light mode as the default?$color-bg-light: #ffffff;
$color-bg-dark: #000000;
body {
background-color: $color-bg-light;
&[data-theme='dark'] {
background-color: $color-bg-dark;
}
} uses nesting with &[data-theme='dark'] inside body, which is valid Sass syntax.$color-bg-light: #ffffff;
$color-bg-dark: #000000;
body {
background-color: $color-bg-light;
&[data-theme='dark'] {
background-color: $color-bg-dark;
}
} sets light mode as default and overrides background for dark mode correctly.$color-bg-light: #ffffff;
$color-bg-dark: #000000;
body[data-theme='light'] {
background-color: $color-bg-light;
}
body[data-theme='dark'] {
background-color: $color-bg-dark;
} is valid CSS but not Sass nesting style; $color-bg-light: #ffffff;
$color-bg-dark: #000000;
body {
background-color: $color-bg-light;
}
body[data-theme='dark'] {
background-color: $color-bg-light;
} sets dark mode to light color (wrong); $color-bg-light: #ffffff;
$color-bg-dark: #000000;
body {
background-color: $color-bg-dark;
}
body[data-theme='light'] {
background-color: $color-bg-light;
} sets dark mode as default and light mode override, which is less common.