Here's a basic example demonstrating how to create an animation where an underline gradually appears on hover:
html<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Tailwind CSS Underline Hover Animation</title> <!-- Import Tailwind CSS --> <link href="https://cdn.jsdelivr.net/npm/tailwindcss@latest/dist/tailwind.min.css" rel="stylesheet"> <!-- Custom styles --> <style> /* Extend these classes in Tailwind configuration */ .hover-underline-animation { display: inline-block; position: relative; color: #3490dc; text-decoration: none; } .hover-underline-animation::after { content: ''; position: absolute; width: 100%; transform: scaleX(0); height: 2px; bottom: 0; left: 0; background-color: #3490dc; transform-origin: bottom right; transition: transform 0.25s ease-out; } .hover-underline-animation:hover::after { transform: scaleX(1); transform-origin: bottom left; } </style> </head> <body class="flex items-center justify-center h-screen"> <!-- Underline animation link --> <a href="#" class="hover-underline-animation text-xl"> Hover me to see the underline animation </a> </body> </html>
In the above code, we define a hover-underline-animation class that can be applied to any element where you want the underline hover animation. This class uses the pseudo-element :after to display the underline on hover and controls the animation effect using the transform property. The transition property ensures the animation is smooth.
You can also leverage TailwindCSS's @apply directive to directly apply utility classes in CSS, or extend your classes through the configuration file tailwind.config.js. These approaches maintain CSS consistency and maximize TailwindCSS's capabilities.