Alpine.js
Alpine.js cheat sheet with directives, x-data, x-bind, x-on, transitions, and code examples for building lightweight interactive UIs.
Sign in to mark items as known and track your progress.
Sign inGetting Started
Installation & Setup
Add Alpine.js to your project via CDN or npm
<!-- CDN (Recommended for quick start) -->
<script defer src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js"></script>
<!-- NPM Installation -->
npm install alpinejs
// Import in your bundle
import Alpine from 'alpinejs'
Alpine.start()x-data - Component State
Define reactive data for your Alpine component
<div x-data="{ open: false, count: 0 }">
<button @click="open = !open">Toggle</button>
<button @click="count++">Increment</button>
<div x-show="open">
Content is visible! Count: <span x-text="count"></span>
</div>
</div>Directives - Data Display
x-text - Display Text
Set text content of an element reactively
<div x-data="{ message: 'Hello Alpine!' }">
<p x-text="message"></p>
<!-- Output: Hello Alpine! -->
</div>x-html - Render HTML
Set HTML content of an element
<div x-data="{
content: '<strong>Bold text</strong>'
}">
<div x-html="content"></div>
<!-- Output: Bold text (rendered as HTML) -->
</div>x-show - Toggle Visibility
Show or hide elements based on conditions
<div x-data="{ open: false }">
<button @click="open = !open">Toggle</button>
<div x-show="open">
This content toggles visibility
</div>
</div>Directives - User Interaction
x-on - Event Listeners
Attach event listeners to elements (shorthand: @)
<div x-data="{ count: 0 }">
<!-- Longhand syntax -->
<button x-on:click="count++">Increment</button>
<!-- Shorthand @ syntax (recommended) -->
<button @click="count++">Increment</button>
<p x-text="count"></p>
</div>x-model - Two-Way Binding
Bind form inputs to data with automatic sync
<div x-data="{ message: 'Hello' }">
<input type="text" x-model="message">
<p x-text="message"></p>
</div>x-bind - Bind Attributes
Dynamically bind HTML attributes (shorthand: :)
<div x-data="{ isActive: true }">
<!-- Longhand -->
<div x-bind:class="isActive ? 'active' : ''">Content</div>
<!-- Shorthand : (recommended) -->
<div :class="isActive ? 'active' : ''">Content</div>
</div>Directives - Control Flow
x-if - Conditional Rendering
Conditionally add/remove elements from DOM
<div x-data="{ show: false }">
<template x-if="show">
<div>This is conditionally rendered</div>
</template>
</div>x-for - Loop & Lists
Render lists of elements from arrays
<div x-data="{
items: ['Apple', 'Banana', 'Cherry']
}">
<template x-for="item in items" :key="item">
<li x-text="item"></li>
</template>
</div>Advanced Features
x-ref - Element References
Reference DOM elements within your component
<div x-data>
<input x-ref="username" type="text">
<button @click="$refs.username.focus()">
Focus Input
</button>
</div>x-transition - Animations
Add smooth transitions when elements appear/disappear
<div x-data="{ open: false }">
<button @click="open = !open">Toggle</button>
<div x-show="open" x-transition>
Animated content
</div>
</div>$el & $refs - Magic Properties
Access special Alpine.js magic properties
<div x-data>
<!-- $el - Current element -->
<button @click="$el.remove()">
Remove This Button
</button>
<!-- $refs - Element references -->
<input x-ref="email">
<button @click="$refs.email.focus()">
Focus
</button>
</div>x-cloak - Hide Uninitialized
Prevent flash of unstyled content before Alpine loads
<!-- Add to your CSS -->
<style>
[x-cloak] {
display: none !important;
}
</style>
<!-- Use on Alpine components -->
<div x-data="{ message: 'Hello' }" x-cloak>
<p x-text="message"></p>
</div>