Vue v-bind Directive

You have already seen that a basic Vue setup consists of a Vue instance and that we can access it from the <div id="app"> tag with {{ }} or the v-bind directive.

On this page we will explain the v-bind directive in more detail.

The v-bind Directive

The v-bind directive lets us bind an HTML attribute to data in the Vue instance. This makes it easy to change the attribute value dynamically.

Syntax

<div v-bind:[attribute]="[Vue data]"></div>

Example

The src attribute value of an <img> tag is taken from the Vue instance data property 'url':

<img v-bind:src="url">
Try it Yourself »

CSS Binding

We can use the v-bind directive to do in-line styling and modify classes dynamically. We will show you briefly how to do that in this section, and later in this tutorial, on the CSS Binding page, we will explain this in more detail.


Bind style

In-line styling with Vue is done by binding the style attribute to Vue with v-bind.

As a value to the v-bind directive, we can write a JavaScript object with the CSS property and value:

Example

The font size depends on the Vue data property 'size'.

<div v-bind:style="{ fontSize: size }">
  Text example
</div>
Try it Yourself »

We can also separate the font size number value from the font size unit if we want to, like this:

Example

The font size number value is stored the Vue data property 'size'.

<div v-bind:style="{ fontSize: size + 'px' }">
  Text example
</div>
Try it Yourself »

We could also write the CSS property name with CSS syntax (kebab-case) in hyphens, but it is not recommended:

Example

The CSS property fontSize is referred to as 'font-size'.

<div v-bind:style="{ 'font-size': size + 'px' }">
  Text example
</div>
Try it Yourself »

Example

The background color depends on the 'bgVal' data property value inside the Vue instance.

<div v-bind:style="{ backgroundColor: 'hsl('+bgVal+',80%,80%)' }">
  Notice the background color on this div tag.
</div>
Try it Yourself »

Example

The background color is set with a JavaScript conditional (ternary) expression depending on whether the 'isImportant' data property value is 'true' or 'false'.

<div v-bind:style="{ backgroundColor: isImportant ? 'lightcoral' : 'lightgray' }">
  Conditional background color
</div>
Try it Yourself »

Bind class

We can use v-bind to change the class attribute.

The value of v-bind:class can be a variable:

Example

The class name is taken from the 'className' Vue data property:

<div v-bind:class="className">
  The class is set with Vue
</div>
Try it Yourself »

The value of v-bind:class can also be an object, where the class name will only take effect if it is set to 'true':

Example

The class attribute is assigned or not depending on if the class 'myClass' is set to 'true' or 'false':

<div v-bind:class="{ myClass: true }">
  The class is set conditionally to change the background color
</div>
Try it Yourself »

When the value of v-bind:class is an object, the class can be assigned depending on a Vue property:

Example

The class attribute is assigned depending on the 'isImportant' property, if it is 'true' or 'false':

<div v-bind:class="{ myClass: isImportant }">
  The class is set conditionally to change the background color
</div>
Try it Yourself »

Shorthand for v-bind

The shorthand for 'v-bind:' is simply ':'.

Example

Here we just write ':' instead of 'v-bind:':

<div :class="{ impClass: isImportant }">
  The class is set conditionally to change the background color
</div>
Try it Yourself »

We will continue to use v-bind: syntax in this tutorial to avoid confusion.


Vue Exercises

Test Yourself With Exercises

Exercise:

Provide the missing code so that the class is set equal to the 'className' data property, using a Vue directive shorthand.

<div ="className">
  The class is set with Vue
</div>

Start the Exercise



Copyright 1999-2023 by Refsnes Data. All Rights Reserved.