Tailwind CSS Animations
Animations are powerful tools for attracting user attention, providing feedback, and creating smooth experiences. Tailwind CSS includes some basic animation and transition utility classes, making it easy to add dynamic effects to your website.
Transitions
Transitions are the most common form of animation, smoothly adding animation effects when an element's CSS property changes. For example, when a button's background color changes in the hover state.
To enable transitions, you need to do three things:
- Add the
transitionclass: This is a base class that enables transition effects. - Set transition duration: Use
duration-{time}utility classes.duration-75,duration-100,duration-150,duration-200,duration-300,duration-500,duration-700,duration-1000
- Set transition timing function: Use
ease-{timing}utility classes.ease-linear: Linearease-in: Slow startease-out: Slow endease-in-out: (default) Slow start and end
In this example, when the mouse hovers, the changes to background color, Y-axis position, and scale will all happen smoothly within 300 milliseconds.
By default, Tailwind enables transitions for commonly used properties like colors, shadows, and transforms. You can control which properties participate in transitions individually using transition-{property} utility classes (like transition-colors).
Transforms
Transform allows you to rotate, scale, move, and skew elements without affecting other elements in the document flow.
- Scale:
scale-{amount},scale-x-{amount},scale-y-{amount}scale-95,scale-100,scale-110
- Rotate:
rotate-{angle}rotate-45,rotate-90,rotate-180
- Translate:
translate-x-{amount},translate-y-{amount}translate-x-4,translate-y-1/2,-translate-x-full
- Skew:
skew-x-{angle},skew-y-{angle}skew-x-3,skew-y-12
To use transform, you must first add the transform utility class to the element to enable GPU acceleration for smoother animation effects.
Animation
For more complex, keyframe-based animations, Tailwind provides some preset animation utility classes.
animate-spin: Used to create rotating animations for loading indicators.animate-ping: Creates an outward rippling effect, often used for notification badges.animate-pulse: Creates a slow fade-in and fade-out effect, often used for skeleton loading states.animate-bounce: Creates an up-and-down bouncing effect, often used for arrows indicating scrollability or clickability.
You can also define your own keyframe animations in tailwind.config.js and apply them using animate-{name}.