Skip to content
Tutorials Center
My learning

Topics

Lesson 7 of 87 ยท Template Syntax

Binding Attributes

Learn Vue v-bind and the colon shorthand to set HTML attributes from data, including boolean attributes, dynamic names, object binding and same-name shorthand.

Mustaches only work inside text. To put data into an attribute such as src, href, disabled or aria-label, you use the v-bind directive. It tells Vue: "the value of this attribute is not a fixed string, it is a JavaScript expression, and it must stay in sync with the data".

In practice you will write v-bind as a single colon, so you will type :src="imageUrl" hundreds of times. This lesson explains how Vue treats different kinds of values, how to bind many attributes at once, and the newest shorthand.

v-bind and the colon shorthand

HTML
<img v-bind:src="imageUrl" v-bind:alt="description">
<!-- the usual, shorter form -->
<img :src="imageUrl" :alt="description">

Both lines are identical. Whatever follows the colon is the attribute name, and the quoted value is a JavaScript expression, exactly like inside a mustache. Without the colon, src="imageUrl" would set the attribute to the literal text imageUrl.

Binding src, alt, title and href

Pick a color and notice that four attributes change together (src, alt, title and href) while your code only changes current. Hover over the image to see the bound title. The href is built with encodeURIComponent, which is the correct way to put user text inside a URL.

What Vue does with the value

v-bind does not simply write text. It follows rules that depend on the value:

flowchart TD
    V["Bound value"] --> N{"null or undefined?"}
    N -- "yes" --> R["Attribute is removed"]
    N -- "no" --> B{"Boolean attribute?<br/>disabled, checked, hidden..."}
    B -- "yes" --> T{"Truthy or empty string?"}
    T -- "yes" --> P["Attribute is present"]
    T -- "no" --> R
    B -- "no" --> S["Attribute is set to the value as a string"]

Missing values remove attributes. Boolean attributes depend on truthiness. Everything else becomes a string.

Boolean attributes

HTML boolean attributes such as disabled, readonly, hidden, required and checked mean "on" when they exist and "off" when they do not. Their value is irrelevant: disabled="false" still disables a button. Vue handles this for you. Bind a boolean, and Vue adds the attribute for a truthy value and removes it for a falsy one. An empty string counts as truthy, so a bare <button disabled> keeps working.

Other attributes behave as regular strings. A null or undefined value removes the attribute, which is how you make an attribute optional: :title="note" shows no tooltip while note is undefined.

Booleans and removed attributes

Press Toggle busy and the snapshot gains disabled=""; press it again and the attribute disappears. Toggle note adds and removes title in the same way, because undefined removes an attribute while a string sets it.

Dynamic attribute names

You can also compute the attribute name by wrapping the argument in square brackets: :[name]="value". This is rare, but it helps when a component decides at runtime which attribute to set. The name expression must produce a string (or null to skip the attribute):

HTML
<a :[attrName]="url">Link</a>

When attrName is "href" you get href="..."; when it is "data-source" you get a data attribute. Keep these expressions simple, and write them in lowercase if you use in-DOM templates, because browsers lowercase attribute names before Vue sees them.

Binding an object of attributes

To bind many attributes together, give v-bind no argument and pass an object. Each key becomes an attribute:

Object binding and same-name shorthand

The snapshot shows everything Vue set on the input: id and placeholder from the same-name shorthand, type, required, maxlength and aria-describedby from the object, and the dynamically named attribute. Switch the dynamic name and watch title swap with data-note.

Same-name shorthand

Since Vue 3.4, when the attribute name is the same as the variable name you can drop the value: :id is the same as :id="id", and :src is the same as :src="src". It keeps templates short when you pass many variables with matching names.

Order matters when mixing

If you combine v-bind="object" with individual attributes, the later one wins:

HTML
<!-- id ends up as "from-object" -->
<div id="plain" v-bind="{ id: 'from-object' }"></div>
<!-- id ends up as "plain" -->
<div v-bind="{ id: 'from-object' }" id="plain"></div>

Data, ARIA and custom attributes

v-bind is not limited to well-known attributes. Any name works, which makes it the standard way to keep accessibility attributes in step with state:

HTML
<button type="button" :aria-expanded="open" :aria-controls="panelId" :data-state="open ? 'open' : 'closed'">
  Details
</button>

One detail to remember: for aria-* attributes, false is a meaningful value, and Vue renders aria-expanded="false" correctly. Vue only removes the attribute for null and undefined. A plain non-boolean attribute set to false is rendered as the text "false", which is what ARIA expects.

Attributes versus DOM properties

Most bindings set an HTML attribute. For some elements, such as <input>, <textarea> and <select>, Vue automatically sets the matching DOM property instead (for example value), because the property is what reflects the live state. You rarely need to think about it. If you ever need to force one, :prop.prop="x" sets a property and :prop.attr="x" sets an attribute. Two-way syncing of input values is the job of v-model, introduced in Two-Way Binding with v-model.

Binding URLs safely

A bound href or src that contains user-controlled text deserves care. Build query strings with encodeURIComponent, as the first example does. Never bind a raw string that a user typed to href without checking its scheme: a value such as javascript:alert(1) would run code when clicked. Allow only http: and https: links from untrusted data. Security in Vue shows a small helper for this.

Common mistakes

  • Forgetting the colon. <img src="photoUrl"> asks the browser to load a file literally called photoUrl. If an attribute shows your variable's name on the page, check for the missing colon.
  • Quoting a variable inside the value. :title="'name'" is the string name; :title="name" is the variable.
  • Binding "false" as a string to a boolean attribute. A non-empty string is truthy, so :disabled="'false'" still disables the element. Bind a real boolean.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the difference between <img src="url"> and <img :src="url">?

    Without the colon the value is a fixed string. With v-bind (the colon) the value is a JavaScript expression.
  2. 2. A button has :disabled="busy" and busy is false. What does the rendered button look like?

    For boolean attributes Vue adds the attribute for truthy values and removes it for falsy ones.
  3. 3. What happens to :title="note" when note is undefined?

    null and undefined remove the attribute, which makes optional attributes easy.
  4. 4. What does <input :id :placeholder> do in Vue 3.4 and later?

    The same-name shorthand writes :id as a short form of :id="id".
  5. 5. How do you bind several attributes from one object at once?

    v-bind with no argument expands the keys of an object into attributes. Later attributes override earlier ones.

Summary

  • v-bind:attr="expression", written :attr="expression", sets an attribute from JavaScript data and keeps it in sync.
  • null and undefined remove an attribute; boolean attributes (disabled, hidden, required) are present for truthy values and absent for falsy ones.
  • :[name]="value" computes the attribute name at runtime.
  • v-bind="object" sets many attributes at once; when mixed with individual attributes, the later one wins.
  • :id is shorthand for :id="id" in Vue 3.4 and later.
  • Classes and styles have extra features, covered next.

Next: Class and Style Binding