agyanadda@gmail.com

Tailwind Forms

Building Components 1 min read Updated Dec 1, 2026 Topic 22 of 27

Tailwind resets form styles, so you style inputs yourself.

A good input

<input class="w-full rounded-lg border border-slate-300 px-3 py-2
  focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500">

Labels and layout

  • Label: block text-sm font-medium mb-1
  • Two columns: grid sm:grid-cols-2 gap-4
  • Help text: text-xs text-slate-500 mt-1

Checkbox and radio colour

accent-indigo-600 colours native checkboxes, radios and ranges.

Error states

  • Server errors: add border-rose-500 and show a text-rose-600 message
  • Live browser check: invalid:border-rose-500 or the peer / peer-invalid: trick
  • user-invalid: shows errors only after the user has typed

Plugin

@tailwindcss/forms gives all form elements a clean default style if you prefer not to start from zero.

Example

Example — Enquiry form HTML
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>

<div class="min-h-screen bg-slate-100 flex items-center justify-center p-6">
  <form class="w-full max-w-lg bg-white rounded-2xl shadow-sm p-6 space-y-4">
    <h2 class="text-xl font-bold">Send an enquiry</h2>
    <div class="grid sm:grid-cols-2 gap-4">
      <div>
        <label for="name" class="block text-sm font-medium mb-1">Name</label>
        <input id="name" required class="w-full rounded-lg border border-slate-300 px-3 py-2 focus:outline-none focus:ring-2 focus:ring-indigo-500">
      </div>
      <div>
        <label for="phone" class="block text-sm font-medium mb-1">Mobile</label>
        <input id="phone" type="tel" pattern="[6-9][0-9]{9}" class="peer w-full rounded-lg border border-slate-300 px-3 py-2 focus:outline-none focus:ring-2 focus:ring-indigo-500 user-invalid:border-rose-500">
        <p class="hidden peer-user-invalid:block text-xs text-rose-600 mt-1">Enter a 10-digit mobile number.</p>
      </div>
    </div>
    <div>
      <label for="course" class="block text-sm font-medium mb-1">Interested in</label>
      <select id="course" class="w-full rounded-lg border border-slate-300 px-3 py-2 bg-white focus:outline-none focus:ring-2 focus:ring-indigo-500">
        <option>Online Training</option>
        <option>Online Exam</option>
        <option>School ERP</option>
      </select>
    </div>
    <div>
      <label for="msg" class="block text-sm font-medium mb-1">Message</label>
      <textarea id="msg" rows="3" class="w-full rounded-lg border border-slate-300 px-3 py-2 focus:outline-none focus:ring-2 focus:ring-indigo-500"></textarea>
    </div>
    <label class="flex items-center gap-2 text-sm"><input type="checkbox" class="size-4 accent-indigo-600" checked> Contact me on WhatsApp</label>
    <button class="w-full bg-indigo-600 hover:bg-indigo-700 text-white font-semibold rounded-lg py-2.5 transition">Send enquiry</button>
  </form>
</div>

Test Yourself With Exercises

Exercise 1 Multiple Choice

Which class changes the colour of a native checkbox?

Exercise 2 True / False

focus:ring-2 shows a ring only when the input is focused.

Exercise 3 Fill in the Blank

The official plugin @tailwindcss/___ gives form elements clean default styles.

Frequently Asked Questions

Use @error in Blade: add border-rose-500 to the input's class when there is an error, and show {{ $message }} in a text-rose-600 paragraph.