Published on

A Complete Guide To Make A Form With Tailwind CSS

Form

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

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

  2. Control the padding on all sides of an element to 1.5rem using the p-6 utilities.

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

  4. Control the background color of an element to indigo-600 using the bg-indigo-600 utilities.

  5. Control the background color of an element to gray-800 using the dark:bg-gray-800 utilities in dark theme.

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

  7. Control the text color of an element to xl using the text-xl utilities.

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

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

  10. Use grid to create a grid container.

  11. Use grid to create a grid container.

  12. To specify the width between columns, you can use the gap-6 utilities.

  13. Control the margin on top side of an element to 1rem using the mt-4 utilities.

  14. Use grid to create a grid container at only small screen sizes.

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

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

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

  18. Control the horizontal padding of an element to 1rem using the px-4 utilities.

  19. Control the vertical padding of an element to 0.5rem using the py-2 utilities.

  20. Control the margin on top side of an element to 0.5rem using the mt-2 utilities.

  21. Control the text color of an element to gray-700 using the text-gray-700 utilities.

  22. Control the background color of an element to white using the bg-white utilities.

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

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

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

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

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

  28. Control the margin on top side of an element to 0.25rem using the mt-1 utilities.

  29. Use flex to create a block-level flex container.

  30. Control the horizontal padding of an element to 1.5rem using the px-6 utilities.

  31. Control the padding on top side of an element to 1.25rem using the pt-5 utilities.

  32. Control the padding on bottom side of an element to 1.5rem using the pb-6 utilities.

  33. Control the border color of an element to 0.5rem using the border-2 utilities.

  34. Control the border color of an element to dashed using the border-dashed utilities.

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

  36. Use h-12 to set an element to a fixed height(3rem).

  37. Use w-12 to set an element to a fixed width(3rem).

  38. Control the text color of an element to gray-600 using the text-gray-600 utilities.

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

  40. Control the text color of an element to indigo-600 using the text-indigo-600 utilities.

  41. Control the text color of an element to indigo-500 on hover using the hover:text-indigo-500 utilities.

  42. Adjust the left padding of an element to 0.25rem using the pl-1 utilities class

  43. Control the text color of an element to xs using the text-xs utilities.

  44. Control the margin on top side of an element to 1.5rem using the mt-6 utilities.

  45. Control the background color of an element to pink-500 using the bg-pink-500 utilities.

  46. Control the background color of an element to pink-700 using the hover:bg-pink-700 utilities on hover.

  47. Control the background color of an element to gray-600 using the focus:bg-gray-600 utilities on focus.

  48. Control the text color of an element to lg using the text-lg utilities.

  49. Control the background color of an element to gray-700 using the bg-gray-700 utilities.

  50. 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.