Published on

Most Effective Ways To Build A Floating Labels With Tailwind CSS

Floating labels

What is Tailwind CSS?

Tailwind CSS is a utility-first CSS framework that focuses on creating personalized user interfaces quickly. It can gives you all the building blocks you are able to create personalized designs without having to fight to override irritating opinionated styles. Also, Tailwind CSS is a highly configurable, low-level CSS framework.

The description of Floating labels ui component

Use these form elements with floating labels from flowbite inspired by google's material design to submit form data flowbite.com/docs/components/forms/#floating-labels

Why use Tailwind CSS to make a Floating labels ui component?

  • It can make the building process of Floating labels ui component faster and more easily.
  • Enables building complex responsive layouts and components freely.
  • Minimum lines of CSS code in Floating labels component file.

The preview of Floating labels ui component

Free download of the Floating labels's source code

The source code of Floating labels ui component

<!-- This is an example component -->
<div class="max-w-2xl mx-auto">

	<form>
		<div class="relative z-0 mb-6 w-full group">
			<input type="email" name="floating_email" class="block py-2.5 px-0 w-full text-sm text-gray-900 bg-transparent border-0 border-b-2 border-gray-300 appearance-none dark:text-white dark:border-gray-600 dark:focus:border-blue-500 focus:outline-none focus:ring-0 focus:border-blue-600 peer" placeholder=" " required />
			<label for="floating_email" class="absolute text-sm text-gray-500 dark:text-gray-400 duration-300 transform -translate-y-6 scale-75 top-3 -z-10 origin-[0] peer-focus:left-0 peer-focus:text-blue-600 peer-focus:dark:text-blue-500 peer-placeholder-shown:scale-100 peer-placeholder-shown:translate-y-0 peer-focus:scale-75 peer-focus:-translate-y-6">Email address</label>
		</div>
		<div class="relative z-0 mb-6 w-full group">
			<input type="password" name="floating_password" id="floating_password" class="block py-2.5 px-0 w-full text-sm text-gray-900 bg-transparent border-0 border-b-2 border-gray-300 appearance-none dark:text-white dark:border-gray-600 dark:focus:border-blue-500 focus:outline-none focus:ring-0 focus:border-blue-600 peer" placeholder=" " required />
			<label for="floating_password" class="absolute text-sm text-gray-500 dark:text-gray-400 duration-300 transform -translate-y-6 scale-75 top-3 -z-10 origin-[0] peer-focus:left-0 peer-focus:text-blue-600 peer-focus:dark:text-blue-500 peer-placeholder-shown:scale-100 peer-placeholder-shown:translate-y-0 peer-focus:scale-75 peer-focus:-translate-y-6">Password</label>
		</div>
		<div class="relative z-0 mb-6 w-full group">
			<input type="password" name="repeat_password" id="floating_repeat_password" class="block py-2.5 px-0 w-full text-sm text-gray-900 bg-transparent border-0 border-b-2 border-gray-300 appearance-none dark:text-white dark:border-gray-600 dark:focus:border-blue-500 focus:outline-none focus:ring-0 focus:border-blue-600 peer" placeholder=" " required />
			<label for="floating_repeat_password" class="absolute text-sm text-gray-500 dark:text-gray-400 duration-300 transform -translate-y-6 scale-75 top-3 -z-10 origin-[0] peer-focus:left-0 peer-focus:text-blue-600 peer-focus:dark:text-blue-500 peer-placeholder-shown:scale-100 peer-placeholder-shown:translate-y-0 peer-focus:scale-75 peer-focus:-translate-y-6">Confirm password</label>
		</div>
		<div class="grid xl:grid-cols-2 xl:gap-6">
			<div class="relative z-0 mb-6 w-full group">
				<input type="text" name="floating_first_name" id="floating_first_name" class="block py-2.5 px-0 w-full text-sm text-gray-900 bg-transparent border-0 border-b-2 border-gray-300 appearance-none dark:text-white dark:border-gray-600 dark:focus:border-blue-500 focus:outline-none focus:ring-0 focus:border-blue-600 peer" placeholder=" " required />
				<label for="floating_first_name" class="absolute text-sm text-gray-500 dark:text-gray-400 duration-300 transform -translate-y-6 scale-75 top-3 -z-10 origin-[0] peer-focus:left-0 peer-focus:text-blue-600 peer-focus:dark:text-blue-500 peer-placeholder-shown:scale-100 peer-placeholder-shown:translate-y-0 peer-focus:scale-75 peer-focus:-translate-y-6">First name</label>
			</div>
			<div class="relative z-0 mb-6 w-full group">
				<input type="text" name="floating_last_name" id="floating_last_name" class="block py-2.5 px-0 w-full text-sm text-gray-900 bg-transparent border-0 border-b-2 border-gray-300 appearance-none dark:text-white dark:border-gray-600 dark:focus:border-blue-500 focus:outline-none focus:ring-0 focus:border-blue-600 peer" placeholder=" " required />
				<label for="floating_last_name" class="absolute text-sm text-gray-500 dark:text-gray-400 duration-300 transform -translate-y-6 scale-75 top-3 -z-10 origin-[0] peer-focus:left-0 peer-focus:text-blue-600 peer-focus:dark:text-blue-500 peer-placeholder-shown:scale-100 peer-placeholder-shown:translate-y-0 peer-focus:scale-75 peer-focus:-translate-y-6">Last name</label>
			</div>
		</div>
		<div class="grid xl:grid-cols-2 xl:gap-6">
			<div class="relative z-0 mb-6 w-full group">
				<input type="tel" pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}" name="floating_phone" id="floating_phone" class="block py-2.5 px-0 w-full text-sm text-gray-900 bg-transparent border-0 border-b-2 border-gray-300 appearance-none dark:text-white dark:border-gray-600 dark:focus:border-blue-500 focus:outline-none focus:ring-0 focus:border-blue-600 peer" placeholder=" " required />
				<label for="floating_phone" class="absolute text-sm text-gray-500 dark:text-gray-400 duration-300 transform -translate-y-6 scale-75 top-3 -z-10 origin-[0] peer-focus:left-0 peer-focus:text-blue-600 peer-focus:dark:text-blue-500 peer-placeholder-shown:scale-100 peer-placeholder-shown:translate-y-0 peer-focus:scale-75 peer-focus:-translate-y-6">Phone number (123-456-7890)</label>
			</div>
			<div class="relative z-0 mb-6 w-full group">
				<input type="text" name="floating_company" id="floating_company" class="block py-2.5 px-0 w-full text-sm text-gray-900 bg-transparent border-0 border-b-2 border-gray-300 appearance-none dark:text-white dark:border-gray-600 dark:focus:border-blue-500 focus:outline-none focus:ring-0 focus:border-blue-600 peer" placeholder=" " required />
				<label for="floating_company" class="absolute text-sm text-gray-500 dark:text-gray-400 duration-300 transform -translate-y-6 scale-75 top-3 -z-10 origin-[0] peer-focus:left-0 peer-focus:text-blue-600 peer-focus:dark:text-blue-500 peer-placeholder-shown:scale-100 peer-placeholder-shown:translate-y-0 peer-focus:scale-75 peer-focus:-translate-y-6">Company (Ex. Google)</label>
			</div>
		</div>
		<button type="submit" class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 font-medium rounded-lg text-sm w-full sm:w-auto px-5 py-2.5 text-center dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800">Submit</button>
	</form>

	<p class="mt-5">Check out the original floating label form elements on <a class="text-blue-600 hover:underline"
			href="https://flowbite.com/docs/components/forms/#floating-labels" target="_blank">Flowbite</a> and browse other similar components built with Tailwind CSS.
	</p>
</div>

How to make a Floating labels with Tailwind CSS?

Install tailwind css of verion 2.2.19

Use the script html tag to import the script of Tailwind CSS of the version 2.2.19

<script src="https://cdn.tailwindcss.com"></script>

All the unility class needed to make a Floating labels component

  • max-w-2xl
  • mx-auto
  • relative
  • z-0
  • mb-6
  • w-full
  • block
  • py-2.5
  • px-0
  • text-sm
  • text-gray-900
  • bg-transparent
  • border-0
  • border-b-2
  • border-gray-300
  • dark:text-white
  • dark:border-gray-600
  • focus:border-blue-600
  • absolute
  • text-gray-500
  • dark:text-gray-400
  • top-3
  • -z-10
  • peer-focus:left-0
  • peer-focus:text-blue-600
  • grid
  • xl:grid-cols-2
  • xl:gap-6
  • text-white
  • bg-blue-700
  • hover:bg-blue-800
  • sm:w-auto
  • px-5
  • text-center
  • dark:bg-blue-600
  • mt-5
  • text-blue-600

37 steps to make a Floating labels component with Tailwind CSS

  1. Set the maximum width/height of an element using the max-w-2xl utilities.

  2. Control the horizontal margin of an element to auto using the mx-auto utilities.

  3. Use relative to position an element according to the normal flow of the document.

  4. Control the stack order (or three-dimensional positioning) of an element to 0 in Tailwind, regardless of order it has been displayed, using the z-0 utilities.

  5. Control the margin on bottom side of an element to 1.5rem using the mb-6 utilities.

  6. Use w-full to set an element to a 100% based width.

  7. Use inline utilities to put the element on its own line and fill its parent.

  8. Control the vertical padding of an element to 2.5 using the py-2.5 utilities.

  9. Control the horizontal padding of an element to 0rem using the px-0 utilities.

  10. Control the text color of an element to sm using the text-sm utilities.

  11. Control the text color of an element to gray-900 using the text-gray-900 utilities.

  12. Control the background color of an element to transparent using the bg-transparent utilities.

  13. Control the border color of an element to 0rem using the border-0 utilities.

  14. Control the border color of an element to b-2 using the border-b-2 utilities.

  15. Control the border color of an element to gray-300 using the border-gray-300 utilities.

  16. Control the text color of an element to white in dark theme using the dark:text-white utilities.

  17. Control the border color of an element to gray-600 using the dark:border-gray-600 utilities in dark theme.

  18. Control the border color of an element to blue-600 using the focus:border-blue-600 utilities on focus.

  19. Use absolute to position an element outside of the normal flow of the document, causing neighboring elements to act as if the element doesn’t exist.

  20. Control the text color of an element to gray-500 using the text-gray-500 utilities.

  21. Control the text color of an element to gray-400 in dark theme using the dark:text-gray-400 utilities.

  22. Use the top-3 utilities to set the top position of a positioned element to 0.75rem.

  23. Control the stack order (or three-dimensional positioning) of an element to -10 in Tailwind, regardless of order it has been displayed, using the -z-10 utilities.

  24. Use the peer-focus:left-0 utilities to set the left position of a positioned element to 0remundefined.

  25. Control the text color of an element to blue-600undefined using the peer-focus:text-blue-600 utilities.

  26. Use grid to create a grid container.

  27. Use grid to create a grid container at only extremely large screen sizes.

  28. To specify the width between columns at only extremely large screen sizes, you can use the xl:gap-6 utilities.

  29. Control the text color of an element to white using the text-white utilities.

  30. Control the background color of an element to blue-700 using the bg-blue-700 utilities.

  31. Control the background color of an element to blue-800 using the hover:bg-blue-800 utilities on hover.

  32. The w-auto utility can be useful if you need to remove an element’s assigned width under a specific condition, like at a particular breakpoint.

  33. Control the horizontal padding of an element to 1.25rem using the px-5 utilities.

  34. Control the text color of an element to center using the text-center utilities.

  35. Control the background color of an element to blue-600 using the dark:bg-blue-600 utilities in dark theme.

  36. Control the margin on top side of an element to 1.25rem using the mt-5 utilities.

  37. Control the text color of an element to blue-600 using the text-blue-600 utilities.

Conclusion

The above is a step-by-step tutorial on how to use Tailwind CSS to make a Floating labels components, learn and follow along to implement your own components.