Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 28 ยท Getting Started

HTML-first interactivity: HTMX and Alpine.js

Meet HTMX and Alpine.js: two small libraries that add server round trips and local state to plain HTML, with no build step and no JSON API.

Modern websites often split into two programs: a JSON API on the server and a large JavaScript application in the browser that turns that JSON back into HTML. That split is powerful, but it costs a lot. You write the view logic twice, you ship hundreds of kilobytes of JavaScript, and you need a build pipeline before a button can do anything.

This track teaches a different path. HTMX lets any element send a request to the server and swap the HTML that comes back into the page. Alpine.js adds small pieces of local state (an open menu, a selected tab, a character counter) directly in your markup. Together they cover most of what ordinary websites need, and you write them as HTML attributes. If you know basic HTML and a little JavaScript, you are ready.

The idea: hypermedia, not JSON

The web was designed around hypermedia: the server sends HTML, and that HTML contains the links and forms that tell the user what they can do next. A normal link click replaces the whole page. HTMX keeps that model but removes the "whole page" restriction. A request can be triggered by any element and any event, and the response replaces just one part of the page.

sequenceDiagram
    participant U as User
    participant B as Browser with HTMX
    participant S as Server
    U->>B: Clicks a button with hx-get
    B->>S: GET /hello (HX-Request: true)
    S-->>B: HTML fragment
    B->>B: Swaps fragment into the target
    B-->>U: Page part updated, no reload

An HTMX interaction: the server answers with HTML, not data.

Compare that with the JSON approach. The server returns {"name": "Ann"}, and your JavaScript must find the element, build the markup and insert it. With HTMX the server already built the markup, in the same templating language you use for full pages. There is nothing to translate.

Who does what

The two libraries solve different problems, and that is why they fit together so well.

Two libraries, two jobs

HTMX Talks to the server and swaps in the HTML that comes back.
Alpine.js Keeps small bits of state in the browser.

One owns server data, the other owns UI state.

1 / 3

A good rule: if the answer to "where does this information live?" is the database, use HTMX. If the answer is "only in the user's current view", use Alpine. A product list is server data. Whether its filter panel is open is view state.

A first live demo

The demo below uses both libraries. The button on the left asks the "server" for a greeting (HTMX). The button on the right toggles a note and counts clicks (Alpine). The server here is a tiny stand-in that runs inside the page, because the preview sandbox cannot reach a real backend. The next lesson explains exactly how it works. For now, just read the attributes.

HTMX and Alpine side by side

Press the left button several times: the counter comes from the server's memory, and the page never reloads. Press the right button: nothing leaves the browser at all.

When not to use them

HTMX and Alpine are a great default, not a universal answer. Be honest about the limits:

  • Highly interactive apps such as a drawing tool, a spreadsheet or a real-time collaborative editor keep complex state on the client. A component framework like React or Vue.js suits them better.
  • Offline-first apps need the data and logic in the browser. HTMX needs a server to answer every interaction.
  • Native mobile apps consume data APIs, not HTML.
  • Very latency-sensitive interactions (dragging, typing in a canvas) cannot wait for a round trip. Use Alpine or plain JavaScript for those parts.

Many real projects mix approaches: a mostly server-rendered site with HTMX and Alpine, plus one React widget for the one screen that truly needs it.

How the examples in this track run

Every example is a small HTML page in a sandboxed preview. Three facts shape them:

  1. Scripts come from a CDN with pinned versions: htmx 2.0.4 and Alpine.js 3.14.8. Pinning means your example never changes behavior when a new release appears.
  2. The preview has no real server, so each HTMX example includes a few lines of mock server. It replaces the browser's request object with one that answers from a table of routes you can read right next to the markup.
  3. Nothing is stored in cookies or localStorage, because the sandbox forbids them. State lives in memory and resets on reload.

The mock server is a teaching tool. In your own projects you delete it and let your real backend (Laravel, Django, Express, Rails, anything that returns HTML) answer the requests. The HTMX attributes stay exactly the same.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does an HTMX request typically get back from the server?

    HTMX follows the hypermedia model: the server returns HTML, and the library inserts it into the target element.
  2. 2. Which of these is best handled by Alpine.js rather than HTMX?

    A dropdown's open state exists only in the browser and must react instantly, which is exactly Alpine's job.
  3. 3. Why does this track pin exact CDN versions?

    A pinned URL always serves the same file, so the lessons and their demos stay accurate.
  4. 4. Which project is the weakest fit for HTMX and Alpine alone?

    A canvas with live shared state needs complex client-side logic; the other three are classic server-rendered interactions.

Summary

  • HTMX sends requests from any element and swaps the returned HTML into the page; Alpine.js keeps small bits of local state in the markup.
  • Use HTMX for data that lives on the server and Alpine for view state that must react instantly.
  • Both are configured with HTML attributes, need no build step and add few kilobytes.
  • They are not right for offline, native or highly interactive client-side apps.
  • In this track, examples use pinned CDN scripts and a small in-page mock server.

Continue with Setting Up: CDN, Versions and the In-Page Mock Server.