Published on

Best Ways To Build 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 make 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

<div class="container items-center px-5 py-12 lg:px-20">
  <form class="flex flex-col w-full p-10 px-8 pt-6 mx-auto my-6 mb-4 transition duration-500 ease-in-out transform bg-white border rounded-lg lg:w-1/2 ">
    <section class="flex flex-col w-full h-full p-1 overflow-auto">
      <label for="name" class="text-base leading-7 text-blueGray-500 mb-5">Input Image</label>
      <header class="flex flex-col items-center justify-center py-12 text-base text-blueGray-500 transition duration-500 ease-in-out transform bg-white border border-dashed rounded-lg focus:border-blue-500 focus:outline-none focus:shadow-outline focus:ring-2 ring-offset-current ring-offset-2">
        <p class="flex flex-wrap justify-center mb-3 text-base leading-7 text-blueGray-500">
          <span>Drag and drop your</span> <span>files anywhere or</span>
        </p>
        <button class="w-auto px-2 py-1 my-2 mr-2 text-blueGray-500 transition duration-500 ease-in-out transform border rounded-md hover:text-blueGray-600 text-md focus:shadow-outline focus:outline-none focus:ring-2 ring-offset-current ring-offset-2 hover:bg-gray-100"> Upload a file </button>
      </header>
    </section>
    <div class="relative pt-4">
      <label for="name" class="text-base leading-7 text-blueGray-500">Input Text</label>
      <input type="text" id="name" name="name" placeholder="name" class="w-full px-4 py-2 mt-2 mr-4 text-base text-black transition duration-500 ease-in-out transform rounded-lg bg-gray-100 focus:border-blueGray-500 focus:bg-white focus:outline-none focus:shadow-outline focus:ring-2 ring-offset-current ring-offset-2">
    </div>
    <div class="relative pt-4">
      <label for="name" class="text-base leading-7 text-blueGray-500">Input Text</label>
      <input type="number" id="number" name="number" placeholder="number" class="w-full px-4 py-2 mt-2 mr-4 text-base text-black transition duration-500 ease-in-out transform rounded-lg bg-gray-100 focus:border-blueGray-500 focus:bg-white focus:outline-none focus:shadow-outline focus:ring-2 ring-offset-current ring-offset-2">
    </div>
    <div class="relative pt-4">
      <label for="name" class="text-base leading-7 text-blueGray-500">Input Date</label>
      <input type="date" id="date" name="date" placeholder="name" class="w-full px-4 py-2 mt-2 mr-4 text-base text-black transition duration-500 ease-in-out transform rounded-lg bg-gray-100 focus:border-blueGray-500 focus:bg-white focus:outline-none focus:shadow-outline focus:ring-2 ring-offset-current ring-offset-2">
    </div>
    <div class="relative pt-4">
      <label for="name" class="text-base leading-7 text-blueGray-500">Input Color</label>
      <input type="color" id="color" name="color" placeholder="name" class="w-full px-4 py-2 mt-2 mr-4 text-base text-black transition duration-500 ease-in-out transform rounded-lg bg-gray-100 focus:border-blueGray-500 focus:bg-white focus:outline-none focus:shadow-outline focus:ring-2 ring-offset-current ring-offset-2">
    </div>
    <div class="relative pt-4">
      <label for="name" class="text-base leading-7 text-blueGray-500">Input Range</label>
      <input type="range" id="range" name="range" placeholder="name" class="w-full px-0 py-2 mt-2 mr-4 text-base text-black transition duration-500 ease-in-out transform rounded-lg bg-gray-100 focus:border-blueGray-500 focus:bg-white focus:outline-none focus:shadow-outline focus:ring-2 ring-offset-current ring-offset-2">
    </div>
    <div class="relative mt-4">
      <label for="name" class="text-base leading-7 text-blueGray-500">Component Select</label>
      <select class="w-full px-4 py-2 mt-2 text-base text-black transition duration-500 ease-in-out transform rounded-lg bg-gray-100 focus:border-blueGray-500 focus:bg-white focus:outline-none focus:shadow-outline focus:ring-2 ring-offset-current ring-offset-2">
        <option>Surabaya</option>
        <option>Bandung</option>
        <option>jakarta</option>
      </select>
    </div>
    <div class="flex flex-wrap mt-4 mb-6 -mx-3">
      <div class="w-full px-3">
        <label class="text-base leading-7 text-blueGray-500" for="description">Text Area </label>
        <textarea class="w-full h-32 px-4 py-2 mt-2 text-base text-blueGray-500 transition duration-500 ease-in-out transform bg-white border rounded-lg focus:border-blue-500 focus:outline-none focus:shadow-outline focus:ring-2 ring-offset-current ring-offset-2 apearance-none autoexpand" id="description" type="text" name="description" placeholder="Message..." required=""></textarea>
      </div>
    </div>
    <div class="flex">
      <label class="flex items-center">
        <input type="checkbox" class="form-checkbox ">
        <span class="ml-2 text-blueGray-500">checkbox </span>
      </label>
    </div>
    <div class="flex items-center w-full pt-4 mb-4">
      <button class="w-full py-3 text-base text-white transition duration-500 ease-in-out transform bg-blue-600 border-blue-600 rounded-md focus:shadow-outline focus:outline-none focus:ring-2 ring-offset-current ring-offset-2 hover:bg-blue-800 "> Button </button>
    </div>
    <hr class="my-4 border-gray-200">
    <span class="px-4 py-1 mx-auto -mt-8 text-xs text-black transition duration-500 ease-in-out transform bg-gray-200 rounded-lg">Or continue with </span>
    <div class="inline-flex items-center justify-between w-full pt-8 ">
      <button class="w-auto px-8 py-2 mr-2 text-base text-black transition duration-500 ease-in-out transform rounded-lg bg-gray-100 hover:bg-blueGray-200 focus:shadow-outline focus:outline-none focus:ring-2 ring-offset-current ring-offset-2 focus:border-blueGray-700 focus:bg-blueGray-800 ">
        <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-brand-github" width="20" height="20" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
          <path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
          <path d="M9 19c-4.3 1.4 -4.3 -2.5 -6 -3m12 5v-3.5c0 -1 .1 -1.4 -.5 -2c2.8 -.3 5.5 -1.4 5.5 -6a4.6 4.6 0 0 0 -1.3 -3.2a4.2 4.2 0 0 0 -.1 -3.2s-1.1 -.3 -3.5 1.3a12.3 12.3 0 0 0 -6.2 0c-2.4 -1.6 -3.5 -1.3 -3.5 -1.3a4.2 4.2 0 0 0 -.1 3.2a4.6 4.6 0 0 0 -1.3 3.2c0 4.6 2.7 5.7 5.5 6c-.6 .6 -.6 1.2 -.5 2v3.5"></path>
        </svg>
      </button>
      <button class="w-auto px-8 py-2 mr-2 text-base text-black transition duration-500 ease-in-out transform rounded-lg bg-gray-100 hover:bg-blueGray-200 focus:shadow-outline focus:outline-none focus:ring-2 ring-offset-current ring-offset-2 focus:border-blueGray-700 focus:bg-blueGray-800 ">
        <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-brand-gitlab" width="20" height="20" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
          <path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
          <path d="M21 14l-9 7l-9 -7l3 -11l3 7h6l3 -7z"></path>
        </svg>
      </button>
      <button class="w-auto px-8 py-2 mr-2 text-base text-black transition duration-500 ease-in-out transform rounded-lg bg-gray-100 hover:bg-blueGray-200 focus:shadow-outline focus:outline-none focus:ring-2 ring-offset-current ring-offset-2 focus:border-blueGray-700 focus:bg-blueGray-800 ">
        <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-brand-twitter" width="20" height="20" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
          <path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
          <path d="M22 4.01c-1 .49 -1.98 .689 -3 .99c-1.121 -1.265 -2.783 -1.335 -4.38 -.737s-2.643 2.06 -2.62 3.737v1c-3.245 .083 -6.135 -1.395 -8 -4c0 0 -4.182 7.433 4 11c-1.872 1.247 -3.739 2.088 -6 2c3.308 1.803 6.913 2.423 10.034 1.517c3.58 -1.04 6.522 -3.723 7.651 -7.742a13.84 13.84 0 0 0 .497 -3.753c-.002 -.249 1.51 -2.772 1.818 -4.013z"></path>
        </svg>
      </button>
    </div>
  </form>
</div>

How to make 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 make a form component

  • px-5
  • py-12
  • lg:px-20
  • flex
  • flex-col
  • w-full
  • p-10
  • px-8
  • pt-6
  • mx-auto
  • my-6
  • mb-4
  • bg-white
  • lg:w-1/2
  • h-full
  • p-1
  • overflow-auto
  • text-base
  • text-blueGray-500
  • mb-5
  • border-dashed
  • focus:border-blue-500
  • flex-wrap
  • mb-3
  • w-auto
  • px-2
  • py-1
  • my-2
  • mr-2
  • hover:text-blueGray-600
  • text-md
  • hover:bg-gray-100
  • relative
  • pt-4
  • px-4
  • py-2
  • mt-2
  • mr-4
  • text-black
  • bg-gray-100
  • focus:border-blueGray-500
  • focus:bg-white
  • px-0
  • mt-4
  • mb-6
  • -mx-3
  • px-3
  • h-32
  • ml-2
  • py-3
  • text-white
  • bg-blue-600
  • border-blue-600
  • hover:bg-blue-800
  • my-4
  • border-gray-200
  • -mt-8
  • text-xs
  • bg-gray-200
  • inline-flex
  • pt-8
  • hover:bg-blueGray-200
  • focus:border-blueGray-700
  • focus:bg-blueGray-800

64 steps to make a form component with Tailwind CSS

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

  2. Control the vertical padding of an element to 3rem using the py-12 utilities.

  3. Control the horizontal padding of an element to 5rem at only large screen sizes using the lg:px-20 utilities.

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

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

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

  7. Control the padding on all sides of an element to 2.5rem using the p-10 utilities.

  8. Control the horizontal padding of an element to 2rem using the px-8 utilities.

  9. Control the padding on top side of an element to 1.5rem using the pt-6 utilities.

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

  11. Control the vertical margin of an element to 1.5rem using the my-6 utilities.

  12. Control the margin on bottom side of an element to 1rem using the mb-4 utilities.

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

  14. Use lg:w-1/2 to set an element to a fixed width(1/2) at only large screen sizes.

  15. Use h-full to set an element’s height to 100% of its parent, as long as the parent has a defined height.

  16. Control the padding on all sides of an element to 0.25rem using the p-1 utilities.

  17. Use overflow-auto to add scrollbars to an element in the event that its content overflows the bounds of that element. Unlike .overflow-scroll, which always shows scrollbars, this utility will only show them if scrolling is necessary.

  18. Control the text color of an element to base using the text-base utilities.

  19. Control the text color of an element to blueGray-500 using the text-blueGray-500 utilities.

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

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

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

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

  24. Control the margin on bottom side of an element to 0.75rem using the mb-3 utilities.

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

  26. Control the horizontal padding of an element to 0.5rem using the px-2 utilities.

  27. Control the vertical padding of an element to 0.25rem using the py-1 utilities.

  28. Control the vertical margin of an element to 0.5rem using the my-2 utilities.

  29. Control the margin on right side of an element to 0.5rem using the mr-2 utilities.

  30. Control the text color of an element to blueGray-600 on hover using the hover:text-blueGray-600 utilities.

  31. Control the text color of an element to md using the text-md utilities.

  32. Control the background color of an element to gray-100 using the hover:bg-gray-100 utilities on hover.

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

  34. Control the padding on top side of an element to 1rem using the pt-4 utilities.

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

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

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

  38. Control the margin on right side of an element to 1rem using the mr-4 utilities.

  39. Control the text color of an element to black using the text-black utilities.

  40. Control the background color of an element to gray-100 using the bg-gray-100 utilities.

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

  42. Control the background color of an element to white using the focus:bg-white utilities on focus.

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

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

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

  46. Control the horizontal margin of an element to -0.75rem using the -mx-3 utilities.

  47. Control the horizontal padding of an element to 0.75rem using the px-3 utilities.

  48. Use h-32 to set an element to a fixed height(8rem).

  49. Control the margin on left side of an element to 0.5rem using the ml-2 utilities.

  50. Control the vertical padding of an element to 0.75rem using the py-3 utilities.

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

  52. Control the background color of an element to blue-600 using the bg-blue-600 utilities.

  53. Control the border color of an element to blue-600 using the border-blue-600 utilities.

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

  55. Control the vertical margin of an element to 1rem using the my-4 utilities.

  56. Control the border color of an element to gray-200 using the border-gray-200 utilities.

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

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

  59. Control the background color of an element to gray-200 using the bg-gray-200 utilities.

  60. Use inline-flex to create an inline flex container that flows with text.

  61. Control the padding on top side of an element to 2rem using the pt-8 utilities.

  62. Control the background color of an element to blueGray-200 using the hover:bg-blueGray-200 utilities on hover.

  63. Control the border color of an element to blueGray-700 using the focus:border-blueGray-700 utilities on focus.

  64. Control the background color of an element to blueGray-800 using the focus:bg-blueGray-800 utilities on focus.

Conclusion

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