logo
BlogsTailwind CSS

Level Up Your Forms: Crafting Animated Inputs with Pure Tailwind CSS

# html# css# tailwind

Hey there, fellow code wizards! ๐Ÿ‘‹ Are you tired of those same old boring input fields? Well, get ready to spice up your forms without touching a single line of JavaScript! Today, we're diving into the magical world of Tailwind CSS to create some eye-catching, animated input fields that'll make your users go "Woah!" ๐Ÿ˜ฎ

The Secret Sauce ๐Ÿงช

Here's a tasty snippet that'll transform your humble input into a search bar with style:

<section class="grid h-screen place-content-center bg-slate-900">
  <div class="rounded-md bg-blue-100 p-8">
    <div class="relative flex w-72 items-center gap-1 border-b-4 border-blue-400 py-1 after:absolute after:-bottom-1 after:h-1 after:w-full after:origin-center after:scale-x-0 after:bg-blue-800 after:transition after:duration-300 after:content-[''] has-[:focus]:after:scale-x-100">
      ๐Ÿ”
      <input type="text" class="peer z-10 w-full bg-transparent text-sm outline-none [&::-placeholder-shown]:bg-red-500" placeholder="" />
      <span class="absolute left-8 select-none text-sm font-semibold text-blue-800 transition-all peer-[:focus]:left-3 peer-[:not(:placeholder-shown)]:left-3 peer-[:focus]:-translate-y-8 peer-[:not(:placeholder-shown)]:-translate-y-8">Search </span>
    </div>
  </div>
</section>

What's the Magic Here? โœจ

  1. Smooth Underline Animation: We're using the after pseudo-element to create a slick underline that grows when the input is focused. No JavaScript required!

  2. Floating Label: The label starts inside the input and gracefully floats up when you start typing or focus on the input. It's all thanks to Tailwind's peer class and some clever positioning.

  3. Magnifying Glass Icon: We've thrown in a little ๐Ÿ” emoji for that extra search bar feel. Feel free to swap it out with your favorite icon!

  4. Responsive Design: The input adjusts nicely to different screen sizes, thanks to Tailwind's utility classes.

Tailwind Playground: https://play.tailwindcss.com/YEKzJsT140

Why It's Awesome ๐ŸŒŸ

  • No JavaScript: This is all pure CSS magic, folks! Your page stays lean and mean.

  • Easy to Customize: Tweak the colors, sizes, and transitions to match your brand in seconds.

  • Accessible: The floating label ensures your form stays accessible and user-friendly.

Wrap Up ๐ŸŽ

There you have it! A slick, animated input field that'll make your forms stand out from the crowd. And the best part? It's all achieved with the power of Tailwind CSS. No extra JavaScript, no client-side logic โ€“ just pure, efficient styling.

So go ahead, copy this snippet into your project and watch your boring old inputs transform into interactive masterpieces. Your users (and your designer) will thank you!

Happy coding, and may your forms always be Fancy or Professional. ๐ŸŽจโœจ

Subscribe for our newsletter

Comments







ยฉ 2024 Developerthink. All Rights Reserved.