Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

Vue Tutorial

Vue HOME Vue Intro Vue Directives Vue v-bind Vue v-if Vue v-show Vue v-for Vue Events Vue v-on Vue Methods Vue Event Modifiers Vue Forms Vue v-model Vue CSS Binding Vue Computed Properties Vue Watchers Vue Templates

Scaling Up

Vue Why, How and Setup Vue First SFC Page Vue Components Vue Props Vue v-for Components Vue $emit() Vue Fallthrough Attributes Vue Scoped Styling Vue Local Components Vue Slots Vue v-slot Vue Scoped Slots Vue Dynamic Components Vue Teleport Vue HTTP Request Vue Template Refs Vue Lifecycle Hooks Vue Provide/Inject Vue Routing Vue Form Inputs Vue Animations Vue Animations with v-for Vue Build Vue Composition API

Vue Reference

Vue Built-in Attributes Vue Built-in Components Vue Built-in Elements Vue Component Instance Vue Directives Vue Instance Options Vue Lifecycle Hooks

Vue Examples

Vue Examples Vue Exercises Vue Quiz Vue Server Vue Certificate

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



×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.