Movatterモバイル変換


[0]ホーム

URL:


Menu
×
See More 
Sign In
+1 Get Certified Upgrade Teachers Spaces Get Certified Upgrade Teachers Spaces
   ❮   
     ❯   

Vue Tutorial

Vue HOMEVue IntroVue DirectivesVue v-bindVue v-ifVue v-showVue v-forVue EventsVue v-onVue MethodsVue Event ModifiersVue FormsVue v-modelVue CSS BindingVue Computed PropertiesVue WatchersVue Templates

Scaling Up

Vue Why, How and SetupVue First SFC PageVue ComponentsVue PropsVue v-for ComponentsVue $emit()Vue Fallthrough AttributesVue Scoped StylingVue Local ComponentsVue SlotsVue v-slotVue Scoped SlotsVue Dynamic ComponentsVue TeleportVue HTTP RequestVue Template RefsVue Lifecycle HooksVue Provide/InjectVue RoutingVue Form InputsVue AnimationsVue Animations with v-forVue BuildVue Composition API

Vue Reference

Vue Built-in AttributesVue Built-in ComponentsVue Built-in ElementsVue Component InstanceVue DirectivesVue Instance OptionsVue Lifecycle Hooks

Vue Examples

Vue ExamplesVue ExercisesVue QuizVue SyllabusVue Study PlanVue ServerVue Certificate

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

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

Example

Thesrc 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 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'.

<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 aJavaScript 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 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:

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

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':

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

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':

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

Shorthand forv-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 usev-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:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

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 ourterms of use,cookies andprivacy policy.

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


[8]ページ先頭

©2009-2025 Movatter.jp