Introduction#
This demo shows how to use form with tailwind
- form, label, input
- form action, submit, button
- upload form HERE
Form#
Let create a form with tailwind
<div class="dark:bg-slate-800 min-h-screen"><div class="mx-auto max-w-4xl px-10"><form id="form"><div class="grid gap-6 mb-6 md:grid-cols-2"><div><labelfor="first_name"class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">First name</label><inputtype="text"id="first_name"class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"placeholder="John"/></div><div><labelfor="last_name"class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Last name</label><inputtype="text"id="last_name"class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"placeholder="Doe"/></div><div><labelfor="company"class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Company</label><inputtype="text"id="company"class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"placeholder="Flowbite"/></div><div><labelfor="phone"class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Phone number</label><inputtype="tel"id="phone"class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"placeholder="123-45-678"pattern="[0-9]{3}-[0-9]{2}-[0-9]{3}"/></div><div><labelfor="website"class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Website URL</label><inputtype="url"id="website"class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"placeholder="flowbite.com"/></div><div><labelfor="visitors"class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Unique visitors (per month)</label><inputtype="number"id="visitors"class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"placeholder=""/></div></div><div class="mb-6"><labelfor="email"class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Email address</label><inputtype="email"id="email"class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"placeholder="john.doe@company.com"/></div><div class="mb-6"><labelfor="password"class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Password</label><inputtype="password"id="password"class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"placeholder="•••••••••"/></div><div class="mb-6"><labelfor="confirm_password"class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Confirm password</label><inputtype="password"id="confirm_password"class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"placeholder="•••••••••"--/>/></div><div class="flex items-start mb-6"><div class="flex items-center h-5"><inputid="remember"type="checkbox"value=""class="w-4 h-4 border border-gray-300 rounded bg-gray-50 focus:ring-3 focus:ring-blue-300 dark:bg-gray-700 dark:border-gray-600 dark:focus:ring-blue-600 dark:ring-offset-gray-800"/></div><labelfor="remember"class="ml-2 text-sm font-medium text-gray-900 dark:text-gray-300">I agree with the<a href="#" class="text-blue-600 hover:underline dark:text-blue-500">terms and conditions</a>.</label></div><buttontype="submit"class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none 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"id="submit_button">Submit</button></form></div></div><script></script>
Submit#
There are different ways to handle form action and submit
- action can be endpoint and form send GET or POST request to
- onsubmit which will be called when submit the form
- button disable default behavior and handle by button click
script>let submitButton = document.getElementById("submit_button");let form = document.getElementById("form");// submitButton.addEventListener("click", (event) => {// event.preventDefault();// console.log("handle click button");// });form.addEventListener("submit", (event) => {event.preventDefault();console.log("handle form submit");});</script>
File Input#
Let custom a file input for uploading files
<html><head><style>input[type='file'] {width: 350px;max-width: 100%;color: #444;padding: 5px;background: #fff;border-radius: 10px;border: 1px solid #555;}input[type='file']::file-selector-button {margin-right: 20px;border: none;background: #084cdf;padding: 10px 20px;border-radius: 10px;color: #fff;cursor: pointer;transition: background 0.2s ease-in-out;}input[type='file']::file-selector-button:hover {background: #0d45a5;}.drop-container {position: relative;display: flex;gap: 10px;flex-direction: column;justify-content: center;align-items: center;height: 200px;padding: 20px;border-radius: 10px;border: 2px dashed #555;color: #444;cursor: pointer;transition: background 0.2s ease-in-out, border 0.2s ease-in-out;}.drop-container:hover {background: #eee;border-color: #111;}.drop-container:hover .drop-title {color: #222;}.drop-title {color: #444;font-size: 20px;font-weight: bold;text-align: center;transition: color 0.2s ease-in-out;}</style></head><body><div><label for="images" class="drop-container" id="dropcontainer"><span class="drop-title">Drop files here</span>or<input type="file" id="images" accept="image/*" required /></label></div></body><script>fileInput.files = e.dataTransfer.filesconst dropContainer = document.getElementById('dropcontainer')const fileInput = document.getElementById('images')dropContainer.addEventListener('dragover',e => {// prevent default to allow drope.preventDefault()},false)dropContainer.addEventListener('dragenter', () => {dropContainer.classList.add('drag-active')})dropContainer.addEventListener('dragleave', () => {dropContainer.classList.remove('drag-active')})dropContainer.addEventListener('drop', e => {e.preventDefault()dropContainer.classList.remove('drag-active')fileInput.files = e.dataTransfer.files})</script></html>