Under The Hood
frontend htmx HTMX 1.x / 2.x

hx-indicator: Declarative Loading States via .htmx-request CSS Classes

Last updated
Prerequisites:
CSS Opacity & Display transitions
HTMX AJAX lifecycle
  • htmx
  • hx-indicator
  • loading-states
  • spinner
  • css
  • htmx-request

Read at your depth

The practical view

`hx-indicator` points to an element that will be shown while an HTMX request is in flight: `<button hx-get="/data" hx-indicator="#spinner">Load</button>`. HTMX automatically adds the `.htmx-request` CSS class to the indicator element (and the triggering element) during the request, and removes it upon completion. The default HTMX stylesheet automatically hides `.htmx-indicator` (`opacity: 0`) and shows it when `.htmx-request` is added (`opacity: 1`).

The same idea in other frameworks

react equivalent
`const [loading, setLoading] = useState(false); return <button disabled={loading}>...`.

Legacy vs modern

Manual Loading State Boilerplate vs Declarative hx-indicator

Manual spinner toggling requires try/finally blocks in JS; `hx-indicator` coordinates loading visibility through CSS classes.

before → after
Manual JS Spinner Toggling
spinner.style.display = 'block'; try { await fetch(); } finally { spinner.style.display = 'none'; } // ❌ Manual
Declarative hx-indicator with CSS
<button hx-post="/save" hx-indicator="#spinner">Save</button> // ✅ NEW: Zero JS needed

Interview gotchas

Context

Interviewers test your knowledge of default indicator scoping.

The mechanical answer

If no `hx-indicator` attribute is explicitly provided on the triggering element, HTMX looks for any child element matching `.htmx-indicator` *inside* the triggering element and adds `.htmx-request` to the triggering element itself. If the indicator is located outside the element, `hx-indicator="#my-spinner"` must be explicitly specified.

Trap

Placing `.htmx-indicator` on a sibling element without specifying `hx-indicator` will result in the spinner never appearing.

Under The Hood — a multi-depth technical interview hub.

Third-party licenses Press ⌘ K to search.