What if you could change your entire website's look by editing just one line of code?
Why CSS custom properties (variables) in Svelte? - Purpose & Use Cases
Imagine you want to change the main color of your whole website. You have to find and update every place in your CSS where that color is used.
Manually changing colors everywhere is slow and easy to miss spots. It can cause inconsistent styles and lots of frustration.
CSS custom properties let you define a color once as a variable and reuse it everywhere. Changing the variable updates all uses instantly.
body { color: #3498db; } h1 { color: #3498db; } button { background: #3498db; }:root { --main-color: #3498db; } body { color: var(--main-color); } h1 { color: var(--main-color); } button { background: var(--main-color); }You can easily create themes and update styles globally with just one change.
A website that switches between light and dark mode by changing a few CSS variables instead of rewriting all styles.
Manually updating repeated styles is slow and error-prone.
CSS custom properties let you store values once and reuse them.
Changing a variable updates all related styles instantly.
Practice
Solution
Step 1: Understand CSS custom properties
CSS custom properties allow you to define variables in CSS that store values like colors or sizes.Step 2: Recognize their use in Svelte
In Svelte, these variables can be used inside style blocks to reuse and dynamically change styles.Final Answer:
To store reusable values that can be changed dynamically in CSS -> Option CQuick Check:
CSS variables = reusable style values [OK]
- Thinking CSS variables run JavaScript
- Confusing CSS variables with importing files
- Believing CSS variables create HTML elements
--main-color with the value blue inside a Svelte component's style block?Solution
Step 1: Identify correct CSS variable syntax
CSS variables start with two dashes and use a colon to assign values, e.g.,--main-color: blue;.Step 2: Recognize the proper selector for global variables
The:rootselector defines variables globally in CSS.Final Answer:
:root { --main-color: blue; } -> Option DQuick Check:
Correct CSS variable syntax uses colon and double dashes [OK]
- Using equals sign (=) instead of colon (:)
- Omitting the double dashes (--)
- Defining variable without a selector
<p> tag appear?<style>
:root {
--text-color: red;
}
p {
color: var(--text-color);
}
</style>
<p>Hello World</p>Solution
Step 1: Check the CSS variable definition
The variable--text-coloris set toredinside:root, making it globally available.Step 2: Check the usage in the
The paragraph text color is set topselectorvar(--text-color), which uses the variable valuered.Final Answer:
Red -> Option AQuick Check:
CSS variable value = red [OK]
- Assuming default black color instead of variable
- Confusing variable name or value
- Ignoring :root scope
<style>
:root {
--bg-color: #eee
}
div {
background-color: var(--bg-color);
}
</style>Solution
Step 1: Check CSS syntax in :root block
CSS requires a semicolon after each property value. The line--bg-color: #eeeis missing a semicolon.Step 2: Verify other syntax elements
Variable name syntax with dashes is correct,var()usage is valid, and property namebackground-coloris correct.Final Answer:
Missing semicolon after #eee in :root block -> Option AQuick Check:
CSS properties need semicolons [OK]
- Forgetting semicolons after CSS declarations
- Thinking variable names can't have dashes
- Believing var() is invalid in Svelte styles
--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>How should the parent component pass the background color to the child using CSS custom properties?
Solution
Step 1: Understand CSS variable scope in Svelte
CSS custom properties can be set on elements and inherited by children. Setting--btn-bgon the child component's root element allows the child's styles to use it.Step 2: Recognize how to pass CSS variables from parent
The parent can set the CSS variable via inline style on the child component tag, e.g.,<Child style="--btn-bg: green;" />, so the child's CSS picks it up.Final Answer:
Set --btn-bg on the child component's element using inline style: <Child style="--btn-bg: green;" /> -> Option BQuick Check:
Pass CSS variables via inline style on component [OK]
- Trying to set CSS variables only inside child styles
- Ignoring CSS variable inheritance and scope
- Using props without connecting to CSS variables
Ready to prove it?
Pass the test and this pattern moves unknown
