DEV Community

Jesse Price
Jesse Price

Posted on

A Guide To Custom Animations in Tailwind Css.

Defining Animations in Tailwind CSS

To incorporate animations into your Tailwind CSS setup, you'll need to extend the theme in your tailwind.config.js file. Here’s how you can do it:

1. Extend Tailwind’s Configuration

You can add custom animations and keyframes to your Tailwind configuration like this:

// tailwind.config.ts
module.exports = {
  mode: 'jit',
  purge: ['./pages/**/*.{ts,tsx}', './components/**/*.{ts,tsx}'],
  theme: {
    extend: {
      animation: {
        'spin-slow': 'spin 3s linear infinite',
        'fade-in': 'fadeIn 2s ease-out',
        'bounce-slow': 'bounce 2s infinite'
      },
      keyframes: {
        fadeIn: {
          '0%': { opacity: '0' },
          '100%': { opacity: '1' }
        },
        bounce: {
          '0%, 100%': { transform: 'translateY(0)' },
          '50%': { transform: 'translateY(-1rem)' }
        }
      }
    },
  },
  variants: {
    extend: {},
  },
  plugins: [],
}
Enter fullscreen mode Exit fullscreen mode

2. Apply Animations in Your TypeScript Components

Once you’ve defined your animations, you can use them in your Next.js components. Here’s an example of how to apply these animations:

// pages/index.tsx
import React from 'react';

const Home: React.FC = () => {
  return (
    <div className="flex flex-col items-center justify-center min-h-screen bg-gray-100">
      <h1 className="text-4xl font-bold mb-4 animate-fade-in">Welcome to My Next.js App</h1>
      <div className="animate-spin-slow">
        <img src="/spinner.svg" alt="Loading" />
      </div>
      <button className="mt-4 px-6 py-3 bg-blue-500 text-white rounded-lg animate-bounce-slow">
        Click Me
      </button>
    </div>
  );
};

export default Home;
Enter fullscreen mode Exit fullscreen mode

By following these steps, you can create smooth and engaging animations for your web applications using Tailwind CSS.

Read More on Animations in Tailwind

Postgres on Neon - Get the Free Plan

No credit card required. The database you love, on a serverless platform designed to help you build faster.

Get Postgres on Neon

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more