DEV Community

Cover image for Animate.css with Tailwind CSS

Animate.css with Tailwind CSS

Divyansh Singh
A sophomore at IIIT Kota.
・2 min read

If you have used Tailwind CSS, you might have noticed that it lacks proper animation utilities. It has only four animations by default - spin, ping, pulse, and bounce. This limitation makes us search for other methods and additional frameworks to animate the components. The most common one is Animate.css if you don't want to write all the keyframes yourself.

Animate.css (like Tailwind CSS) provides animation utility classes and works nicely alongside Tailwind. But since it is just a pure CSS library, it lacks Tailwind features like variants, directives, purging, and even editor tools like Tailwind Intellisense. But what if you can make Tailwind know about the animation classes provided by Animate.css?

The question will probably make you search for some plugin, and the first one on the list will be tailwindcss-animatecss. It is a great plugin in itself, except it requires quite a configuration. Although its adjustable (optional) settings are pretty nice, but setting the variants and required classes seems overkill in the new JIT mode.

So, I wrote a configuration myself – animated-tailwindcss. To use the configuration, you just need to install it, import it, and wrap your existing configuration with it. After being properly configured, you can use the animation utilities of Animate.css as if they are a part of the Tailwind CSS!

All your variants will work with the animation classes. You can use them inside the directives like @apply and @screen. Also, you will not have extra keyframes emitted to the code in production, and in development, you can use the Tailwind Intellisense without any extra configuration! The animation classes are motion-safe by default, i.e., animations will be disabled if the user prefers reduced motion.

Please give the configuration a try. If you feel that something went wrong, please comment or create an issue on GitHub. I am working on adding options to customize duration, delay, etc. They will be available soon!

Note that the only difference you will notice if you have used Animate.css earlier is that you need to reference the class names with the prefix animate- instead of animate__. Also, if you want, you can customize animations easily (add new or override the existing ones). Thank you!

Discussion (4)

flassgravel profile image

How do i add hover and active states to the animations eg:

brc_dd profile image
Divyansh Singh Author • Edited

@flassgravel Hey there! You can directly use it like animate-animated hover:animate-bounceIn if you are using JIT mode. Without JIT mode, you first need to enable the variants. Please refer the official docs.

Note that, using hover may result in flickering. This is not a bug, but is how CSS :hover pseudo-class works. In this particular case the moment you will hover, the element will be transformed to scale3d(0.3, 0.3, 0.3). So, if your hover position was not near the center your mouse will be out of the element. Hence, the animation will be cut off. But now since the element is back to its original size, it will begin again, and will flicker continuously until the pointer is over pivot or completely outside the element. To prevent this, use the resolution mentioned in this answer.

With React frameworks, you can easily do something like this:

flassgravel profile image

Thank you very much for your reply! I haven't got jit mode to work yet so i'll have to wait until then. Thanks anyways!

blackforestcode profile image
Nils Domin

Just tried, works great, thanks!!!