Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 28 ยท Alpine.js Basics

Binding Attributes, Classes and Styles

Bind HTML attributes with Alpine.js x-bind and the colon shorthand: dynamic classes, inline styles, disabled buttons, boolean attributes and x-model basics.

In the last lesson you used x-text to put state into an element's text. Real interfaces also need state to control attributes: the class that makes a tab look active, the disabled flag on a button, the href of a link, the style of a progress bar. The directive for that is x-bind, and almost everyone writes it as a colon.

HTML
<button x-bind:disabled="saving">Save</button>
<button :disabled="saving">Save</button>   <!-- same thing, shorter -->

Whatever follows the colon is the attribute name, and the value is a JavaScript expression. When the expression changes, Alpine updates the attribute.

Binding ordinary attributes

You can bind any attribute: :href, :src, :alt, :title, :placeholder, :aria-label and so on.

Bound href, title and aria-label

(The x-for loop above is explained in Conditionals and Loops; here it just makes the buttons.)

Boolean attributes: disabled, checked, hidden

Some attributes are boolean: their mere presence means "on". Alpine handles these correctly. A truthy expression adds the attribute, and a falsy one removes it. That means :disabled="false" removes disabled entirely; it does not write disabled="false" (which in plain HTML would still disable the control).

A button that enables itself

Here x-model links the input and the name property in both directions: typing updates the state, and changing the state would update the input. It gets a full lesson of its own in Forms and x-model; for now just notice how little code it takes.

Binding classes

:class is the directive you will use most. It accepts three kinds of values.

An object: each key is a class name, each value decides whether that class is present. This is the usual choice for toggles.

HTML
<li :class="{ 'done': item.done, 'urgent': item.priority > 2 }">

An array: a list of class names, where you can mix strings and expressions.

HTML
<div :class="['card', active ? 'card-active' : 'card-idle']">

A string: any expression returning a class string, for example :class="isOpen ? 'open' : 'closed'".

Importantly, :class adds to a static class attribute rather than replacing it. You can safely write both on one element:

Class bindings: object and array

In the array form, an empty string adds nothing, so condition ? 'class-name' : '' is a safe way to include a class only sometimes. Avoid condition && 'class-name' in arrays: when the condition is false, the array holds the value false, and Alpine would add a class literally named "false".

Binding styles

:style accepts an object whose keys are CSS properties. Use camelCase (backgroundColor) or quote the dashed name ('background-color'). It adds to inline styles you already wrote. Use :style for values that change continuously, such as a progress width or a color picked by the user. For simple on/off looks, prefer classes: they keep your design in the stylesheet.

A style-bound progress bar

Binding many attributes at once

With an object after plain x-bind, you can bind several attributes in one go. Each key is an attribute name.

HTML
<input x-bind="{ type: 'email', required: true, placeholder: '[email protected]' }">

This becomes more useful when you store the object under a name and reuse it, which the plugins lesson covers with Alpine.bind.

Common mistakes

  • Forgetting quotes inside the expression. :class="active" reads a variable named active. For a literal class write :class="'active'".
  • Using :class with a dashed key without quotes. { is-open: open } is a syntax error; write { 'is-open': open }.
  • Expecting :disabled="'false'" to enable. A non-empty string is truthy. Use the boolean false.
  • Overusing :style. If you toggle between two looks, name them with classes.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the shorthand for x-bind:disabled?

    The colon is the shorthand for x-bind, just as @ is the shorthand for x-on.
  2. 2. What happens to the disabled attribute when you write :disabled="false"?

    Alpine treats boolean attributes specially: falsy values remove the attribute entirely.
  3. 3. An element has class="card" and :class="{ 'active': on }". When on is true, what are its classes?

    Bound classes are merged with the static class attribute.
  4. 4. Which is the best use of :style?

    Continuously varying numeric values suit :style. Fixed on/off looks belong in CSS classes toggled with :class.

Summary

  • x-bind:attr and the shorthand :attr set any attribute from a JavaScript expression.
  • Boolean attributes such as disabled are added when truthy and removed when falsy.
  • :class accepts an object, an array or a string and merges with a static class.
  • :style takes an object for values that change continuously.
  • x-model links an input and a state property in both directions (next lessons go deeper).

Continue with Conditionals and Loops with x-if and x-for.