📖 Scenario: You are building a simple Svelte component for a website that needs consistent colors and spacing. Using CSS custom properties (variables) helps keep the design easy to update and maintain.
🎯 Goal: Create a Svelte component that uses CSS custom properties to define colors and spacing. Then apply these variables to style the component's elements.
📋 What You'll Learn
Create CSS custom properties for --main-color and --padding inside a <style> block
Set --main-color to #3498db and --padding to 1rem
Use the var(--main-color) and var(--padding) variables to style a <div> background color and padding
Add a heading inside the <div> that uses the custom color
💡 Why This Matters
🌍 Real World
CSS custom properties let you keep design consistent and easy to update across a website or app. Using them in Svelte components helps build reusable, maintainable UI parts.
💼 Career
Front-end developers often use CSS variables with frameworks like Svelte to create scalable styles and dynamic themes. This skill is important for building modern, maintainable web apps.
Progress0 / 4 steps
1
Create CSS custom properties
Inside the <style> block, create CSS custom properties --main-color set to #3498db and --padding set to 1rem inside the :root selector.
Svelte
Hint
Use the :root selector to define global CSS variables. Write --main-color: #3498db; and --padding: 1rem; inside it.
2
Add a config variable for the background color
Create a backgroundColor variable in the <script> block and set it to var(--main-color) as a string.
Svelte
Hint
Inside the <script> block, write let backgroundColor = "var(--main-color)"; to store the CSS variable as a string.
3
Apply CSS variables to style the div
In the <style> block, style the div to use background-color: var(--main-color); and padding: var(--padding);.
Svelte
Hint
Inside the div style, write background-color: var(--main-color); and padding: var(--padding); to use the CSS variables.
4
Use the backgroundColor variable in the div style attribute
Add a style attribute to the div element and set it to background-color: {backgroundColor} to use the variable from the script.
Svelte
Hint
Add style="background-color: {backgroundColor}" to the div tag to apply the script variable as inline style.
Practice
(1/5)
1. What is the main purpose of CSS custom properties (variables) in a Svelte component?
easy
A. To import external CSS files into the component
B. To write JavaScript code inside CSS
C. To store reusable values that can be changed dynamically in CSS
D. To create new HTML elements automatically
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 C
Quick Check:
CSS variables = reusable style values [OK]
Hint: CSS variables hold reusable style values [OK]
Common Mistakes:
Thinking CSS variables run JavaScript
Confusing CSS variables with importing files
Believing CSS variables create HTML elements
2. Which of the following is the correct syntax to define a CSS custom property named --main-color with the value blue inside a Svelte component's style block?
easy
A. div { --main-color: blue }
B. style { --main-color = blue; }
C. :root { main-color: blue; }
D. :root { --main-color: blue; }
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 :root selector defines variables globally in CSS.
Final Answer:
:root { --main-color: blue; } -> Option D
Quick Check:
Correct CSS variable syntax uses colon and double dashes [OK]
Hint: Use :root and colon for CSS variables [OK]
Common Mistakes:
Using equals sign (=) instead of colon (:)
Omitting the double dashes (--)
Defining variable without a selector
3. Given the following Svelte component style and markup, what color will the text inside the <p> tag appear?
B. Incorrect variable name syntax, should not use dashes
C. Cannot use var() inside Svelte style blocks
D. Background color property name is wrong
Solution
Step 1: Check CSS syntax in :root block
CSS requires a semicolon after each property value. The line --bg-color: #eee is missing a semicolon.
Step 2: Verify other syntax elements
Variable name syntax with dashes is correct, var() usage is valid, and property name background-color is correct.
Final Answer:
Missing semicolon after #eee in :root block -> Option A
Quick Check:
CSS properties need semicolons [OK]
Hint: Check for missing semicolons in CSS blocks [OK]
Common Mistakes:
Forgetting semicolons after CSS declarations
Thinking variable names can't have dashes
Believing var() is invalid in Svelte styles
5. In a Svelte component, you want to create a button whose background color changes based on a CSS custom property --btn-bg. You want to allow the parent component to set this variable dynamically. Which approach correctly allows this dynamic styling?
How should the parent component pass the background color to the child using CSS custom properties?
hard
A. Pass bgColor prop and use it directly in the child's style attribute without CSS variables
B. Set --btn-bg on the child component's element using inline style: <Child style="--btn-bg: green;" />
C. Define --btn-bg inside the child's <style> block only
D. Use JavaScript to change the button's background color instead of CSS variables
Solution
Step 1: Understand CSS variable scope in Svelte
CSS custom properties can be set on elements and inherited by children. Setting --btn-bg on 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 B
Quick Check:
Pass CSS variables via inline style on component [OK]
Hint: Pass CSS variables via inline style on component tag [OK]
Common Mistakes:
Trying to set CSS variables only inside child styles
Ignoring CSS variable inheritance and scope
Using props without connecting to CSS variables
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.