Table of contents
1.
Introduction
2.
How to use class binding?
2.1.
Using the object syntax
2.2.
Using the array syntax
2.3.
Use with components
3.
Style binding
3.1.
How to use style binding?
4.
FAQ
5.
Key takeaways
Last Updated: Mar 27, 2024

Class and style bindings

Author Aditya Kumar
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Class and style bindings fulfil the need to dynamically add styles based on certain conditions. Add multiple classes, styles when need be. Vue.js is done with “v-bind”, a directive that manipulates the DOM when applied to the HTML.
 

How to use class binding?

<template>
 <div>
   <h1 class="temp">class binding</h1>
 </div>
</template>

<script>
export default {
 name: "Home",
};
</script>

<style scoped>
.temp {
 color: red;
}
</style>
You can also try this code with Online Javascript Compiler
Run Code

 

Here we have added class temp just as it is added in a simple HTML file.

Can we add classes conditionally?

Yes!

Refer to below examples--

Using the object syntax

<template>
 <div>
   <h1 :class="{temp: true}"></h1>
 </div>
</template>

<script>
export default {
 name: "Home",
};
</script>>

<style scoped>
.temp {
 color: red;
}
</style>
You can also try this code with Online Javascript Compiler
Run Code

The above conditional styling is done to an HTML element, and the rendered element will be:

<h1 class=”temp”></h1>

The temp class will be added to the class list of h1 if temp is true.

 

Using the array syntax

v-bind:class need to be applied for adding a list of classes to the element 

<template>
<div>
        <h1 v-bind:class="[active, error]">array syntax</h1>
     <div>
</template>

<script>
export default {
 name: "Home",
 data() {
   return {
     active: true,
     error: false,
   };
 },
};
</script>
<style>
.active {
}
.text-danger {
 color: red;
}
</style>
You can also try this code with Online Javascript Compiler
Run Code

 

We have declared strings of classes in the data object and used them inside a list of classes, which will render

<h1 class=”active error”> array syntax</h1>

 

We can use strings inside of list, without declaration stuff

<h1 v-bind:class=”[“active”,text-danger”]”>array syntax</h1>

 

If we want to conditionally add class:

<h1 v-bind:class=”[isActive?active:””,hasError?error:””]>
You can also try this code with Online Javascript Compiler
Run Code


And if :

data{
isActive:true,
hasError:false,
Active:”active”,
error:”text-danger”
}
You can also try this code with Online Javascript Compiler
Run Code


The rendered element would be: 

<h1 class=”active”>array syntax</h1>
You can also try this code with Online Javascript Compiler
Run Code


Also for multiple conditional classes, we can use object syntax inside of array syntax as below:

<h1 v-bind:class=”[{active:isActive},error]”>array syntax </h1>
You can also try this code with Online Javascript Compiler
Run Code

 

 

Use with components

Let’s first see how we add class to components. We declare a component as follows with an h1 element 

Vue.component('my-component', {
   template: '<h1 class="fun dang">Hello :)</h1>'
})
You can also try this code with Online Javascript Compiler
Run Code


Now we add classes to comp when using it

<my-component class=”hello”></my-component>
You can also try this code with Online Javascript Compiler
Run Code


The rendered html would be 

<h1 class=”fun dang hello”>Hello :)</h1>

 

We can add class based on condition just as we added in html elements

<my-component v-bind:class=”{active:isActive}”></my-component>
You can also try this code with Online Javascript Compiler
Run Code

 

If isActive is true, the active class will be added and vice versa

 

Style binding

How to use style binding?

<template>  
<div>
    <h1 v-bind:style="{color:sky,fontSize:fontSize+`px`}">style        binding</h1>
</div>
</template>
<script>
export default {
   name:"Home",
   data:function(){
       return {
           sky:'blue',
           fontSize:20
       }
   }
}
</script>
You can also try this code with Online Javascript Compiler
Run Code

 

We just used the v-bind: style attribute to add style object inline. Also, style objects can be declared elsewhere and placed inside double quotes.

 

<template>
 <div>
     <h1 v-bind:style="style0bject">style binding</h1>
 </div>
</template>

<script>
export default {
 name: "Home",
 data: function () {
   return {
     sky: "blue",
     fontSize: 20,
     styleObject: {
       color: "red",
       fontSize: "20px",
     },
   };
 },
};
</script>
You can also try this code with Online Javascript Compiler
Run Code

 

If we need to use multiple style objects for a single DOM element, add them to the list of objects with v-bind: style as below

 

<h1 v-bind:style=”[firstObj,secondObj]”>Vue is awesome!</h1> 
 
You can also try this code with Online Javascript Compiler
Run Code

FAQ

  1. What is a directive?

Vue.js directives are HTML attributes that get applied to an element for controlling the DOM.v-if behaves the same way as javascript if tag, v-for ie for looping in javascript

<h1 v-if="isAwesome">Vue is awesome!</h1>
You can also try this code with Online Javascript Compiler
Run Code

If the expression inside v-if is truthy, the Html will be rendered

 

2. What is data binding?

Whenever data is changed, it is reflected wherever it has been interpolated or bound. If we increase the cart items, the count would also be updated in the cart along with the total value of items. If there is a class condition, toggling the truthy value will remove the class from the element’s class list.
 

Key takeaways

Congratulations on getting through this article and we learned Class and style bindings and how to use them. This will help understand more of the other directives in vue. js; we saw multiple ways to add classes to components, Html elements with various examples with the rendered dom element. Also, we saw how to add styles using the v-bind: style attribute.

 

You can start learning web development and that too for free. Check out this blog to know how to get started with Start Learning Full-Stack Web Development For Free.

Live masterclass