Customizing Tailwind CSS Animations: Advancing Your Web Design Skills
Tags: webdev, javascript, programming, beginners
Published: 2023-04-22 12:44:08

Tailwind CSS is a breakthrough CSS framework that has changed the way developers approach front-end development. 

Because of its low-level nature of it and its mobile-first approach, it has become a popular choice for designing unique, responsive, and scalable web designs.

It also features robust support for animations and enables developers to build engaging user experiences that draw in visitors.

In this detailed article, we'll look at animations using TailwindCSS. We'll go through the different sorts of animations provided by the framework and provide step-by-step instructions for incorporating them into your applications.
 
This post will guide individuals through the process of adding simple hover animations as well as more complicated animations.

By the end of this post, you would have acquired the necessary skills and expertise to create captivating animations for your website.

## Setting Up the Development Environment

To begin, we should install and set up TailwindCSS within our React app, as detailed below.

- Create a React app by running the command and choosing React as the framework:

```
npm init vite
```

- Next, install TailwindCSS and other dependencies like postcss and autoprefixer using the command:
```
npm install -D tailwindcss postcss autoprefixer
```

- Create config files by running the command: npx tailwindcss init -p and open the tailwind.config.css file and replace the content with the provided code snippet.

```
/** @type {import('tailwindcss').Config} */

module.exports = {
content: [
   "./src/**/*.{js,jsx,ts,tsx}",
 ],
 theme: {
   extend: {},
 },
 plugins: [],
}
```

- Lastly, paste the following code snippet inside the src/index.css file:
```
@tailwind base;
@tailwind components;
@tailwind utilities;
```

We can now use TailwindCSS in our React app. [Here](https://www.locofy.ai/blog/setup-and-configure-tailwindcss) is the blog article covering the installation process in-depth.

---

## A Deep Dive into TailwindCSS Animations for Modern Web Design

Tailwind CSS comes with four animation properties by default to add some visual flare to your web pages. These animations are intended to be simply integrated into your project to improve the user experience.

Here, we'll go over each of these animations in depth, with complete examples for incorporating them into our website design.

## 1. Animate-spin

Want to add a loading or spinning animation to your website? Simply include the "animate-spin" property and the animation will be automatically applied.

Let's take an example.

```
import React from 'react';

function App() {
   return (
       <div className='w-screen h-screen flex justify-center items-center'>
           <button
               type="button"
               className="flex bg-red-500 text-white px-4 py-2 rounded-md"
           >
               <svg className="h-5 w-5 mr-3 animate-spin" viewBox="0 0 24 24">
                   <circle cx="12" cy="12" r="10" stroke="currentColor" strokeWidth="4" fill="none" />
                   <path fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647zM12 20.735a8 8 0 008-8h4a12 12 0 01-12 12v-4.265zM20 12a8 8 0 01-8 8v4.265a12 12 0 0012-12h-4zm-8-6.735a8 8 0 018-8v-4.265a12 12 0 00-12 12h4z" />
               </svg>
               Processing...
           </button>
       </div >
   );
}

export default App;
```

Here is the output:

![Animate-spin](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/big77wzmvzjqufmru2um.png)

## 2. Animate-pulse

In addition to the "animate-spin" property, TailwindCSS also offers the "animate-pulse" property for fading elements in and out. This property is particularly useful for creating skeleton loaders and other such visual effects.

```
import React from 'react';

function App() {
   return (
       <div className='w-screen h-screen flex justify-center items-center'>
           <button
               type="button"
               className="flex bg-red-500 text-white px-4 py-2 rounded-md animate-pulse"
           >
               <svg className="h-5 w-5 mr-3 animate-spin" viewBox="0 0 24 24">
                   <circle cx="12" cy="12" r="10" stroke="currentColor" strokeWidth="4" fill="none" />
                   <path fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647zM12 20.735a8 8 0 008-8h4a12 12 0 01-12 12v-4.265zM20 12a8 8 0 01-8 8v4.265a12 12 0 0012-12h-4zm-8-6.735a8 8 0 018-8v-4.265a12 12 0 00-12 12h4z" />
               </svg>
               Processing...
           </button>
       </div >
   );
}

export default App;
```

And here is the output:

![Animate-pulse](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t5fqk5bjalez1i0kstte.png)

## 3. Animate-bounce

To make an element bounce up and down, just use the animate-bounce. This is useful if you want to draw attention to an element.

```
import React from 'react';

function App() {
   return (
       <div className='w-screen h-screen flex justify-center items-center'>
           <button
               type="button"
               className="flex bg-red-500 text-white px-4 py-2 rounded-md animate-bounce">
               Processing...
           </button>
       </div >
   );
}

export default App;
```

As a result, we get:

![Animate-bounce](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3h2afw9hfed52r8fprxt.png)

## 4. Animate-ping

The last Tailwindcss animation property is animate-ping. This property creates a radar ping or water ripple effect, making it perfect for notification badges and other similar design elements.

```
import React from 'react';

function App() {
   return (
       <div className='w-screen h-screen flex justify-center items-center'>
           <button
               type="button"
               className="flex bg-red-500 text-white px-4 py-2 rounded-md animate-ping"
           >
               Processing...
           </button>
       </div >
   );
}

export default App;
```

This is how it appears:

![Animate-ping](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g5bdvtcmzwmfofb41v9z.png)

These are the default animations and these can be applied to the hover or active state, as well:

```
<button type="button" className="hover:animate-ping"> Processing… </button>
```

These animations can be applied to the breakpoints.

```
<button className="md:animate-ping">Processing…</button>
```

---

## Create Custom Animations with TailwindCSS

TailwindCSS offers an easy approach to using built-in animations by passing only a single property. Developers can, however, also create customized animations depending on their own requirements.

For instance, one may design a unique animation by modifying the "tailwind.config.css file". This enables developers to produce animations that are distinctive and specifically suited to the needs of their projects.

**Inside tailwind.config.js:**

```
/** @type {import('tailwindcss').Config} */

module.exports = {
 content: [
   "./src/**/*.{js,jsx,ts,tsx}",
 ],
 theme: {
   extend: {
     animation: {
       'button-rotate': 'button-rotate 2s ease-in-out infinite',
     },
     keyframes: {
       'button-rotate': {
         '0%': { transform: 'scale(1) rotate(0deg)' },
         '50%': { transform: 'scale(1.5) rotate(180deg)' },
         '100%': { transform: 'scale(1) rotate(360deg)' },
       },
     },
   },
 },
 plugins: [],
}
```

In this case, it builds a custom animation called "button-rotate" with parameters including an infinite repeat rate, an easing function of "ease-in-out," and a duration of two seconds.

**Inside App.js file:**

```
import './App.css';

function App() {
 return (
   <div class="flex justify-center items-center h-screen w-screen">
     <button class="animate-button-rotate bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded-full">
       Click Me
     </button>
   </div>
 )
}

export default App;
```

And here is the output:

![Custom Animations](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h7spl26llmc5yti2ekoa.png)

---

## Leveraging Arbitrary Values for TailwindCSS Animations

With TailwindCSS, developers can use unique CSS attributes inside the framework thanks to a feature called "[arbitrary values](https://tailwindcss.com/docs/adding-custom-styles#using-arbitrary-values)." 

By adding values in square brackets to TailwindCSS classes, this functionality enables adjustment of design aspects like placement and padding.

For example, we can include properties like padding-[5px], bottom-[17px], and so on.

In short, the arbitrary values feature in TailwindCSS provides versatility and customization choices to swiftly produce aesthetically pleasing web designs.

The same can be used to create custom animation as well.

```
import React from 'react';

function App() {
 return (
   <div class="flex justify-center items-center h-screen w-screen">
     <button class="animate-[wiggle_1s_ease-in-out_infinite] bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded-full">
       Click Me
     </button>
   </div>
 );
}

export default App;
```

Here, we used "animate-[wiggle_1s_ease-in-out_infinite]" with specific settings. Let's specify the keyframes immediately.

**Inside tailwind.config.css**

```
/** @type {import('tailwindcss').Config} */

module.exports = {
 content: [
   "./src/**/*.{js,jsx,ts,tsx}",
 ],
 theme: {
   extend: {
     keyframes: {
       'wiggle': {
         '0%': { transform: 'scale(1) rotate(0deg)' },
         '50%': { transform: 'scale(1.5) rotate(180deg)' },
         '100%': { transform: 'scale(1) rotate(360deg)' },
       },
     },
   },
 },
 plugins: [],
}
```

The outcome will be the same as we specified within the custom animation sections.

---

## Go from Design to Code with TailwindCSS using Locofy.ai

While you can convert your UI designs in Figma & Adobe XD to code faster with Tailwind, it still requires a lot of effort and trial & error to get right.

Creating the pixel-perfect clone of your design files in React is no easy feat and requires several weeks of effort depending upon the complexity of your app. This is where [Locofy](https://www.locofy.ai/) can help you.

With the Locofy.ai plugin for Figma and Adobe XD, you can convert your designs to production-ready front-end code that works out-of-the-box with TailwindCSS, as well as prominent UI libraries such as Material UI and Bootstrap.

What's more is that you can even use drag-and-drop components offered by the plugin to quickly build designs & export responsive code in React, Next.js, Gatsby, React Native, and HTML-CSS.

---

Hope you like it.

That's it - thanks.