0
0
Svelteframework~20 mins

Dynamic inline styles in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Dynamic Styles Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the rendered background color?
Consider this Svelte component that sets a dynamic background color based on a variable. What background color will the
have when rendered?
Svelte
<script>
  let color = 'blue';
</script>

<div style="background-color: {color}; width: 100px; height: 100px;"></div>
AThe div will have a transparent background color.
BThe div will have a red background color.
CThe div will have a blue background color.
DThe div will have no background color because the style syntax is invalid.
Attempts:
2 left
💡 Hint
Look at how the variable 'color' is used inside the style attribute with curly braces.
📝 Syntax
intermediate
2:00remaining
Which option correctly applies a dynamic font size?
You want to set the font size dynamically in a Svelte component using a variable 'size' that holds a number representing pixels. Which option correctly applies this style?
Svelte
let size = 24;
A<p style="font-size: size + 'px';">Text</p>
B<p style="font-size: {size}px;">Text</p>
C<p style="font-size: '{size}px';">Text</p>
D<p style="font-size: {size};">Text</p>
Attempts:
2 left
💡 Hint
Remember to use curly braces to insert variables and include units like 'px' as part of the string.
state_output
advanced
2:00remaining
What is the background color after clicking the button twice?
This Svelte component toggles the background color between 'red' and 'green' each time the button is clicked. What is the background color after two clicks?
Svelte
<script>
  let isRed = true;
  function toggle() {
    isRed = !isRed;
  }
</script>

<div style="background-color: {isRed ? 'red' : 'green'}; width: 100px; height: 100px;"></div>
<button on:click={toggle}>Toggle Color</button>
AThe background color is red.
BThe background color is green.
CThe background color is blue.
DThe background color does not change.
Attempts:
2 left
💡 Hint
Each click flips the boolean 'isRed'. Think about the initial value and how it changes after two toggles.
🔧 Debug
advanced
2:00remaining
Why does this dynamic style not update on variable change?
This Svelte component tries to update the text color dynamically, but the color does not change when the button is clicked. What is the cause?
Svelte
<script>
  let color = 'black';
  function changeColor() {
    color = 'blue';
  }
</script>

<p style="color: {color};">Hello</p>
<button on:click={changeColor}>Change Color</button>
AThe variable 'color' is not declared with let.
BSvelte does not support dynamic inline styles.
CThe on:click event is not correctly attached to the button.
DThe style uses quotes around 'color', so it is treated as a string, not a variable.
Attempts:
2 left
💡 Hint
Check how the variable is used inside the style attribute.
🧠 Conceptual
expert
3:00remaining
How does Svelte optimize dynamic inline styles?
Svelte compiles components to efficient JavaScript. How does it handle dynamic inline styles to minimize DOM updates?
ASvelte updates only the changed style properties on the element instead of replacing the entire style attribute.
BSvelte re-renders the entire component whenever any style changes.
CSvelte uses inline CSS classes instead of style attributes for dynamic styles.
DSvelte does not optimize dynamic styles and updates the whole style attribute every time.
Attempts:
2 left
💡 Hint
Think about how frameworks avoid unnecessary DOM changes for better performance.