Bird
Raised Fist0
Svelteframework~20 mins

CSS custom properties (variables) in Svelte - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
CSS Variable Mastery in Svelte
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
How does this Svelte component apply CSS custom properties?

Consider this Svelte component using CSS variables. What color will the text inside the <p> tag be?

Svelte
<script>
  let themeColor = 'blue';
</script>

<style>
  :root {
    --main-color: red;
  }
  p {
    color: var(--main-color);
  }
  .dynamic {
    --main-color: {themeColor};
  }
</style>

<div class="dynamic">
  <p>Hello world!</p>
</div>
AThe text color will be red because :root sets --main-color to red.
BThe text color will be blue because the .dynamic class overrides --main-color to blue.
CThe text color will be black because CSS variables cannot be set dynamically in Svelte styles.
DThe text color will be the browser default because var(--main-color) is invalid here.
Attempts:
2 left
💡 Hint

Remember that CSS custom properties cascade and can be overridden by classes on parent elements.

📝 Syntax
intermediate
1:30remaining
Which CSS custom property syntax is correct in Svelte style block?

Which option correctly defines and uses a CSS custom property inside a Svelte <style> block?

A:root { --bg-color: green; } div { background-color: var(--bg-color); }
B:root { bg-color: green; } div { background-color: var(bg-color); }
C:root { --bg-color = green; } div { background-color: var(--bg-color); }
D:root { --bg-color: green; } div { background-color: --bg-color; }
Attempts:
2 left
💡 Hint

CSS custom properties start with two dashes and use var(--property-name) to access.

❓ state_output
advanced
2:00remaining
What is the background color after updating a CSS variable in Svelte?

Given this Svelte component, what background color will the <div> have after clicking the button once?

Svelte
<script>
  let color = 'red';
  function changeColor() {
    color = 'green';
  }
</script>

<style>
  div {
    background-color: var(--bg-color, blue);
    width: 100px;
    height: 100px;
  }
</style>

<div style="--bg-color: {color}"></div>
<button on:click={changeColor}>Change Color</button>
AThe background color stays blue because CSS variables cannot be updated dynamically.
BThe background color changes from blue to green after clicking the button.
CThe background color changes from red to green after clicking the button.
DThe background color stays red because inline styles do not update with Svelte variables.
Attempts:
2 left
💡 Hint

Inline styles with CSS variables can update reactively when bound to Svelte variables.

🔧 Debug
advanced
2:00remaining
Why does this CSS variable not apply in Svelte?

Look at this Svelte component. Why does the text color not change to green?

Svelte
<script>
  let textColor = 'green';
</script>

<style>
  p {
    color: var(--text-color);
  }
</style>

<p style="text-color: {textColor}">Hello!</p>
ABecause the variable <code>textColor</code> is not reactive.
BBecause <code>text-color</code> is not a valid CSS property; it should be <code>color</code>.
CBecause Svelte does not support CSS variables inside style attributes.
DBecause the CSS variable <code>--text-color</code> is never defined in any style or inline style.
Attempts:
2 left
💡 Hint

Check if the CSS variable --text-color is actually set anywhere.

🧠 Conceptual
expert
2:30remaining
How do CSS custom properties enable theming in Svelte apps?

Which statement best explains why CSS custom properties are useful for theming in Svelte applications?

AThey allow dynamic updates of style values at runtime without recompiling components, enabling theme switching easily.
BThey require JavaScript to update styles, so themes can only change on page reload.
CThey replace all CSS classes, so styles are only applied via variables, simplifying CSS management.
DThey prevent style inheritance, so each component must define all colors explicitly.
Attempts:
2 left
💡 Hint

Think about how CSS variables can be changed dynamically and cascade through the DOM.

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

  1. Step 1: Understand CSS custom properties

    CSS custom properties allow you to define variables in CSS that store values like colors or sizes.
  2. Step 2: Recognize their use in Svelte

    In Svelte, these variables can be used inside style blocks to reuse and dynamically change styles.
  3. Final Answer:

    To store reusable values that can be changed dynamically in CSS -> Option C
  4. 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

  1. 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;.
  2. Step 2: Recognize the proper selector for global variables

    The :root selector defines variables globally in CSS.
  3. Final Answer:

    :root { --main-color: blue; } -> Option D
  4. 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?

<style>
  :root {
    --text-color: red;
  }
  p {
    color: var(--text-color);
  }
</style>

<p>Hello World</p>
medium
A. Red
B. Blue
C. Black
D. Green

Solution

  1. Step 1: Check the CSS variable definition

    The variable --text-color is set to red inside :root, making it globally available.
  2. Step 2: Check the usage in the p selector

    The paragraph text color is set to var(--text-color), which uses the variable value red.
  3. Final Answer:

    Red -> Option A
  4. Quick Check:

    CSS variable value = red [OK]
Hint: var(--variable) uses the defined CSS variable value [OK]
Common Mistakes:
  • Assuming default black color instead of variable
  • Confusing variable name or value
  • Ignoring :root scope
4. Identify the error in this Svelte component style block that tries to use a CSS custom property:

<style>
  :root {
    --bg-color: #eee
  }
  div {
    background-color: var(--bg-color);
  }
</style>
medium
A. Missing semicolon after #eee in :root block
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

  1. 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.
  2. Step 2: Verify other syntax elements

    Variable name syntax with dashes is correct, var() usage is valid, and property name background-color is correct.
  3. Final Answer:

    Missing semicolon after #eee in :root block -> Option A
  4. 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?

<!-- 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?
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

  1. 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.
  2. 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.
  3. Final Answer:

    Set --btn-bg on the child component's element using inline style: <Child style="--btn-bg: green;" /> -> Option B
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard