+++ title = "Animations" template = "demo.html" +++ htmx is designed to allow you to use [CSS transitions](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Transitions/Using_CSS_transitions) to add smooth animations and transitions to your web page using only CSS and HTML. Below are a few examples of various animation techniques. htmx also allows you to use the new [View Transitions API](https://developer.mozilla.org/en-US/docs/Web/API/View_Transitions_API) for creating animations. ### Basic CSS Animations {#basic} ### Color Throb The simplest animation technique in htmx is to keep the `id` of an element stable across a content swap. If the `id` of an element is kept stable, htmx will swap it in such a way that CSS transitions can be written between the old version of the element and the new one. Consider this div: ```html
Color Swap Demo
``` This div will poll every second and will get replaced with new content which changes the `color` style to a new value (e.g. `blue`): ```html
Color Swap Demo
``` Because the div has a stable id, `color-demo`, htmx will structure the swap such that a CSS transition, defined on the `.smooth` class, applies to the style update from `red` to `blue`, and smoothly transitions between them. #### Demo {#throb-demo}
Color Swap Demo
### Smooth Progress Bar The [Progress Bar](@/examples/progress-bar.md) demo uses this basic CSS animation technique as well, by updating the `length` property of a progress bar element, allowing for a smooth animation. ## Swap Transitions {#swapping} ### Fade Out On Swap If you want to fade out an element that is going to be removed when the request ends, you want to take advantage of the `htmx-swapping` class with some CSS and extend the swap phase to be long enough for your animation to complete. This can be done like so: ```html ``` #### Demo {#fade-swap-demo} ## Settling Transitions {#settling} ### Fade In On Addition Building on the last example, we can fade in the new content by using the `htmx-added` class during the settle phase. You can also write CSS transitions against the target, rather than the new content, by using the `htmx-settling` class. ```html ``` #### Demo {#fade-settle-demo} ## Request In Flight Animation {#request} You can also take advantage of the `htmx-request` class, which is applied to the element that triggers a request. Below is a form that on submit will change its look to indicate that a request is being processed: ```html

``` #### Demo {#request-demo}

## Using the htmx `class-tools` Extension Many interesting animations can be created by using the [`class-tools`](https://github.com/bigskysoftware/htmx-extensions/blob/main/src/class-tools/README.md) extension. Here is an example that toggles the opacity of a div. Note that we set the toggle time to be a bit longer than the transition time. This avoids flickering that can happen if the transition is interrupted by a class change. ```html
Toggle Demo
``` #### Demo {#class-tools-demo}
Toggle Demo
### Using the View Transition API {#view-transitions} htmx provides access to the new [View Transitions API](https://developer.mozilla.org/en-US/docs/Web/API/View_Transitions_API) via the `transition` option of the [`hx-swap`](/attributes/hx-swap) attribute. Below is an example of a swap that uses a view transition. The transition is tied to the outer div via a `view-transition-name` property in CSS, and that transition is defined in terms of `::view-transition-old` and `::view-transition-new`, using `@keyframes` to define the animation. (Fuller details on the View Transition API can be found on the [Chrome Developer Page](https://developer.chrome.com/docs/web-platform/view-transitions/) on them.) The old content of this transition should slide out to the left and the new content should slide in from the right. Note that, as of this writing, the visual transition will only occur on Chrome 111+, but more browsers are expected to implement this feature in the near future. ```html

Initial Content

``` #### Demo

Initial Content

## Conclusion You can use the techniques above to create quite a few interesting and pleasing effects with plain old HTML while using htmx.