Vue Class & Style Bindings

Dynamic classes and inline styles.

✍️ 3 exercises + code challenge in this lesson
  • :class="{ active: isActive }" adds the class when the value is true.
  • Arrays: :class="[base, extra]"
  • :style="{ color: textColor, fontSize: size + 'px' }"

Key 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>
Free sign-in required · Google login available

Vue Class & Style Bindings Practice Exercises

Check what you learned in “Vue Class & Style Bindings”.

/ 3 solved
Fill in the blank

Add the class "active" when isActive is true.

<div :class="{ : isActive }"></div>
✓ Correct! Not quite. Try again.
Multiple choice

How do you bind an inline style?

✓ Correct! Not quite. Try again.
Code challenge

Add the class "highlight" to the <p> when important is true, using :class.

Write the code in the live editor. Your answer is checked automatically.

Free sign-in required · Google login available

to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

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