Get Back
October 15, 2024
Text & Icon Tailwindcss Button
tricks

This component is designed to provide a visually engaging call-to-action button for web applications or websites. It combines text and an arrow icon, enhancing user interaction through animation.

Set up the HTML structure:

  • Create a div container with the classes flex, h-svh, and w-full to position the button in the center of the viewport.
  • Inside the container, create a button element with the classes group,
    overflow-hidden, rounded-md, bg-blue-500, px-4, py-1.5, text-sm, text-white, and shadow-md.

Add the text and arrow elements:

  • Inside the button, create a div with the classes relative, flex, and items-center to center the text and arrow vertically.
  • Inside this div, add another div with the classes group-hover:translate-x-20, transition-all, and duration-300 to create the text animation. This div will contain the text "Click Me!".
  • Add another div with the classes absolute, -left-10, top-1/2, -translate-y-1/2, transition-all, and duration-300 to create the arrow animation. This div will contain the arrow icon.

Add the arrow icon:

  • Inside the arrow div, add an SVG element with the appropriate attributes to display the arrow icon. You can use a library like Lucide for pre-made icons.

4. Style the button and animations:

  • Use Tailwind CSS classes to style the button's appearance, such as background color, text color, and shadow.
  • Use the group-hover:translate-x-20 class to make the text move 20 pixels to the right on hover.
  • Use the group-hover:left-1/2 and group-hover:-translate-x-1/2 classes to make the arrow move to the center of the button on hover.
  • Adjust the animation durations and timing functions as needed.

Here's a simplified example


    <div class="flex h-svh w-full items-center justify-center">
      <button class="group overflow-hidden rounded-md bg-blue-500 px-4 py-1.5 text-sm text-white shadow-md">
        <div class="relative flex items-center justify-center">
          <div class="group-hover:translate-x-20 transition-all duration-300 ease-in-out ">Click Me!</div>
          <div class="absolute -left-10 top-1/2 -translate-y-1/2 transition-all duration-300 ease-in-out group-hover:left-1/2 group-hover:-translate-x-1/2">
            <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-arrow-up-right">
              <path d="M7 7h10v10" />
              <path d="M7 17 17 7" />
            </svg>
          </div>
        </div>
      </button>
    </div>
    
            
Made With Kisses By Mohamed Rafik