Challenge - 5 Problems
Dynamic Styles Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2: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>
Attempts:
2 left
💡 Hint
Look at how the variable 'color' is used inside the style attribute with curly braces.
✗ Incorrect
In Svelte, you can use curly braces inside attribute values to insert JavaScript expressions. Here, {color} inserts the string 'blue', so the div's background color becomes blue.
📝 Syntax
intermediate2: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;
Attempts:
2 left
💡 Hint
Remember to use curly braces to insert variables and include units like 'px' as part of the string.
✗ Incorrect
Option B correctly uses curly braces to insert the variable 'size' and appends 'px' outside the braces. This results in a valid CSS value like '24px'. Other options either treat the variable as a string or omit units.
❓ state_output
advanced2: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>
Attempts:
2 left
💡 Hint
Each click flips the boolean 'isRed'. Think about the initial value and how it changes after two toggles.
✗ Incorrect
Initially, isRed is true, so background is red. After one click, isRed becomes false (green). After the second click, isRed flips back to true (red). So after two clicks, background is red again.
🔧 Debug
advanced2: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>
Attempts:
2 left
💡 Hint
Check how the variable is used inside the style attribute.
✗ Incorrect
Putting 'color' in quotes inside the style attribute makes it a literal string, so the style is always 'color' instead of the variable's value. Removing the quotes and using {color} fixes the issue.
🧠 Conceptual
expert3: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?
Attempts:
2 left
💡 Hint
Think about how frameworks avoid unnecessary DOM changes for better performance.
✗ Incorrect
Svelte generates code that updates only the specific style properties that changed, avoiding full attribute replacements. This reduces DOM operations and improves performance.