Categories
Vue Answers

How to add conditional class style binding with Vue.js?

Spread the love

Sometimes, we want to add conditional class style binding with Vue.js.

In this article, we’ll look at how to add conditional class style binding with Vue.js.

How to add conditional class style binding with Vue.js?

To add conditional class style binding with Vue.js, we can set the :class prop.

For instance, we write

<template>
  <div
    :class="{
      'fa-checkbox-marked': content.cravings,
      'fa-checkbox-blank-outline': !content.cravings,
    }"
  ></div>
</template>

to set the class prop to an object that applies the fa-checkbox-marked class is content.cravings is true.

And we apply the fa-checkbox-blank-outline class if content.cravings is false.

Conclusion

To add conditional class style binding with Vue.js, we can set the :class prop.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *