Post

Tailwind CSS Transitions And Animations

Transitions and animations can add a polished and interactive feel to your web application. Tailwind CSS provides utility classes to easily apply transitions and animations to elements.

Transitions

Transitions allow you to smoothly animate changes to CSS properties, such as changing colors, sizes, or positions. Tailwind CSS provides utility classes to specify transition properties and durations.

Here’s how you can use transition classes in Tailwind CSS:

1
2
3
<button class="bg-blue-500 hover:bg-blue-700 transition-colors duration-300 text-white font-bold py-2 px-4 rounded">
    Hover over me
</button>

In this example:

  • transition-colors class specifies that the transition will be applied to color changes.
  • duration-300 class specifies the duration of the transition in milliseconds (300ms in this case).

When you hover over the button, you’ll notice that the color change is animated smoothly over the specified duration.

Animations

Tailwind CSS also provides utility classes to apply pre-defined animations to elements. These animations are based on the popular animate.css library.

Here’s how you can use animation classes in Tailwind CSS:

1
<div class="animate-bounce">Bouncing element</div>

In this example, the animate-bounce class applies a bouncing animation to the element.

Customizing Transitions and Animations

Tailwind CSS allows you to customize transitions and animations by defining custom CSS variables in your configuration file (tailwind.config.js) and then using those variables in your utility classes.

For example, you can define custom transition durations:

// tailwind.config.js

1
2
3
4
5
6
7
8
9
module.exports = {
  theme: {
    extend: {
      transitionDuration: {
        '2000': '2000ms',
      }
    }
  }
}

Then, you can use the custom transition duration in your HTML:

1
2
3
<button class="bg-blue-500 hover:bg-blue-700 transition-colors duration-2000 text-white font-bold py-2 px-4 rounded">
    Hover over me
</button>

This will apply a transition with a duration of 2000 milliseconds (2 seconds) when the button is hovered over.

This post is licensed under CC BY 4.0 by the author.