vue add class code example

Example 1: dynamically bind class name in vue

<template>
  <span
    class="description"
    :class="[
      fontTheme,
      darkMode ? 'dark-theme' : 'light-theme',
    ]"
  >
    This is how you add dynamic classes in Vue.
  </span>
</template>

Example 2: vue toggle class

<template>
  	<div>
      <div
        class="staticClass"
        v-bind:class="{ active: isActive, 'text-danger': hasError }"
      ></div>
      <!-- OR -->
      <div
           v-bind:class="[isActive ? activeClass : '', errorClass]"
      ></div>
	</div>
</template>

<script>
  export default {
  	data() {
      isActive: true,
      hasError: false,
      activeClass: 'active',
      errorClass: 'text-danger',
    }
  }
</script>

Example 3: vue conditional class

<div :class="{ 'prop-name': boolVar }"></div>

Example 4: vue inline style bind

<div v-bind:style="{ color: activeColor, fontSize: fontSize + 'px' }"></div>

Example 5: vue class binding

<div v-bind:class="{ active: isActive }"></div>

Example 6: vue if echo class

<div class="defaultclass" :class="condition?' conditionalclasstoappend':''"></div>