乐闻世界logo
搜索文章和话题

How to using underline hover animation in tailwindcss?

2个答案

1
2

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.

2024年6月29日 12:07 回复

您可以使用组 max-w-{x} 和transition-all 在纯 TailwindCSS 中实现这一点,在跨度上使用组悬停,这样当您经过链接时它就会启动动画

shell
<a href="#" class="group text-sky-600 transition duration-300"> Link <span class="block max-w-0 group-hover:max-w-full transition-all duration-500 h-0.5 bg-sky-600"></span> </a>
2024年6月29日 12:07 回复

你的答案