Vuev-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 thev-bind directive.
On this page we will explain thev-bind directive in more detail.
Thev-bind Directive
Thev-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
Example
Thesrc attribute value of an<img> tag is taken from the Vue instance data property 'url':
CSS Binding
We can use thev-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 theCSS 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 withv-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'.
Text example
</div>
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'.
Text example
</div>
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'.
Text example
</div>
Example
The background color depends on the 'bgVal' data property value inside the Vue instance.
Notice the background color on this div tag.
</div>
Example
The background color is set with aJavaScript conditional (ternary) expression depending on whether the 'isImportant' data property value is 'true' or 'false'.
Conditional background color
</div>
Bind class
We can usev-bind to change the class attribute.
The value ofv-bind:class can be a variable:
Example
Theclass name is taken from the 'className' Vue data property:
The class is set with Vue
</div>
The value ofv-bind:class can also be an object, where the class name will only take effect if it is set to 'true':
Example
Theclass attribute is assigned or not depending on if the class 'myClass' is set to 'true' or 'false':
The class is set conditionally to change the background color
</div>
When the value ofv-bind:class is an object, the class can be assigned depending on a Vue property:
Example
Theclass attribute is assigned depending on the 'isImportant' property, if it is 'true' or 'false':
The class is set conditionally to change the background color
</div>
Shorthand forv-bind
The shorthand for 'v-bind:' is simply ':'.
Example
Here we just write ':' instead of 'v-bind:':
The class is set conditionally to change the background color
</div>
We will continue to usev-bind: syntax in this tutorial to avoid confusion.

