| 1 | --- |
| 2 | title: "hx-indicator" |
| 3 | description: "Specifies the loading indicator element" |
| 4 | --- |
| 5 | |
| 6 | The `hx-indicator` attribute allows you to specify the element that will have the `htmx-request` class |
| 7 | added to it for the duration of the request. This can be used to show spinners or progress indicators |
| 8 | while the request is in flight. |
| 9 | |
| 10 | ## Syntax |
| 11 | |
| 12 | ```html |
| 13 | <button hx-post="/submit" hx-indicator="#spinner">Submit</button> |
| 14 | ``` |
| 15 | |
| 16 | The value of this attribute is a CSS query selector of the element or elements to apply the class to, |
| 17 | or the keyword [`closest`](https://developer.mozilla.org/docs/Web/API/Element/closest), followed by a CSS selector, |
| 18 | which will find the closest ancestor element or itself, that matches the given CSS selector (e.g. `closest tr`); |
| 19 | |
| 20 | Here is an example with a spinner adjacent to the button: |
| 21 | |
| 22 | ```html |
| 23 | <div> |
| 24 | <button hx-post="/example" hx-indicator="#spinner"> |
| 25 | Post It! |
| 26 | </button> |
| 27 | <img id="spinner" class="htmx-indicator" src="/img/bars.svg" alt="Loading..."/> |
| 28 | </div> |
| 29 | ``` |
| 30 | |
| 31 | Note that you can also use the `inherit` keyword to inherit parent values for an indicator and add additional indicator |
| 32 | CSS selectors: |
| 33 | |
| 34 | ```html |
| 35 | <main hx-indicator="#global-indicator"> |
| 36 | ... |
| 37 | <button hx-post="/example" hx-indicator="inherit, #spinner"> |
| 38 | Post It! |
| 39 | </button> |
| 40 | <img id="spinner" class="htmx-indicator" src="/img/bars.svg" alt="Loading..."/> |
| 41 | </main> |
| 42 | ``` |
| 43 | |
| 44 | When a request is in flight, this will cause the `htmx-request` class to be added to the `#spinner` |
| 45 | image. The image also has the `htmx-indicator` class on it, which defines an opacity transition |
| 46 | that will show the spinner: |
| 47 | |
| 48 | ```css |
| 49 | .htmx-indicator { |
| 50 | opacity: 0; |
| 51 | visibility: hidden; |
| 52 | } |
| 53 | .htmx-request .htmx-indicator, |
| 54 | .htmx-request.htmx-indicator { |
| 55 | opacity: 1; |
| 56 | visibility: visible; |
| 57 | transition: opacity 200ms ease-in; |
| 58 | } |
| 59 | ``` |
| 60 | |
| 61 | This default `htmx-indicator` CSS also sets the visibility to hidden for better screen reader accessibility and does a |
| 62 | quick fade in of the opacity. |
| 63 | |
| 64 | If you would prefer a different effect for showing the spinner you could define and use your own indicator |
| 65 | CSS. Here is an example that uses `display` rather than opacity (Note that we use `my-indicator` instead of |
| 66 | `htmx-indicator`): |
| 67 | |
| 68 | ```css |
| 69 | .my-indicator{ |
| 70 | display:none; |
| 71 | } |
| 72 | .htmx-request .my-indicator, |
| 73 | .htmx-request.my-indicator{ |
| 74 | display:inline; |
| 75 | } |
| 76 | ``` |
| 77 | |
| 78 | Note that the target of the `hx-indicator` selector need not be the exact element that you |
| 79 | want to show: it can be any element in the parent hierarchy of the indicator. |
| 80 | |
| 81 | Finally, note that the `htmx-request` class by default is added to the element causing |
| 82 | the request, so you can place an indicator inside of that element and not need to explicitly |
| 83 | call it out with the `hx-indicator` attribute: |
| 84 | |
| 85 | ```html |
| 86 | <button hx-post="/example"> |
| 87 | Post It! |
| 88 | <img class="htmx-indicator" src="/img/bars.svg" alt="Loading..."/> |
| 89 | </button> |
| 90 | ``` |
| 91 | |
| 92 | ## Delaying the Indicator |
| 93 | |
| 94 | For fast requests, briefly flashing a spinner can look jittery. |
| 95 | |
| 96 | You can control this by adding a delay to the CSS transition for showing the indicator: |
| 97 | |
| 98 | ```css |
| 99 | .htmx-indicator { |
| 100 | opacity: 0; |
| 101 | visibility: hidden; |
| 102 | } |
| 103 | .htmx-request .htmx-indicator, |
| 104 | .htmx-request.htmx-indicator { |
| 105 | opacity: 1; |
| 106 | visibility: visible; |
| 107 | transition: opacity 200ms ease-in 200ms; /* fade duration, then delay */ |
| 108 | } |
| 109 | ``` |
| 110 | |
| 111 | ## Demo |
| 112 | |
| 113 | This simulates what a spinner might look like in that situation: |
| 114 | |
| 115 | ```html |
| 116 | <button class="btn" classes="toggle htmx-request:3s"> |
| 117 | Post It! |
| 118 | <img class="htmx-indicator" src="/img/bars.svg" alt="Loading..."/> |
| 119 | </button> |
| 120 | ``` |
| 121 | |
| 122 | ## Notes |
| 123 | |
| 124 | * In the absence of an explicit indicator, the `htmx-request` class will be added to the element triggering the |
| 125 | request |
| 126 | * If you want to use your own CSS but still use `htmx-indicator` as class name, then you need to disable |
| 127 | `includeIndicatorCSS`. See [Configuring htmx](/docs#configuration). The easiest way is to add this to the |
| 128 | `<head>` of your HTML: |
| 129 | |
| 130 | ```html |
| 131 | <meta name="htmx-config" content='{"includeIndicatorCSS": false}'> |
| 132 | ``` |
| 133 | |
| 134 | * If you want to use your own CSS and disable the built-in indicator styles entirely, set `includeIndicatorCSS` to |
| 135 | `false` and host your own CSS file with a copy of your preferred `htmx-indicator` style from above |