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.

  1. Finding attributes When the page loads, and whenever new HTML arrives, it looks for elements with hx- attributes. src/htmx.jsprocessNode
  2. Triggers Decides what sets an element off: a click, a change, a submit, or whatever hx-trigger says. src/htmx.jsgetTriggerSpecs
  3. The request Gathers the form values, sends the request, and marks it as coming from htmx. src/htmx.jsissueAjaxRequest
  4. The swap Puts the HTML that came back into the target, in the way hx-swap asks. src/htmx.jsswap
  5. 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">

  1. When the page loaded, htmx found the button and gave it a click listener.
  2. A button has no hx-trigger, so the default is used: click.
  3. On the click, it works out the target: the element with id "list".
  4. It sends GET /more with the header HX-Request: true, so the server knows to send a piece and not a whole page.
  5. The reply is HTML. It is turned into elements and swapped into the target.
  6. 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.

src/htmx.js

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

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

How React worksYou describe what the screen should look like. React works out the smallest set of changes to make it so.How Express worksA request comes in and passes through a list of functions, in order, until one of them answers.How Flask worksYou write a function for each address. Flask finds the right one for a request, calls it, and turns what it…How Redis worksOne big dictionary held in memory, answered by a single loop that takes one command at a time.How nanoGPT worksText becomes numbers, a model learns to guess the next number, and guessing again and again writes new text.How micrograd worksEvery number remembers how it was made, so the answer can be traced back to say how much each input mattered.How SQLite worksYour SQL is compiled into a small program, and a tiny virtual machine runs it against one file on disk.How FastAPI worksYou write a function with type hints. FastAPI reads the hints to check the request, convert the values and…How Vue worksVue notices which data each part of the screen reads, so when that data changes it knows exactly what to…How Requests worksOne friendly line, requests.get(url), on top of the fiddly work of opening connections and speaking HTTP.How Redux worksAll of an app’s state lives in one place, and the only way to change it is to describe what happened.How Git worksEvery version of every file is stored under a name made from its own contents, and a commit is a small note…How Preact worksThe same idea as React, in a few kilobytes: you describe the screen, and Preact changes only what is…How Zustand worksA store is a value, a way to change it, and a list of who wants to know. The whole core is about a hundred…How Axios worksOne way to make web requests that works the same in a browser and in Node, with hooks before and after every…How LevelDB worksA store for keys and values that makes writing fast by never changing a file: it only adds, and tidies up…How etcd’s Raft worksSeveral servers keep the same list of changes in the same order, even when some of them crash. This is the…

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.