Performance: CSS custom properties (variables)
CSS custom properties affect style calculation and paint stages, influencing how quickly styles update and how stable the layout remains.
:root { --main-color: blue; } .button { color: var(--main-color); } /* Change --main-color to update all */:root { --main-color: blue; } .button { color: blue; } /* Changing color by updating all selectors */| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Hardcoded colors in many selectors | None | Multiple reflows if layout changes | High paint cost on updates | [X] Bad |
| Using CSS custom properties for colors | None | Single reflow if layout unaffected | Low paint cost on updates | [OK] Good |
--main-color with the value blue inside a Svelte component's style block?--main-color: blue;.:root selector defines variables globally in CSS.<p> tag appear?<style>
:root {
--text-color: red;
}
p {
color: var(--text-color);
}
</style>
<p>Hello World</p>--text-color is set to red inside :root, making it globally available.p selectorvar(--text-color), which uses the variable value red.<style>
:root {
--bg-color: #eee
}
div {
background-color: var(--bg-color);
}
</style>--bg-color: #eee is missing a semicolon.var() usage is valid, and property name background-color is correct.--btn-bg. You want to allow the parent component to set this variable dynamically. Which approach correctly allows this dynamic styling?<!-- Child.svelte -->
<script>
export let bgColor = 'lightblue';
</script>
<button style="background-color: var(--btn-bg);">Click Me</button>
<style>
button {
color: white;
}
</style>--btn-bg on the child component's root element allows the child's styles to use it.<Child style="--btn-bg: green;" />, so the child's CSS picks it up.Pass the test and this pattern moves unknown