Conditional classes let you add or remove CSS classes based on a condition. This helps change styles easily without extra code.
Conditional classes (class:name) in Svelte
<element class:name={condition}>The name is the CSS class you want to add.
The condition is a JavaScript expression that returns true or false.
active class only if isActive is true.<button class:active={isActive}>Click me</button>hidden class when isVisible is false.<div class:hidden={!isVisible}>Content</div>error class if hasError is true.<p class:error={hasError}>Warning!</p>This Svelte component shows a button that toggles an active class when clicked. The button text and style change based on the isActive state.
It uses class:active={isActive} to add the class only when isActive is true.
ARIA attribute aria-pressed improves accessibility by indicating the toggle state.
<script> let isActive = false; function toggle() { isActive = !isActive; } </script> <button class:active={isActive} on:click={toggle} aria-pressed={isActive}> {isActive ? 'Active' : 'Inactive'} </button> <style> button { padding: 0.5rem 1rem; font-size: 1rem; border: 2px solid #333; background-color: white; color: #333; cursor: pointer; border-radius: 0.25rem; transition: background-color 0.3s, color 0.3s; } button.active { background-color: #007acc; color: white; } </style>
You can use multiple conditional classes on one element.
Use clear and simple conditions for readability.
Remember to add accessible attributes like aria-pressed for toggle buttons.
Conditional classes add CSS classes based on true/false conditions.
Use class:name={condition} syntax in Svelte.
This helps style elements dynamically without extra JavaScript.
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
