Using Animations with Tailwind CSS

Software developer since 2004. Web/front-end expert (HTML, CSS, JS). O'Reilly and Apress author. Freelance tech writer. Blogger. Bridging code and communication in tech.
Search for a command to run...

Software developer since 2004. Web/front-end expert (HTML, CSS, JS). O'Reilly and Apress author. Freelance tech writer. Blogger. Bridging code and communication in tech.
Note to anyone struggling with the above.
Looks like Tailwind has updated the 'extends' configuration since this was published.
"animations" has now been updated to "animation".
The article was helpful even with that issue - Thanks
Using semantic HTML elements has several benefits over using plain divs.

Vibe coding is a new way to code that is gaining popularity. But is it the future?

Looking back on my journey as a published author, from Apress to O'Reilly.

Learn how to use media queries to learn about certain device settings.

You’re working on a bug fix and find some bad code that causes a bug. The git blame command will tell you who the last developer was that modified that line, and when they modified it. But what if you don’t know what code introduced the bug? For thes...

Tailwind CSS is awesome. Animations are awesome. So let's combine them! It's really easy to define your own custom animations with Tailwind.
A CSS animation is defined by creating a set of keyframes. These keyframes define what CSS properties are applied at different points during the animation. When the animation is performed, the browser gradually transitions these properties between keyframes, creating an animation. Here's a simple example that makes an element spin:
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
These keyframes define the animation, and they are applied by using the animation CSS property on an element. This is a shorthand property that allows you to set multiple animation-related properties, such as animation-name and animation-duration, in a single line.
Let's apply the spin animation to an element:
.box {
margin: 50px;
width: 200px;
height: 200px;
background: red;
animation: spin 1s infinite;
}
Here is the result:
Now that we remember how animation works with CSS, let's do it the Tailwind way. Tailwind ships with a few built-in animations, such as:
spinpingpulsebounceThese animations can then be applied to an element by adding the class animate-<animation name>, e.g. animate-spin.
Let's say we want to do a fancier animation than the ones that are built in. Let's add a roll animation that moves and rotates the element. We can extend Tailwind to add new animation classes to do whatever we want.
To do this, you will need to edit your Tailwind configuration file. This is where the custom keyframes and animations are defined. These are set by specifying or extending the keyframes and animations properties in the theme.
First, let's define the keyframes:
module.exports = {
theme: {
extend: {
keyframes: {
roll: {
'0%, 100%': { transform: 'translateX(0) rotate(0deg)' },
'50%': { transform: 'translateX(20rem) rotate(385deg)' }
}
}
}
}
};
As you can see, Tailwind lets you combine keyframes if they have the same CSS properties. The keyframes we've defined here have the element in its original position with no rotation at the beginning and the end of the animation. At the halfway point of the animation, the element is moved 20rem to the right and rotated 385 degrees.
Next, we have to define an animation that uses these keyframes, under the animations property:
module.exports = {
theme: {
extend: {
keyframes: {
roll: {
'0%, 100%': { transform: 'translateX(0) rotate(0deg)' },
'50%': { transform: 'translateX(20rem) rotate(385deg)' }
}
},
animations: {
roll: 'roll 3s ease-in-out infinite'
}
}
}
};
These changes will cause Tailwind to define a new class called animate-roll. All we have to do now is apply that class to an element, along with a little extra Tailwind styling, and we're done!
<div class="p-20">
<div class="animate-roll bg-blue-600 border-2 border-blue-700 text-white px-8 py-2 rounded-md text-center w-40 font-bold text-xl">
Hello Tailwind!
</div>
</div>
The final result can be seen in this Tailwind Play playground.
