Discover how a tiny syntax change can save you from messy class toggling forever!
Why Conditional classes (class:name) in Svelte? - Purpose & Use Cases
Imagine you want to change the look of a button when a user clicks it, like making it green when active and gray when not.
Doing this by hand means writing JavaScript to add or remove CSS classes every time the button state changes.
Manually adding and removing classes is slow and easy to mess up.
You might forget to remove a class or add the wrong one, causing the button to look broken or inconsistent.
This makes your code messy and hard to maintain.
Svelte's class:name directive lets you toggle CSS classes automatically based on conditions.
You just write the condition once, and Svelte handles adding or removing the class for you.
if (active) { button.classList.add('green'); } else { button.classList.remove('green'); }
<button class:green={active}>Click me</button>This makes your UI code cleaner and your app more reliable by linking styles directly to state.
Think of a to-do list where completed tasks get a line-through style automatically when you check them off.
Manually toggling classes is error-prone and verbose.
class:name lets you bind classes to conditions simply.
This keeps your code clean and UI consistent.
Practice
class:active={isActive} do in a component?Solution
Step 1: Understand the syntax
This syntax adds the CSS classclass:name={condition}nameonly when theconditionis true.Step 2: Apply to
The classclass:active={isActive}activeis added only ifisActiveis true; otherwise, it is not added.Final Answer:
Adds the CSS class 'active' only if the variable isActive is true. -> Option BQuick Check:
Conditional class = Adds class if true [OK]
- Thinking class:name adds class always
- Confusing true and false conditions
- Assuming it removes class when true
highlight in Svelte when isHighlighted is true?Solution
Step 1: Recall correct Svelte conditional class syntax
The correct syntax isclass:name={condition}without spaces around the equals sign.Step 2: Check each option
class:highlight={isHighlighted}matches the correct syntax exactly:class:highlight={isHighlighted}. Others have syntax errors or invalid formats.Final Answer:
class:highlight={isHighlighted} -> Option CQuick Check:
Correct syntax = class:name={condition} [OK]
- Adding spaces around = sign
- Using dot instead of colon
- Trying object syntax inside class attribute
<script>
let isError = false;
</script>
<div class:error={isError} class:success={!isError}>Status</div>What CSS classes will the <div> have when
isError is false?Solution
Step 1: Evaluate
Sinceclass:error={isError}whenisErroris falseisErroris false, the classerroris not added.Step 2: Evaluate
The expressionclass:success={!isError}whenisErroris false!isErroris true, so the classsuccessis added.Final Answer:
Only the class success -> Option DQuick Check:
Negation adds success class when isError is false [OK]
- Adding both classes regardless of condition
- Confusing negation operator !
- Assuming false adds class
<script>
let isActive = true;
</script>
<button class:active = {isActive}>Click Me</button>Solution
Step 1: Check variable declaration
The variableisActiveis declared correctly in the script block.Step 2: Check syntax of conditional class
The syntaxclass:active = {isActive}has spaces around the equals sign, which is invalid in Svelte. It should beclass:active={isActive}without spaces.Final Answer:
There should be no spaces around the equals sign in class:active = {isActive}. -> Option AQuick Check:
No spaces around = in class:name={condition} [OK]
- Adding spaces around = sign
- Thinking class:name can't be on button
- Missing variable declaration
visible and highlight to a <div> in Svelte. visible should be added when isVisible is true, and highlight only when isHighlighted is also true and isVisible is true.Which of the following is the correct way to write this?
Solution
Step 1: Understand conditions for each class
The classvisibledepends only onisVisible. The classhighlightdepends on bothisHighlightedandisVisiblebeing true.Step 2: Check each option's logic
<div class:visible={isVisible} class:highlight={isHighlighted && isVisible}>Content</div>correctly usesclass:visible={isVisible}andclass:highlight={isHighlighted && isVisible}, matching the requirements.<div class:visible={isVisible} class:highlight={isHighlighted || isVisible}>Content</div>uses OR which is incorrect.<div class:visible={isVisible && isHighlighted} class:highlight={isVisible}>Content</div>swaps conditions incorrectly.<div class:visible={isVisible} class:highlight={isHighlighted}>Content</div>misses theisVisiblecondition forhighlight.Final Answer:
<div class:visible={isVisible} class:highlight={isHighlighted && isVisible}>Content</div> -> Option AQuick Check:
Use && to combine conditions for conditional classes [OK]
- Using || instead of && for combined conditions
- Swapping conditions between classes
- Omitting one condition in combined class
Ready to prove it?
Pass the test and this pattern moves unknown
