Menu
CoddyTech

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 icon

Challenge

Easy

Keep 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>
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals