| 1 | --- |
| 2 | title: "Animations" |
| 3 | description: Animate content as it swaps in |
| 4 | icon: "icon-[mdi--animation]" |
| 5 | soon: true |
| 6 | --- |
| 7 | htmx is designed to allow you to use [CSS transitions](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Transitions/Using_CSS_transitions) |
| 8 | to add smooth animations and transitions to your web page using only CSS and HTML. Below are a few examples of |
| 9 | various animation techniques. |
| 10 | |
| 11 | htmx also allows you to use the new [View Transitions API](https://developer.mozilla.org/en-US/docs/Web/API/View_Transitions_API) |
| 12 | for creating animations. |
| 13 | |
| 14 | ### Basic CSS Animations |
| 15 | |
| 16 | ### Color Throb |
| 17 | |
| 18 | The simplest animation technique in htmx is to keep the `id` of an element stable across a content swap. If the |
| 19 | `id` of an element is kept stable, htmx will swap it in such a way that CSS transitions can be written between |
| 20 | the old version of the element and the new one. |
| 21 | |
| 22 | Consider this div: |
| 23 | |
| 24 | ```html |
| 25 | <style> |
| 26 | .smooth { |
| 27 | transition: all 1s ease-in; |
| 28 | } |
| 29 | </style> |
| 30 | <div id="color-demo" class="smooth" style="color:red" |
| 31 | hx-get="/colors" hx-swap="outerHTML" hx-trigger="every 1s"> |
| 32 | Color Swap Demo |
| 33 | </div> |
| 34 | |
| 35 | ``` |
| 36 | |
| 37 | This div will poll every second and will get replaced with new content which changes the `color` style to a new value |
| 38 | (e.g. `blue`): |
| 39 | |
| 40 | ```html |
| 41 | <div id="color-demo" class="smooth" style="color:blue" |
| 42 | hx-get="/colors" hx-swap="outerHTML" hx-trigger="every 1s"> |
| 43 | Color Swap Demo |
| 44 | </div> |
| 45 | ``` |
| 46 | |
| 47 | Because the div has a stable id, `color-demo`, htmx will structure the swap such that a CSS transition, defined on the |
| 48 | `.smooth` class, applies to the style update from `red` to `blue`, and smoothly transitions between them. |
| 49 | |
| 50 | [//]: # (#### Demo ) |
| 51 | |
| 52 | [//]: # () |
| 53 | [//]: # (<style>) |
| 54 | |
| 55 | [//]: # (.smooth {) |
| 56 | |
| 57 | [//]: # ( transition: all 1s ease-in;) |
| 58 | |
| 59 | [//]: # (}) |
| 60 | |
| 61 | [//]: # (</style>) |
| 62 | |
| 63 | [//]: # (<div id="color-demo" class="smooth" style="color:red") |
| 64 | |
| 65 | [//]: # ( hx-get="/colors" hx-swap="outerHTML" hx-trigger="every 1s">) |
| 66 | |
| 67 | [//]: # ( Color Swap Demo) |
| 68 | |
| 69 | [//]: # (</div>) |
| 70 | |
| 71 | [//]: # () |
| 72 | [//]: # (<script>) |
| 73 | |
| 74 | [//]: # ( var colors = ['blue', 'green', 'orange', 'red'];) |
| 75 | |
| 76 | [//]: # ( onGet("/colors", function () {) |
| 77 | |
| 78 | [//]: # ( var color = colors.shift();) |
| 79 | |
| 80 | [//]: # ( colors.push(color);) |
| 81 | |
| 82 | [//]: # ( return '<div id="color-demo" hx-get="/colors" hx-swap="outerHTML" class="smooth" hx-trigger="every 1s" style="color:' + color + '">\n'+) |
| 83 | |
| 84 | [//]: # ( ' Color Swap Demo\n'+) |
| 85 | |
| 86 | [//]: # ( '</div>\n') |
| 87 | |
| 88 | [//]: # ( });) |
| 89 | |
| 90 | [//]: # (</script>) |
| 91 | |
| 92 | ### Smooth Progress Bar |
| 93 | |
| 94 | The [Progress Bar](/patterns/loading/progress-bar) demo uses this basic CSS animation technique as well, by updating the `length` |
| 95 | property of a progress bar element, allowing for a smooth animation. |
| 96 | |
| 97 | ## Swap Transitions |
| 98 | |
| 99 | ### Fade Out On Swap |
| 100 | |
| 101 | If you want to fade out an element that is going to be removed when the request ends, you want to take advantage |
| 102 | of the `htmx-swapping` class with some CSS and extend the swap phase to be long enough for your animation to |
| 103 | complete. This can be done like so: |
| 104 | |
| 105 | ```html |
| 106 | <style> |
| 107 | .fade-me-out.htmx-swapping { |
| 108 | opacity: 0; |
| 109 | transition: opacity 1s ease-out; |
| 110 | } |
| 111 | </style> |
| 112 | <button class="fade-me-out" |
| 113 | hx-delete="/fade_out_demo" |
| 114 | hx-swap="outerHTML swap:1s"> |
| 115 | Fade Me Out |
| 116 | </button> |
| 117 | ``` |
| 118 | |
| 119 | [//]: # (#### Demo ) |
| 120 | |
| 121 | [//]: # () |
| 122 | [//]: # (<style>) |
| 123 | |
| 124 | [//]: # (.fade-me-out.htmx-swapping {) |
| 125 | |
| 126 | [//]: # ( opacity: 0;) |
| 127 | |
| 128 | [//]: # ( transition: opacity 1s ease-out;) |
| 129 | |
| 130 | [//]: # (}) |
| 131 | |
| 132 | [//]: # (</style>) |
| 133 | |
| 134 | [//]: # () |
| 135 | [//]: # (<button class="fade-me-out") |
| 136 | |
| 137 | [//]: # ( hx-delete="/fade_out_demo") |
| 138 | |
| 139 | [//]: # ( hx-swap="outerHTML swap:1s">) |
| 140 | |
| 141 | [//]: # ( Delete Me) |
| 142 | |
| 143 | [//]: # (</button>) |
| 144 | |
| 145 | [//]: # () |
| 146 | [//]: # (<script>) |
| 147 | |
| 148 | [//]: # ( onDelete("/fade_out_demo", function () {return ""});) |
| 149 | |
| 150 | [//]: # (</script>) |
| 151 | |
| 152 | ## Settling Transitions |
| 153 | |
| 154 | ### Fade In On Addition |
| 155 | |
| 156 | Building on the last example, we can fade in the new content by using the `htmx-added` class during the settle |
| 157 | phase. You can also write CSS transitions against the target, rather than the new content, by using the `htmx-settling` |
| 158 | class. |
| 159 | |
| 160 | ```html |
| 161 | <style> |
| 162 | #fade-me-in.htmx-added { |
| 163 | opacity: 0; |
| 164 | } |
| 165 | #fade-me-in { |
| 166 | opacity: 1; |
| 167 | transition: opacity 1s ease-out; |
| 168 | } |
| 169 | </style> |
| 170 | <button id="fade-me-in" |
| 171 | class="btn primary" |
| 172 | hx-post="/fade_in_demo" |
| 173 | hx-swap="outerHTML settle:1s"> |
| 174 | Fade Me In |
| 175 | </button> |
| 176 | ``` |
| 177 | |
| 178 | [//]: # (#### Demo ) |
| 179 | |
| 180 | [//]: # () |
| 181 | [//]: # (<style>) |
| 182 | |
| 183 | [//]: # (#fade-me-in.htmx-added {) |
| 184 | |
| 185 | [//]: # ( opacity: 0;) |
| 186 | |
| 187 | [//]: # (}) |
| 188 | |
| 189 | [//]: # (#fade-me-in {) |
| 190 | |
| 191 | [//]: # ( opacity: 1;) |
| 192 | |
| 193 | [//]: # ( transition: opacity 1s ease-out;) |
| 194 | |
| 195 | [//]: # (}) |
| 196 | |
| 197 | [//]: # (</style>) |
| 198 | |
| 199 | [//]: # () |
| 200 | [//]: # (<button id="fade-me-in") |
| 201 | |
| 202 | [//]: # ( class="btn primary") |
| 203 | |
| 204 | [//]: # ( hx-post="/fade_me_in") |
| 205 | |
| 206 | [//]: # ( hx-swap="outerHTML settle:1s">) |
| 207 | |
| 208 | [//]: # ( Fade Me In) |
| 209 | |
| 210 | [//]: # (</button>) |
| 211 | |
| 212 | [//]: # () |
| 213 | [//]: # (<script>) |
| 214 | |
| 215 | [//]: # ( onPost("/fade_me_in", function () {return "<button id=\"fade-me-in\"\n"+) |
| 216 | |
| 217 | [//]: # ( " class=\"btn primary\"\n"+) |
| 218 | |
| 219 | [//]: # ( " hx-post=\"/fade_me_in\"\n"+) |
| 220 | |
| 221 | [//]: # ( " hx-swap=\"outerHTML settle:1s\">\n"+) |
| 222 | |
| 223 | [//]: # ( " Fade Me In\n"+) |
| 224 | |
| 225 | [//]: # ( "</button>"});) |
| 226 | |
| 227 | [//]: # (</script>) |
| 228 | |
| 229 | ## Request In Flight Animation |
| 230 | |
| 231 | You can also take advantage of the `htmx-request` class, which is applied to the element that triggers a request. Below |
| 232 | is a form that on submit will change its look to indicate that a request is being processed: |
| 233 | |
| 234 | ```html |
| 235 | <style> |
| 236 | form.htmx-request { |
| 237 | opacity: .5; |
| 238 | transition: opacity 300ms linear; |
| 239 | } |
| 240 | </style> |
| 241 | <form hx-post="/name" hx-swap="outerHTML"> |
| 242 | <label>Name:</label><input name="name"><br/> |
| 243 | <button class="btn primary">Submit</button> |
| 244 | </form> |
| 245 | ``` |
| 246 | |
| 247 | [//]: # (#### Demo ) |
| 248 | |
| 249 | [//]: # () |
| 250 | [//]: # (<style>) |
| 251 | |
| 252 | [//]: # ( form.htmx-request {) |
| 253 | |
| 254 | [//]: # ( opacity: .5;) |
| 255 | |
| 256 | [//]: # ( transition: opacity 300ms linear;) |
| 257 | |
| 258 | [//]: # ( }) |
| 259 | |
| 260 | [//]: # (</style>) |
| 261 | |
| 262 | [//]: # () |
| 263 | [//]: # (<div aria-live="polite">) |
| 264 | |
| 265 | [//]: # (<form hx-post="/name" hx-swap="outerHTML">) |
| 266 | |
| 267 | [//]: # (<label>Name:</label><input name="name"><br/>) |
| 268 | |
| 269 | [//]: # (<button class="btn primary">Submit</button>) |
| 270 | |
| 271 | [//]: # (</form>) |
| 272 | |
| 273 | [//]: # (</div>) |
| 274 | |
| 275 | [//]: # () |
| 276 | [//]: # (<script>) |
| 277 | |
| 278 | [//]: # ( onPost("/name", function(){ return "Submitted!"; });) |
| 279 | |
| 280 | [//]: # (</script>) |
| 281 | |
| 282 | ## Using the htmx `class-tools` Extension |
| 283 | |
| 284 | Many interesting animations can be created by using the [`class-tools`](https://github.com/bigskysoftware/htmx-extensions/blob/main/src/class-tools/README) extension. |
| 285 | |
| 286 | Here is an example that toggles the opacity of a div. Note that we set the toggle time to be a bit longer than |
| 287 | the transition time. This avoids flickering that can happen if the transition is interrupted by a class change. |
| 288 | |
| 289 | ```html |
| 290 | <style> |
| 291 | .demo.faded { |
| 292 | opacity:.3; |
| 293 | } |
| 294 | .demo { |
| 295 | opacity:1; |
| 296 | transition: opacity ease-in 900ms; |
| 297 | } |
| 298 | </style> |
| 299 | <div class="demo" classes="toggle faded:1s">Toggle Demo</div> |
| 300 | ``` |
| 301 | |
| 302 | #### Demo |
| 303 | |
| 304 | <style> |
| 305 | .demo.faded { |
| 306 | opacity:.3; |
| 307 | } |
| 308 | .demo { |
| 309 | opacity:1; |
| 310 | transition: opacity ease-in 900ms; |
| 311 | } |
| 312 | </style> |
| 313 | <div class="demo" classes="toggle faded:1s">Toggle Demo</div> |
| 314 | |
| 315 | ### Using the View Transition API |
| 316 | |
| 317 | htmx provides access to the new [View Transitions API](https://developer.mozilla.org/en-US/docs/Web/API/View_Transitions_API) |
| 318 | via the `transition` option of the [`hx-swap`](/reference/attributes/hx-swap) attribute. |
| 319 | |
| 320 | Below is an example of a swap that uses a view transition. The transition is tied to the outer div via a |
| 321 | `view-transition-name` property in CSS, and that transition is defined in terms of `::view-transition-old` |
| 322 | and `::view-transition-new`, using `@keyframes` to define the animation. (Fuller details on the View Transition |
| 323 | API can be found on the [Chrome Developer Page](https://developer.chrome.com/docs/web-platform/view-transitions/) on them.) |
| 324 | |
| 325 | The old content of this transition should slide out to the left and the new content should slide in from the right. |
| 326 | |
| 327 | Note that, as of this writing, the visual transition will only occur on Chrome 111+, but more browsers are expected to |
| 328 | implement this feature in the near future. |
| 329 | |
| 330 | ```html |
| 331 | <style> |
| 332 | @keyframes fade-in { |
| 333 | from { opacity: 0; } |
| 334 | } |
| 335 | |
| 336 | @keyframes fade-out { |
| 337 | to { opacity: 0; } |
| 338 | } |
| 339 | |
| 340 | @keyframes slide-from-right { |
| 341 | from { transform: translateX(90px); } |
| 342 | } |
| 343 | |
| 344 | @keyframes slide-to-left { |
| 345 | to { transform: translateX(-90px); } |
| 346 | } |
| 347 | |
| 348 | .slide-it { |
| 349 | view-transition-name: slide-it; |
| 350 | } |
| 351 | |
| 352 | ::view-transition-old(slide-it) { |
| 353 | animation: 180ms cubic-bezier(0.4, 0, 1, 1) both fade-out, |
| 354 | 600ms cubic-bezier(0.4, 0, 0.2, 1) both slide-to-left; |
| 355 | } |
| 356 | ::view-transition-new(slide-it) { |
| 357 | animation: 420ms cubic-bezier(0, 0, 0.2, 1) 90ms both fade-in, |
| 358 | 600ms cubic-bezier(0.4, 0, 0.2, 1) both slide-from-right; |
| 359 | } |
| 360 | </style> |
| 361 | |
| 362 | |
| 363 | <div class="slide-it"> |
| 364 | <h1>Initial Content</h1> |
| 365 | <button class="btn primary" hx-get="/new-content" hx-swap="innerHTML transition:true" hx-target="closest div"> |
| 366 | Swap It! |
| 367 | </button> |
| 368 | </div> |
| 369 | ``` |
| 370 | |
| 371 | [//]: # (#### Demo) |
| 372 | |
| 373 | [//]: # () |
| 374 | [//]: # (<style>) |
| 375 | |
| 376 | [//]: # ( @keyframes fade-in {) |
| 377 | |
| 378 | [//]: # ( from { opacity: 0; }) |
| 379 | |
| 380 | [//]: # ( }) |
| 381 | |
| 382 | [//]: # () |
| 383 | [//]: # ( @keyframes fade-out {) |
| 384 | |
| 385 | [//]: # ( to { opacity: 0; }) |
| 386 | |
| 387 | [//]: # ( }) |
| 388 | |
| 389 | [//]: # () |
| 390 | [//]: # ( @keyframes slide-from-right {) |
| 391 | |
| 392 | [//]: # ( from { transform: translateX(90px); }) |
| 393 | |
| 394 | [//]: # ( }) |
| 395 | |
| 396 | [//]: # () |
| 397 | [//]: # ( @keyframes slide-to-left {) |
| 398 | |
| 399 | [//]: # ( to { transform: translateX(-90px); }) |
| 400 | |
| 401 | [//]: # ( }) |
| 402 | |
| 403 | [//]: # () |
| 404 | [//]: # ( .slide-it {) |
| 405 | |
| 406 | [//]: # ( view-transition-name: slide-it;) |
| 407 | |
| 408 | [//]: # ( }) |
| 409 | |
| 410 | [//]: # () |
| 411 | [//]: # ( ::view-transition-old(slide-it) {) |
| 412 | |
| 413 | [//]: # ( animation: 180ms cubic-bezier(0.4, 0, 1, 1) both fade-out,) |
| 414 | |
| 415 | [//]: # ( 600ms cubic-bezier(0.4, 0, 0.2, 1) both slide-to-left;) |
| 416 | |
| 417 | [//]: # ( }) |
| 418 | |
| 419 | [//]: # ( ::view-transition-new(slide-it) {) |
| 420 | |
| 421 | [//]: # ( animation: 420ms cubic-bezier(0, 0, 0.2, 1) 90ms both fade-in,) |
| 422 | |
| 423 | [//]: # ( 600ms cubic-bezier(0.4, 0, 0.2, 1) both slide-from-right;) |
| 424 | |
| 425 | [//]: # ( }) |
| 426 | |
| 427 | [//]: # (</style>) |
| 428 | |
| 429 | [//]: # () |
| 430 | [//]: # (<div class="slide-it">) |
| 431 | |
| 432 | [//]: # ( <h1>Initial Content</h1>) |
| 433 | |
| 434 | [//]: # ( <button class="btn primary" hx-get="/new-content" hx-swap="innerHTML transition:true" hx-target="closest div">) |
| 435 | |
| 436 | [//]: # ( Swap It!) |
| 437 | |
| 438 | [//]: # ( </button>) |
| 439 | |
| 440 | [//]: # (</div>) |
| 441 | |
| 442 | [//]: # () |
| 443 | [//]: # (<script>) |
| 444 | |
| 445 | [//]: # ( var originalContent = htmx.find(".slide-it").innerHTML;) |
| 446 | |
| 447 | [//]: # () |
| 448 | [//]: # ( this.server.respondWith("GET", "/new-content", function(xhr){) |
| 449 | |
| 450 | [//]: # ( xhr.respond(200, {}, "<h1>New Content</h1> <button class='btn danger' hx-get='/original-content' hx-swap='innerHTML transition:true' hx-target='closest div'>Restore It! </button>")) |
| 451 | |
| 452 | [//]: # ( });) |
| 453 | |
| 454 | [//]: # () |
| 455 | [//]: # ( this.server.respondWith("GET", "/original-content", function(xhr){) |
| 456 | |
| 457 | [//]: # ( xhr.respond(200, {}, originalContent)) |
| 458 | |
| 459 | [//]: # ( });) |
| 460 | |
| 461 | [//]: # (</script>) |
| 462 | |
| 463 | ## Conclusion |
| 464 | |
| 465 | You can use the techniques above to create quite a few interesting and pleasing effects with plain old HTML while using htmx. |