📖 Scenario: You are building a simple to-do list app. Each task can be marked as done or not done. You want to show done tasks with a different style.
🎯 Goal: Create a Svelte component that shows a list of tasks. Use conditional classes with class:done to style tasks that are done.
📋 What You'll Learn
Create a list of tasks with exact names and done status
Create a variable to track the minimum number of done tasks to highlight
Use a for loop to display tasks
Use class:done to conditionally add a CSS class based on task status
💡 Why This Matters
🌍 Real World
Conditional classes are used in real apps to change styles dynamically, like marking completed tasks or highlighting active menu items.
💼 Career
Knowing how to use conditional classes in Svelte is important for building interactive user interfaces that respond to user actions.
Progress0 / 4 steps
1
Create the tasks list
Create a variable called tasks that is an array of objects with these exact entries: { name: 'Buy groceries', done: false }, { name: 'Clean room', done: true }, { name: 'Pay bills', done: false }.
Svelte
Hint
Use let tasks = [ ... ] with objects inside the array.
2
Add a highlight threshold variable
Create a variable called highlightDoneCount and set it to 1 to represent the minimum number of done tasks to highlight.
Svelte
Hint
Use let highlightDoneCount = 1; to create the variable.
3
Display tasks with conditional classes
Use a {#each tasks as task} block to display each task's name inside a <li>. Add a conditional class done using class:done={task.done} on the <li>.
Svelte
Hint
Use {#each tasks as task} and inside it write <li class:done={task.done}>{task.name}</li>.
4
Add CSS for the done class
Add a <style> block with CSS that styles the .done class to have text-decoration: line-through; and color: gray;.
Svelte
Hint
Write a <style> block with CSS for the .done class.
Practice
(1/5)
1. What does the Svelte syntax class:active={isActive} do in a component?
easy
A. Always adds the CSS class 'active' regardless of isActive value.
B. Adds the CSS class 'active' only if the variable isActive is true.
C. Removes the CSS class 'active' when isActive is true.
D. Adds the CSS class 'active' only if isActive is false.
Solution
Step 1: Understand the syntax class:name={condition}
This syntax adds the CSS class name only when the condition is true.
Step 2: Apply to class:active={isActive}
The class active is added only if isActive is true; otherwise, it is not added.
Final Answer:
Adds the CSS class 'active' only if the variable isActive is true. -> Option B
Quick Check:
Conditional class = Adds class if true [OK]
Hint: class:name adds class only when condition is true [OK]
Common Mistakes:
Thinking class:name adds class always
Confusing true and false conditions
Assuming it removes class when true
2. Which of the following is the correct syntax to conditionally add a CSS class highlight in Svelte when isHighlighted is true?
easy
A. class:highlight = {isHighlighted}
B. class.highlight={isHighlighted}
C. class:highlight={isHighlighted}
D. class={highlight: isHighlighted}
Solution
Step 1: Recall correct Svelte conditional class syntax
The correct syntax is class: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 C
Quick Check:
Correct syntax = class:name={condition} [OK]
Hint: No spaces around = in class:name={condition} [OK]
Common Mistakes:
Adding spaces around = sign
Using dot instead of colon
Trying object syntax inside class attribute
3. Given this Svelte code snippet:
<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?
medium
A. No classes added
B. Only the class error
C. Both classes error and success
D. Only the class success
Solution
Step 1: Evaluate class:error={isError} when isError is false
Since isError is false, the class error is not added.
Step 2: Evaluate class:success={!isError} when isError is false
The expression !isError is true, so the class success is added.
Final Answer:
Only the class success -> Option D
Quick Check:
Negation adds success class when isError is false [OK]
Hint: Check condition true to add class, false to skip [OK]
A. There should be no spaces around the equals sign in class:active = {isActive}.
B. The class: directive cannot be used on a <button> element.
C. The class name cannot be active.
D. The variable isActive is not declared.
Solution
Step 1: Check variable declaration
The variable isActive is declared correctly in the script block.
Step 2: Check syntax of conditional class
The syntax class:active = {isActive} has spaces around the equals sign, which is invalid in Svelte. It should be class:active={isActive} without spaces.
Final Answer:
There should be no spaces around the equals sign in class:active = {isActive}. -> Option A
Quick Check:
No spaces around = in class:name={condition} [OK]
Hint: No spaces around = in class:name={condition} [OK]
Common Mistakes:
Adding spaces around = sign
Thinking class:name can't be on button
Missing variable declaration
5. You want to add two conditional classes 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?