0
0
Svelteframework~5 mins

Conditional classes (class:name) in Svelte - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does class:name={condition} do in Svelte?
It adds the CSS class <code>name</code> to the element only if <code>condition</code> is true. If false, the class is not added.
Click to reveal answer
beginner
How do you apply multiple conditional classes in Svelte?
You write multiple <code>class:name={condition}</code> attributes on the same element, each controlling one class based on its condition.
Click to reveal answer
intermediate
Can you use expressions inside the condition of class:name={condition}?
Yes, you can use any JavaScript expression that evaluates to true or false inside the curly braces to control the class application.
Click to reveal answer
intermediate
What happens if the condition in class:name={condition} changes during runtime?
Svelte automatically updates the element's classes to add or remove the class name based on the new condition value.
Click to reveal answer
beginner
Why is using class:name={condition} better than manually toggling classes with JavaScript in Svelte?
Because it keeps your code clean, declarative, and reactive. Svelte handles class changes automatically without extra code.
Click to reveal answer
What does class:active={isActive} do in Svelte?
ARemoves the class 'active' if isActive is true
BAdds the class 'active' only if isActive is true
CAlways adds the class 'active'
DAdds the class 'isActive' instead of 'active'
How can you add two conditional classes 'red' and 'bold' based on variables in Svelte?
A<div class={isRed ? 'red' : ''} class={isBold ? 'bold' : ''}>
B<div class='red bold' if={isRed && isBold}>
C<div class:red={isRed} class:bold={isBold}>
D<div class:red, bold={isRed && isBold}>
If class:highlight={score > 50} is used, when is the 'highlight' class applied?
AWhen score is greater than 50
BWhen score is less than 50
CAlways
DNever
What happens if the condition in class:name={condition} changes from true to false?
AThe class 'name' is removed from the element
BThe class 'name' stays on the element
CAn error occurs
DThe class 'name' is added twice
Which is a benefit of using class:name={condition} in Svelte?
AOnly works with static classes
BSlower performance
CRequires manual DOM updates
DCleaner and reactive class management
Explain how to use conditional classes in Svelte and why it helps with reactive UI.
Think about how Svelte updates classes when variables change.
You got /4 concepts.
    Describe how you would add multiple conditional classes to an element in Svelte.
    Remember each class gets its own class:name attribute.
    You got /3 concepts.