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.
✗ 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
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.
✗ 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
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?
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
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.
✗ 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
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.
✗ Incorrect
Svelte generates code that updates only the specific style properties that changed, avoiding full attribute replacements. This reduces DOM operations and improves performance.
Practice
(1/5)
1. What is the correct way to apply a dynamic background color in a Svelte component using inline styles?
easy
A. <div style="background-color: {color};"></div>
B. <div style="background-color: $color;"></div>
C. <div style="background-color: color;"></div>
D. <div style="background-color: {{color}};"></div>
Solution
Step 1: Understand Svelte's style binding syntax
Svelte uses curly braces inside the style attribute to insert dynamic values, like {color}.
Step 2: Identify the correct syntax for inline styles
The correct syntax is style="background-color: {color};". Other options use incorrect variable interpolation.
Final Answer:
<div style="background-color: {color};"></div> -> Option A
C. Width and height 100px, no background color applied
D. Width and height 100px, background color literal string 'red'
Solution
Step 1: Analyze the style attribute values
The width and height use correct dynamic values with {size}px. The background-color: 'red' is a static value with single quotes.
Step 2: Understand rendering in Svelte and CSS
After compilation, the style becomes "width: 100px; height: 100px; background-color: 'red'". Browsers parse 'red' as the color keyword red, applying the background color correctly. No syntax error.
Final Answer:
Width and height 100px, background color red -> Option B
Quick Check:
Quoted CSS color keywords render correctly [OK]
Hint: CSS accepts quoted color keywords in style attributes [OK]
Common Mistakes:
Thinking quotes around colors cause syntax errors
Believing quoted keywords don't apply as colors
Confusing HTML attribute quoting with CSS parsing
4. Identify the error in this Svelte code snippet:
<script>
let color = 'blue';
</script>
<div style="color: {color}">Text</div>
medium
A. Missing semicolon after {color}
B. Curly braces not allowed inside style attribute
C. Style attribute should use single quotes instead of double quotes
D. No error, code works correctly
Solution
Step 1: Review Svelte style binding syntax
Using curly braces inside the style attribute to insert a variable is correct syntax in Svelte.
Step 2: Check for syntax errors
The style string "color: {color}" is valid. Semicolons are optional if only one style is present. Quotes around the attribute are standard HTML.
Final Answer:
No error, code works correctly -> Option D
Quick Check:
Curly braces inside style attribute are valid [OK]
Hint: Curly braces inside style attribute are valid for dynamic values [OK]
Common Mistakes:
Thinking semicolon is mandatory for single style
Believing quotes type affects dynamic style syntax
Assuming curly braces are not allowed in style
5. You want to create a Svelte component that changes the text color dynamically based on a status variable with values 'success', 'warning', or 'error'. Which is the best way to apply dynamic inline styles for this?
hard
A. <div style="color: {status === 'success' ? 'green' : status === 'warning' ? 'orange' : 'red'}">Status</div>
B. <div style="color: {status == 'success' & 'green' || status == 'warning' && 'orange' || 'red'}">Status</div>
Step 1: Understand dynamic inline style with conditional expressions
<div style="color: {status === 'success' ? 'green' : status === 'warning' ? 'orange' : 'red'}">Status</div> uses a nested ternary operator inside curly braces to select the correct color string based on status.
Step 2: Evaluate other options for correctness and best practice
<div style="color: {status == 'success' && 'green' || status == 'warning' && 'orange' || 'red'}">Status</div> uses logical operators which can cause unexpected results. <div class={status} >Status</div> <style>.success { color: green; } .warning { color: orange; } .error { color: red; }</style> uses classes and CSS, not inline styles. <div style="color: {status}">Status</div> sets color to the literal status string, which is invalid color.
Final Answer:
<div style="color: {status === 'success' ? 'green' : status === 'warning' ? 'orange' : 'red'}">Status</div> -> Option A
Quick Check:
Use ternary inside { } for conditional inline styles [OK]
Hint: Use nested ternary inside { } for multiple conditions [OK]
Common Mistakes:
Using logical operators instead of ternary
Setting style to variable name without mapping
Confusing inline styles with CSS classes
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.