- Published on
Most Effective Ways To Build A Floating Labels With Tailwind CSS

- What is Tailwind CSS?
- The description of Floating labels ui component
- Why use Tailwind CSS to make a Floating labels ui component?
- The preview of Floating labels ui component
- The source code of Floating labels ui component
- How to make a Floating labels with Tailwind CSS?
- Install tailwind css of verion 2.2.19
- All the unility class needed to make a Floating labels component
- 37 steps to make a Floating labels component with Tailwind CSS
- Conclusion
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
Set the maximum width/height of an element using the
max-w-2xl
utilities.Control the horizontal margin of an element to auto using the
mx-auto
utilities.Use
relative
to position an element according to the normal flow of the document.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.Control the margin on bottom side of an element to 1.5rem using the
mb-6
utilities.Use
w-full
to set an element to a 100% based width.Use
inline
utilities to put the element on its own line and fill its parent.Control the vertical padding of an element to 2.5 using the
py-2.5
utilities.Control the horizontal padding of an element to 0rem using the
px-0
utilities.Control the text color of an element to sm using the
text-sm
utilities.Control the text color of an element to gray-900 using the
text-gray-900
utilities.Control the background color of an element to transparent using the
bg-transparent
utilities.Control the border color of an element to 0rem using the
border-0
utilities.Control the border color of an element to b-2 using the
border-b-2
utilities.Control the border color of an element to gray-300 using the
border-gray-300
utilities.Control the text color of an element to white in dark theme using the
dark:text-white
utilities.Control the border color of an element to gray-600 using the
dark:border-gray-600
utilities in dark theme.Control the border color of an element to blue-600 using the
focus:border-blue-600
utilities on focus.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.Control the text color of an element to gray-500 using the
text-gray-500
utilities.Control the text color of an element to gray-400 in dark theme using the
dark:text-gray-400
utilities.Use the
top-3
utilities to set the top position of a positioned element to 0.75rem.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.Use the
peer-focus:left-0
utilities to set the left position of a positioned element to 0remundefined.Control the text color of an element to blue-600undefined using the
peer-focus:text-blue-600
utilities.Use
grid
to create a grid container.Use
grid
to create a grid container at only extremely large screen sizes.To specify the width between columns at only extremely large screen sizes, you can use the
xl:gap-6
utilities.Control the text color of an element to white using the
text-white
utilities.Control the background color of an element to blue-700 using the
bg-blue-700
utilities.Control the background color of an element to blue-800 using the
hover:bg-blue-800
utilities on hover.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.Control the horizontal padding of an element to 1.25rem using the
px-5
utilities.Control the text color of an element to center using the
text-center
utilities.Control the background color of an element to blue-600 using the
dark:bg-blue-600
utilities in dark theme.Control the margin on top side of an element to 1.25rem using the
mt-5
utilities.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.