0
0
Svelteframework~20 mins

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

Choose your learning style9 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.