:class="{ active: isActive }"adds the class when the value is true.- Arrays:
:class="[base, extra]" :style="{ color: textColor, fontSize: size + 'px' }"
Vue Class & Style Bindings
Dynamic classes and inline styles.
✍️ 3 exercises + code challenge in this lessonKey Takeaways: Vue Class & Style Bindings
Mastering Vue Class & Style Bindings helps you write clean and reliable Vue.js code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.
Vue Class & Style Bindings Code Example
Vue.js<script src="https://unpkg.com/vue@3.5.12/dist/vue.global.prod.js"></script>
<style>
.done { text-decoration: line-through; color: #94a3b8; }
</style>
<div id="app">
<p v-for="t in tasks" :key="t.text" :class="{ done: t.done }" @click="t.done = !t.done">{{ t.text }}</p>
<p :style="{ color: color, fontSize: size + 'px' }">Styled text</p>
</div>
<script>
Vue.createApp({
data() {
return { tasks: [ { text: "Learn Vue", done: true }, { text: "Build an app", done: false } ], color: "#42b883", size: 22 };
}
}).mount("#app");
</script>
Vue Class & Style Bindings Practice Exercises
Check what you learned in “Vue Class & Style Bindings”.
Add the class "active" when isActive is true.
<div :class="{ : isActive }"></div>
How do you bind an inline style?
Add the class "highlight" to the <p> when important is true, using :class.
Free sign-in required · Google login available
Stuck on something?
Ask the community in the CodesInTech forum.
Need a developer?
Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.
Explore Our Services