O'Reilly logo

The Majesty of Vue.js by Kostas Maniatis, Alex Kyriakidis

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 7. Class and Style Bindings

Class binding

Object syntax

A common need for data binding is manipulating an element's class and its styles. For such cases, you can use v-bind:class. This can be used to apply classes conditionally, toggle them and/or apply many of them using one bound object et al.

The v-bind:class directive takes an object with the following format as an argument:

{
    'classA': true,
    'classB': false,
    'classC': true
 } 

And applies all classes with the true value to the element. For example, the classes of the following element will be classA and classC:

<div v-bind:class="elClasses"></div>
data: {
    elClasses:
        {
            'classA': true,
            'classB': false,
            'classC': true
        }
} 

To demonstrate how v-bind is used with the class attributes, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required