Skip to main content

Archive

Show more

Forms in Tailwind

Forms in Tailwind

Tailwind CSS provides utility classes for building stylish and responsive forms without the need for custom CSS. Here's how you can create forms using Tailwind:


1. Basic Form Structure

Create a basic form with input fields and a submit button:

<form class="bg-white shadow-md rounded px-8 pt-6 pb-8 mb-4">
    <div class="mb-4">
        <label class="block text-gray-700 text-sm font-bold mb-2" for="username">
            Username
        </label>
        <input class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" id="username" type="text" placeholder="Username">
    </div>
    <div class="mb-6">
        <label class="block text-gray-700 text-sm font-bold mb-2" for="password">
            Password
        </label>
        <input class="shadow appearance-none border border-red-500 rounded w-full py-2 px-3 text-gray-700 mb-3 leading-tight focus:outline-none focus:shadow-outline" id="password" type="password" placeholder="******************">
        <p class="text-red-500 text-xs italic">Please choose a password.</p>
    </div>
    <div class="flex items-center justify-between">
        <button class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline" type="button">
            Sign In
        </button>
        <a class="inline-block align-baseline font-bold text-sm text-blue-500 hover:text-blue-800" href="#">
            Forgot Password?
        </a>
    </div>
</form>

2. Form Validation

Add validation styles to form fields based on input states:

<input class="shadow appearance-none border border-red-500 rounded w-full py-2 px-3 text-gray-700 mb-3 leading-tight focus:outline-none focus:shadow-outline" id="password" type="password" placeholder="******************">
<p class="text-red-500 text-xs italic">Please choose a password.</p>

3. Inline Form

Create an inline form with horizontally aligned input fields:

<form class="inline-block">
    <input class="shadow appearance-none border rounded py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" id="username" type="text" placeholder="Username">
    <input class="shadow appearance-none border rounded py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" id="password" type="password" placeholder="Password">
    <button class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline" type="button">
        Sign In
    </button>
</form>

4. Form Components

Use Tailwind's form components for checkboxes, radio buttons, and select inputs:

<label class="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2" for="grid-state">
    State
</label>
<div class="relative">
    <select class="block appearance-none w-full bg-gray-200 border border-gray-200 text-gray-700 py-3 px-4 pr-8 rounded leading-tight focus:outline-none focus:bg-white focus:border-gray-500" id="grid-state">
        <option>New York</option>
        <option>Los Angeles</option>
        <option>Chicago</option>
    </select>
    <div class="pointer-events-none absolute inset-y-0 right-0 flex items-center px-2 text-gray-700">
        <svg class="fill-current h-4 w-4" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20">
            <path d="M10 12a2 2 0 100-4 2 2 0 000 4z"/>
            <path fill-rule="evenodd" d="M19.707 9.293a1 1 0 00-1.414-1.414L10 16.586l-8.293-8.293a1 1 0 00-1.414 1.414l9 9a1 1 0 001.414 0l9-9z" clip-rule="evenodd"/>
        </svg>
    </div>
</div>

5. Form Grid

Use Tailwind's grid system to create complex form layouts:

<div class="grid grid-cols-2 gap-4">
    <div class="mb-4">
        <label class="block text-gray-700 text-sm font-bold mb-2" for="username">
            Username
        </label>
        <input class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" id="username" type="text" placeholder="Username">
    </div>
    <div class="mb-4">
        <label class="block text-gray-700 text-sm font-bold mb-2" for="password">
            Password
        </label>
        <input class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" id="password" type="password" placeholder="******************">
    </div>
    <div class="col-span-2">
        <button class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline" type="button">
            Sign In
        </button>
    </div>
</div>

6. Form Validation

Apply validation styles to form fields based on input states:

<input class="shadow appearance-none border border-red-500 rounded w-full py-2 px-3 text-gray-700 mb-3 leading-tight focus:outline-none focus:shadow-outline" id="password" type="password" placeholder="******************">
<p class="text-red-500 text-xs italic">Please choose a password.</p>

7. Form Submission

Handle form submission using JavaScript or server-side processing:

<form>
    <!-- Form fields -->
</form>

<script>
    // JavaScript form submission handling
    document.querySelector('form').addEventListener('submit', function(event) {
        // Prevent default form submission
        event.preventDefault();
        // Perform form validation
        // Submit form data via AJAX or redirect to a different page
    });
</script>

8. Utility Classes Reference

Refer to the Tailwind CSS documentation for a complete list of form utility classes and their corresponding values: https://tailwindcss.com/docs/forms


Conclusion

Tailwind CSS simplifies the process of creating forms by providing utility classes for styling, layout, and validation. By leveraging Tailwind's extensive set of utility classes, developers can quickly design responsive and visually appealing forms that meet the requirements of their web applications.

Comments