How it works · JavaScript library that extends HTML
How htmx works
Any element can ask the server for a piece of HTML and put it into the page. You write attributes, not JavaScript.
Think of it like this. A page that orders its own parts: a button says “fetch this, and put it there”, and a courier does it.
All of htmx is one file. The parts below are groups of functions inside it.
- 👀 Finding attributes When the page loads, and whenever new HTML arrives, it looks for elements with hx- attributes. src/htmx.jsprocessNode
- 👆 Triggers Decides what sets an element off: a click, a change, a submit, or whatever hx-trigger says. src/htmx.jsgetTriggerSpecs
- 📡 The request Gathers the form values, sends the request, and marks it as coming from htmx. src/htmx.jsissueAjaxRequest
- 🔁 The swap Puts the HTML that came back into the target, in the way hx-swap asks. src/htmx.jsswap
- 📣 Events Announces every step, so your own code can listen in or stop it. src/htmx.jstriggerEvent
Follow one thing through: a click on <button hx-get="/more" hx-target="#list">
- When the page loaded, htmx found the button and gave it a click listener.
- A button has no hx-trigger, so the default is used: click.
- On the click, it works out the target: the element with id "list".
- It sends GET /more with the header HX-Request: true, so the server knows to send a piece and not a whole page.
- The reply is HTML. It is turned into elements and swapped into the target.
- The new elements are searched for hx- attributes too, so what arrived works the same way.
The clever bit: A short pause that makes CSS transitions work
New content is swapped in, and then htmx waits 20 milliseconds before it “settles”: it adds the class htmx-settling, then copies the final attributes onto elements that kept their id. Because the old values are on screen for one moment and the new ones arrive after, the browser sees a change and plays your CSS transition. Without the pause, content would only ever pop in.
Words you’ll meet
- hx-get
- An attribute that says: when triggered, fetch this address.
- target
- The element the new HTML goes into.
- swap
- How it goes in: replacing the inside, the whole element, or added before or after.
- hypermedia
- The server sends HTML that says what you can do next, not raw data.
Good questions to ask next
- How does htmx find elements that arrive later?
- What can the server say in its response headers?
- How does hx-boost turn normal links into requests?
Open htmx in Bento and ask them there. It reads the project in your browser, draws its parts as a map you can press, and opens the real code behind each one.
Take htmx apart yourself.
Free and open source. Nothing to install, no account.
More projects, taken apart
Written by hand from the code as it was on 2026-11-18 (commit 51c3e67). htmx belongs to its authors; this page is an independent explanation. Spot a mistake? Tell us.