- Published on
A Complete Guide To Make A Form With Tailwind CSS

- What is Tailwind CSS?
- The description of Form ui component
- Why use Tailwind CSS to build a Form ui component?
- The preview of Form ui component
- The source code of Form ui component
- How to build a Form with Tailwind CSS?
- Install tailwind css of verion 2.2.4
- All the unility class needed to build a Form component
- 50 steps to build a Form 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 Form ui component
Form responsive
Why use Tailwind CSS to build a Form ui component?
- It can make the building process of Form ui component faster and more easily.
- Enables building complex responsive layouts and components freely.
- Minimum lines of CSS code in Form component file.
The preview of Form ui component
Free download of the Form's source code
The source code of Form ui component
<section class="max-w-4xl p-6 mx-auto bg-indigo-600 rounded-md shadow-md dark:bg-gray-800 mt-20">
<h1 class="text-xl font-bold text-white capitalize dark:text-white">Account settings</h1>
<form>
<div class="grid grid-cols-1 gap-6 mt-4 sm:grid-cols-2">
<div>
<label class="text-white dark:text-gray-200" for="username">Username</label>
<input id="username" type="text" class="block w-full px-4 py-2 mt-2 text-gray-700 bg-white border border-gray-300 rounded-md dark:bg-gray-800 dark:text-gray-300 dark:border-gray-600 focus:border-blue-500 dark:focus:border-blue-500 focus:outline-none focus:ring">
</div>
<div>
<label class="text-white dark:text-gray-200" for="emailAddress">Email Address</label>
<input id="emailAddress" type="email" class="block w-full px-4 py-2 mt-2 text-gray-700 bg-white border border-gray-300 rounded-md dark:bg-gray-800 dark:text-gray-300 dark:border-gray-600 focus:border-blue-500 dark:focus:border-blue-500 focus:outline-none focus:ring">
</div>
<div>
<label class="text-white dark:text-gray-200" for="password">Password</label>
<input id="password" type="password" class="block w-full px-4 py-2 mt-2 text-gray-700 bg-white border border-gray-300 rounded-md dark:bg-gray-800 dark:text-gray-300 dark:border-gray-600 focus:border-blue-500 dark:focus:border-blue-500 focus:outline-none focus:ring">
</div>
<div>
<label class="text-white dark:text-gray-200" for="passwordConfirmation">Password Confirmation</label>
<input id="passwordConfirmation" type="password" class="block w-full px-4 py-2 mt-2 text-gray-700 bg-white border border-gray-300 rounded-md dark:bg-gray-800 dark:text-gray-300 dark:border-gray-600 focus:border-blue-500 dark:focus:border-blue-500 focus:outline-none focus:ring">
</div>
<div>
<label class="text-white dark:text-gray-200" for="passwordConfirmation">Color</label>
<input id="color" type="color" class="block w-full px-4 py-2 mt-2 text-gray-700 bg-white border border-gray-300 rounded-md dark:bg-gray-800 dark:text-gray-300 dark:border-gray-600 focus:border-blue-500 dark:focus:border-blue-500 focus:outline-none focus:ring">
</div>
<div>
<label class="text-white dark:text-gray-200" for="passwordConfirmation">Select</label>
<select class="block w-full px-4 py-2 mt-2 text-gray-700 bg-white border border-gray-300 rounded-md dark:bg-gray-800 dark:text-gray-300 dark:border-gray-600 focus:border-blue-500 dark:focus:border-blue-500 focus:outline-none focus:ring">
<option>Surabaya</option>
<option>Jakarta</option>
<option>Tangerang</option>
<option>Bandung</option>
</select>
</div>
<div>
<label class="text-white dark:text-gray-200" for="passwordConfirmation">Range</label>
<input id="range" type="range" class="block w-full py-2 mt-2 text-gray-700 bg-white border border-gray-300 rounded-md dark:bg-gray-800 dark:text-gray-300 dark:border-gray-600 focus:border-blue-500 dark:focus:border-blue-500 focus:outline-none focus:ring">
</div>
<div>
<label class="text-white dark:text-gray-200" for="passwordConfirmation">Date</label>
<input id="date" type="date" class="block w-full px-4 py-2 mt-2 text-gray-700 bg-white border border-gray-300 rounded-md dark:bg-gray-800 dark:text-gray-300 dark:border-gray-600 focus:border-blue-500 dark:focus:border-blue-500 focus:outline-none focus:ring">
</div>
<div>
<label class="text-white dark:text-gray-200" for="passwordConfirmation">Text Area</label>
<textarea id="textarea" type="textarea" class="block w-full px-4 py-2 mt-2 text-gray-700 bg-white border border-gray-300 rounded-md dark:bg-gray-800 dark:text-gray-300 dark:border-gray-600 focus:border-blue-500 dark:focus:border-blue-500 focus:outline-none focus:ring"></textarea>
</div>
<div>
<label class="block text-sm font-medium text-white">
Image
</label>
<div class="mt-1 flex justify-center px-6 pt-5 pb-6 border-2 border-gray-300 border-dashed rounded-md">
<div class="space-y-1 text-center">
<svg class="mx-auto h-12 w-12 text-white" stroke="currentColor" fill="none" viewBox="0 0 48 48" aria-hidden="true">
<path d="M28 8H12a4 4 0 00-4 4v20m32-12v8m0 0v8a4 4 0 01-4 4H12a4 4 0 01-4-4v-4m32-4l-3.172-3.172a4 4 0 00-5.656 0L28 28M8 32l9.172-9.172a4 4 0 015.656 0L28 28m0 0l4 4m4-24h8m-4-4v8m-12 4h.02" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" />
</svg>
<div class="flex text-sm text-gray-600">
<label for="file-upload" class="relative cursor-pointer bg-white rounded-md font-medium text-indigo-600 hover:text-indigo-500 focus-within:outline-none focus-within:ring-2 focus-within:ring-offset-2 focus-within:ring-indigo-500">
<span class="">Upload a file</span>
<input id="file-upload" name="file-upload" type="file" class="sr-only">
</label>
<p class="pl-1 text-white">or drag and drop</p>
</div>
<p class="text-xs text-white">
PNG, JPG, GIF up to 10MB
</p>
</div>
</div>
</div>
</div>
<div class="flex justify-end mt-6">
<button class="px-6 py-2 leading-5 text-white transition-colors duration-200 transform bg-pink-500 rounded-md hover:bg-pink-700 focus:outline-none focus:bg-gray-600">Save</button>
</div>
</form>
</section>
<section class="max-w-4xl p-6 mx-auto bg-white rounded-md shadow-md dark:bg-gray-800 mt-20">
<h2 class="text-lg font-semibold text-gray-700 capitalize dark:text-white">Account settings</h2>
<form>
<div class="grid grid-cols-1 gap-6 mt-4 sm:grid-cols-2">
<div>
<label class="text-gray-700 dark:text-gray-200" for="username">Username</label>
<input id="username" type="text" class="block w-full px-4 py-2 mt-2 text-gray-700 bg-white border border-gray-300 rounded-md dark:bg-gray-800 dark:text-gray-300 dark:border-gray-600 focus:border-blue-500 dark:focus:border-blue-500 focus:outline-none focus:ring">
</div>
<div>
<label class="text-gray-700 dark:text-gray-200" for="emailAddress">Email Address</label>
<input id="emailAddress" type="email" class="block w-full px-4 py-2 mt-2 text-gray-700 bg-white border border-gray-300 rounded-md dark:bg-gray-800 dark:text-gray-300 dark:border-gray-600 focus:border-blue-500 dark:focus:border-blue-500 focus:outline-none focus:ring">
</div>
<div>
<label class="text-gray-700 dark:text-gray-200" for="password">Password</label>
<input id="password" type="password" class="block w-full px-4 py-2 mt-2 text-gray-700 bg-white border border-gray-300 rounded-md dark:bg-gray-800 dark:text-gray-300 dark:border-gray-600 focus:border-blue-500 dark:focus:border-blue-500 focus:outline-none focus:ring">
</div>
<div>
<label class="text-gray-700 dark:text-gray-200" for="passwordConfirmation">Password Confirmation</label>
<input id="passwordConfirmation" type="password" class="block w-full px-4 py-2 mt-2 text-gray-700 bg-white border border-gray-300 rounded-md dark:bg-gray-800 dark:text-gray-300 dark:border-gray-600 focus:border-blue-500 dark:focus:border-blue-500 focus:outline-none focus:ring">
</div>
</div>
<div class="flex justify-end mt-6">
<button class="px-6 py-2 leading-5 text-white transition-colors duration-200 transform bg-gray-700 rounded-md hover:bg-gray-600 focus:outline-none focus:bg-gray-600">Save</button>
</div>
</form>
</section>
How to build a Form with Tailwind CSS?
Install tailwind css of verion 2.2.4
Use the script
html tag to import the script of Tailwind CSS of the version 2.2.4
<script src="https://cdn.tailwindcss.com"></script>
All the unility class needed to build a Form component
max-w-4xl
p-6
mx-auto
bg-indigo-600
dark:bg-gray-800
mt-20
text-xl
text-white
dark:text-white
grid
grid-cols-1
gap-6
mt-4
sm:grid-cols-2
dark:text-gray-200
block
w-full
px-4
py-2
mt-2
text-gray-700
bg-white
border-gray-300
dark:text-gray-300
dark:border-gray-600
focus:border-blue-500
text-sm
mt-1
flex
px-6
pt-5
pb-6
border-2
border-dashed
text-center
h-12
w-12
text-gray-600
relative
text-indigo-600
hover:text-indigo-500
pl-1
text-xs
mt-6
bg-pink-500
hover:bg-pink-700
focus:bg-gray-600
text-lg
bg-gray-700
hover:bg-gray-600
50 steps to build a Form component with Tailwind CSS
Set the maximum width/height of an element using the
max-w-4xl
utilities.Control the padding on all sides of an element to 1.5rem using the
p-6
utilities.Control the horizontal margin of an element to auto using the
mx-auto
utilities.Control the background color of an element to indigo-600 using the
bg-indigo-600
utilities.Control the background color of an element to gray-800 using the
dark:bg-gray-800
utilities in dark theme.Control the margin on top side of an element to 5rem using the
mt-20
utilities.Control the text color of an element to xl using the
text-xl
utilities.Control the text color of an element to white using the
text-white
utilities.Control the text color of an element to white in dark theme using the
dark:text-white
utilities.Use
grid
to create a grid container.Use
grid
to create a grid container.To specify the width between columns, you can use the
gap-6
utilities.Control the margin on top side of an element to 1rem using the
mt-4
utilities.Use
grid
to create a grid container at only small screen sizes.Control the text color of an element to gray-200 in dark theme using the
dark:text-gray-200
utilities.Use
inline
utilities to put the element on its own line and fill its parent.Use
w-full
to set an element to a 100% based width.Control the horizontal padding of an element to 1rem using the
px-4
utilities.Control the vertical padding of an element to 0.5rem using the
py-2
utilities.Control the margin on top side of an element to 0.5rem using the
mt-2
utilities.Control the text color of an element to gray-700 using the
text-gray-700
utilities.Control the background color of an element to white using the
bg-white
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 gray-300 in dark theme using the
dark:text-gray-300
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-500 using the
focus:border-blue-500
utilities on focus.Control the text color of an element to sm using the
text-sm
utilities.Control the margin on top side of an element to 0.25rem using the
mt-1
utilities.Use
flex
to create a block-level flex container.Control the horizontal padding of an element to 1.5rem using the
px-6
utilities.Control the padding on top side of an element to 1.25rem using the
pt-5
utilities.Control the padding on bottom side of an element to 1.5rem using the
pb-6
utilities.Control the border color of an element to 0.5rem using the
border-2
utilities.Control the border color of an element to dashed using the
border-dashed
utilities.Control the text color of an element to center using the
text-center
utilities.Use
h-12
to set an element to a fixed height(3rem).Use
w-12
to set an element to a fixed width(3rem).Control the text color of an element to gray-600 using the
text-gray-600
utilities.Use
relative
to position an element according to the normal flow of the document.Control the text color of an element to indigo-600 using the
text-indigo-600
utilities.Control the text color of an element to indigo-500 on hover using the
hover:text-indigo-500
utilities.Adjust the left padding of an element to 0.25rem using the
pl-1
utilities classControl the text color of an element to xs using the
text-xs
utilities.Control the margin on top side of an element to 1.5rem using the
mt-6
utilities.Control the background color of an element to pink-500 using the
bg-pink-500
utilities.Control the background color of an element to pink-700 using the
hover:bg-pink-700
utilities on hover.Control the background color of an element to gray-600 using the
focus:bg-gray-600
utilities on focus.Control the text color of an element to lg using the
text-lg
utilities.Control the background color of an element to gray-700 using the
bg-gray-700
utilities.Control the background color of an element to gray-600 using the
hover:bg-gray-600
utilities on hover.
Conclusion
The above is a step-by-step tutorial on how to use Tailwind CSS to build a Form components, learn and follow along to implement your own components.