This tutorial will guide you through creating a hover effect using Tailwind CSS that scales elements on hover. This is a great way to add subtle animation and visual feedback to your website’s interactive elements.
Step 1: Include Tailwind CSS
First, you need to include Tailwind CSS in your project. This usually involves adding a link to the Tailwind CSS CDN in the “ section of your HTML document.
<link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/tailwindcss/1.5.2/tailwind.min.css'>
Step 2: Create the HTML Structure
Next, create the basic HTML structure for your elements. This example uses divs with Tailwind CSS classes to style and position them. The key class for the animation is `hover:scale-110`, which scales the element to 110% of its original size on hover.
<section class="text-gray-700 body-font"> <div class="container px-5 py-24 mx-auto"> <div class="flex flex-wrap -m-4 text-center"> <div class="p-4 md:w-1/4 sm:w-1/2 w-full"> <div class="border-2 border-gray-600 px-4 py-6 rounded-lg transform transition duration-500 hover:scale-110"> <svg fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" class="text-indigo-500 w-12 h-12 mb-3 inline-block" viewBox="0 0 24 24"> <path d="M8 17l4 4 4-4m-4-5v9"></path> <path d="M20.88 18.09A5 5 0 0018 9h-1.26A8 8 0 103 16.29"></path> </svg> <h2 class="title-font font-medium text-3xl text-gray-900">2.7K</h2> <p class="leading-relaxed">Downloads</p> </div> </div> <div class="p-4 md:w-1/4 sm:w-1/2 w-full"> <div class="border-2 border-gray-600 px-4 py-6 rounded-lg transform transition duration-500 hover:scale-110"> <svg fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" class="text-indigo-500 w-12 h-12 mb-3 inline-block" viewBox="0 0 24 24"> <path d="M17 21v-2a4 4 0 00-4-4H5a4 4 0 00-4 4v2"></path> <circle cx="9" cy="7" r="4"></circle> <path d="M23 21v-2a4 4 0 00-3-3.87m-4-12a4 4 0 010 7.75"></path> </svg> <h2 class="title-font font-medium text-3xl text-gray-900">1.3K</h2> <p class="leading-relaxed">Users</p> </div> </div> <div class="p-4 md:w-1/4 sm:w-1/2 w-full"> <div class="border-2 border-gray-600 px-4 py-6 rounded-lg transform transition duration-500 hover:scale-110"> <svg fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" class="text-indigo-500 w-12 h-12 mb-3 inline-block" viewBox="0 0 24 24"> <path d="M3 18v-6a9 9 0 0118 0v6"></path> <path d="M21 19a2 2 0 01-2 2h-1a2 2 0 01-2-2v-3a2 2 0 012-2h3zM3 19a2 2 0 002 2h1a2 2 0 002-2v-3a2 2 0 00-2-2H3z"></path> </svg> <h2 class="title-font font-medium text-3xl text-gray-900">74</h2> <p class="leading-relaxed">Files</p> </div> </div> <div class="p-4 md:w-1/4 sm:w-1/2 w-full"> <div class="border-2 border-gray-600 px-4 py-6 rounded-lg transform transition duration-500 hover:scale-110"> <svg fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" class="text-indigo-500 w-12 h-12 mb-3 inline-block" viewBox="0 0 24 24"> <path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"></path> </svg> <h2 class="title-font font-medium text-3xl text-gray-900">46</h2> <p class="leading-relaxed">Places</p> </div> </div> </div> </div> </section>
Congratulations! You’ve successfully added a scale-on-hover animation using Tailwind CSS.