Conditional Classes
Part of the Fundamentals section of Coddy's Vue journey. Lesson 12 of 55.
An object class binding maps class names to conditions. In this example the done class is absent at first and appears when finished becomes true.
const finished = ref(false);
// <article class="item" :class="{ done: finished }">Read</article>The static item class stays present while Vue adds or removes done. The object key names the class; its value decides whether that class is active. Several keys can depend on separate booleans, avoiding manual class-string updates.
Challenge
EasyKeep class="card" on p#result. Toggle a selected class with #toggle, starting unselected. Use an object class binding.
Try it yourself
<!doctype html><html><head><meta charset="UTF-8"><title>Vue practice</title></head><body><div id="app"></div><script type="module" src="./main.js"></script></body></html>This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
2Reactive State
Reactive RefsUpdating Refs in ScriptEvent Handler ArgumentsReactive ObjectsRecap: Reactive State5Conditions and Lists
Conditional BranchesShow without UnmountingRendering ListsStable List IdentityRecap: Conditions8Component Communication
Emitting EventsEvent PayloadsLifting Shared StateDefault and Named SlotsRecap: Shared Panel11Project: Community Board
Board StructureAdding Community Tasks