乐闻世界logo
搜索文章和话题

How to use TailwindCSS Forms plugin?

2月17日 22:57

TailwindCSS provides a dedicated Forms plugin (@tailwindcss/forms) that offers beautiful and consistent styles for form elements, greatly simplifying form development work.

Installation and Configuration

1. Install Plugin

bash
# Using npm npm install -D @tailwindcss/forms # Using yarn yarn add -D @tailwindcss/forms # Using pnpm pnpm add -D @tailwindcss/forms

2. Configure Plugin

javascript
// tailwind.config.js module.exports = { plugins: [ require('@tailwindcss/forms'), ], }

Basic Form Styles

1. Input Fields

html
<!-- Text input --> <input type="text" placeholder="Enter username" class=" w-full px-4 py-2 border border-gray-300 rounded focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent "> <!-- Email input --> <input type="email" placeholder="Enter email" class=" w-full px-4 py-2 border border-gray-300 rounded focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent "> <!-- Password input --> <input type="password" placeholder="Enter password" class=" w-full px-4 py-2 border border-gray-300 rounded focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent "> <!-- Number input --> <input type="number" placeholder="Enter number" class=" w-full px-4 py-2 border border-gray-300 rounded focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent ">

2. Textarea

html
<!-- Textarea --> <textarea placeholder="Enter content" rows="4" class=" w-full px-4 py-2 border border-gray-300 rounded focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent resize-none " ></textarea>

3. Select Boxes

html
<!-- Single select dropdown --> <select class=" w-full px-4 py-2 border border-gray-300 rounded focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent "> <option value="">Select an option</option> <option value="1">Option 1</option> <option value="2">Option 2</option> <option value="3">Option 3</option> </select> <!-- Multiple select dropdown --> <select multiple class=" w-full px-4 py-2 border border-gray-300 rounded focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent "> <option value="1">Option 1</option> <option value="2">Option 2</option> <option value="3">Option 3</option> </select>

4. Checkboxes and Radio Buttons

html
<!-- Checkbox --> <label class="flex items-center space-x-2"> <input type="checkbox" class=" w-4 h-4 text-blue-600 border-gray-300 rounded focus:ring-blue-500 "> <span>Agree to terms</span> </label> <!-- Radio buttons --> <div class="space-y-2"> <label class="flex items-center space-x-2"> <input type="radio" name="gender" value="male" class=" w-4 h-4 text-blue-600 border-gray-300 focus:ring-blue-500 "> <span>Male</span> </label> <label class="flex items-center space-x-2"> <input type="radio" name="gender" value="female" class=" w-4 h-4 text-blue-600 border-gray-300 focus:ring-blue-500 "> <span>Female</span> </label> </div>

Form States

1. Disabled State

html
<!-- Disabled input --> <input type="text" placeholder="Disabled input" disabled class=" w-full px-4 py-2 border border-gray-300 rounded bg-gray-100 text-gray-500 cursor-not-allowed " > <!-- Disabled button --> <button disabled class=" px-4 py-2 bg-blue-500 text-white rounded disabled:bg-gray-400 disabled:cursor-not-allowed " > Disabled button </button>

2. Read-only State

html
<!-- Read-only input --> <input type="text" value="Read-only content" readonly class=" w-full px-4 py-2 border border-gray-300 rounded bg-gray-100 " >

3. Error State

html
<!-- Error state input --> <input type="text" placeholder="Enter username" class=" w-full px-4 py-2 border border-red-500 rounded focus:outline-none focus:ring-2 focus:ring-red-500 focus:border-transparent " > <p class="text-red-500 text-sm mt-1">Username cannot be empty</p>

Form Layouts

1. Horizontal Layout

html
<!-- Horizontal form --> <form class="flex items-end space-x-4"> <div class="flex-1"> <label class="block text-sm font-medium text-gray-700 mb-1"> Username </label> <input type="text" placeholder="Enter username" class=" w-full px-4 py-2 border border-gray-300 rounded focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent " > </div> <div class="flex-1"> <label class="block text-sm font-medium text-gray-700 mb-1"> Password </label> <input type="password" placeholder="Enter password" class=" w-full px-4 py-2 border border-gray-300 rounded focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent " > </div> <button type="submit" class=" px-6 py-2 bg-blue-500 hover:bg-blue-600 text-white rounded font-medium " > Login </button> </form>

2. Vertical Layout

html
<!-- Vertical form --> <form class="space-y-4 max-w-md"> <div> <label class="block text-sm font-medium text-gray-700 mb-1"> Username </label> <input type="text" placeholder="Enter username" class=" w-full px-4 py-2 border border-gray-300 rounded focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent " > </div> <div> <label class="block text-sm font-medium text-gray-700 mb-1"> Email </label> <input type="email" placeholder="Enter email" class=" w-full px-4 py-2 border border-gray-300 rounded focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent " > </div> <div> <label class="block text-sm font-medium text-gray-700 mb-1"> Password </label> <input type="password" placeholder="Enter password" class=" w-full px-4 py-2 border border-gray-300 rounded focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent " > </div> <button type="submit" class=" w-full px-4 py-2 bg-blue-500 hover:bg-blue-600 text-white rounded font-medium " > Register </button> </form>

3. Grid Layout

html
<!-- Grid form --> <form class="grid grid-cols-1 md:grid-cols-2 gap-4 max-w-2xl"> <div> <label class="block text-sm font-medium text-gray-700 mb-1"> First Name </label> <input type="text" placeholder="Enter first name" class=" w-full px-4 py-2 border border-gray-300 rounded focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent " > </div> <div> <label class="block text-sm font-medium text-gray-700 mb-1"> Last Name </label> <input type="text" placeholder="Enter last name" class=" w-full px-4 py-2 border border-gray-300 rounded focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent " > </div> <div class="md:col-span-2"> <label class="block text-sm font-medium text-gray-700 mb-1"> Email </label> <input type="email" placeholder="Enter email" class=" w-full px-4 py-2 border border-gray-300 rounded focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent " > </div> <div class="md:col-span-2"> <label class="block text-sm font-medium text-gray-700 mb-1"> Address </label> <textarea placeholder="Enter address" rows="3" class=" w-full px-4 py-2 border border-gray-300 rounded focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent resize-none " ></textarea> </div> <div class="md:col-span-2"> <button type="submit" class=" w-full px-4 py-2 bg-blue-500 hover:bg-blue-600 text-white rounded font-medium " > Submit </button> </div> </form>

Custom Form Styles

1. Using @apply

css
/* Define form styles in CSS file */ .form-input { @apply w-full px-4 py-2 border border-gray-300 rounded focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent; } .form-input-error { @apply border-red-500 focus:ring-red-500; } .form-label { @apply block text-sm font-medium text-gray-700 mb-1; }
html
<!-- Use custom styles --> <form class="space-y-4"> <div> <label class="form-label">Username</label> <input type="text" class="form-input" placeholder="Enter username"> </div> <div> <label class="form-label">Password</label> <input type="password" class="form-input form-input-error" placeholder="Enter password"> </div> </form>

2. Configure Plugin Options

javascript
// tailwind.config.js module.exports = { plugins: [ require('@tailwindcss/forms')({ strategy: 'class', // or 'base' }), ], }

Complete Form Example

html
<!-- Registration form --> <form class="max-w-md mx-auto space-y-6 bg-white p-8 rounded-lg shadow-md"> <h2 class="text-2xl font-bold text-center text-gray-900"> Create Account </h2> <div> <label class="block text-sm font-medium text-gray-700 mb-1"> Username </label> <input type="text" placeholder="Enter username" class=" w-full px-4 py-2 border border-gray-300 rounded focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent " > </div> <div> <label class="block text-sm font-medium text-gray-700 mb-1"> Email </label> <input type="email" placeholder="Enter email" class=" w-full px-4 py-2 border border-gray-300 rounded focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent " > </div> <div> <label class="block text-sm font-medium text-gray-700 mb-1"> Password </label> <input type="password" placeholder="Enter password" class=" w-full px-4 py-2 border border-gray-300 rounded focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent " > </div> <div> <label class="flex items-center space-x-2"> <input type="checkbox" class=" w-4 h-4 text-blue-600 border-gray-300 rounded focus:ring-blue-500 " > <span class="text-sm text-gray-700"> I agree to the terms and privacy policy </span> </label> </div> <button type="submit" class=" w-full px-4 py-2 bg-blue-500 hover:bg-blue-600 text-white rounded font-medium transition-colors " > Register </button> <p class="text-center text-sm text-gray-600"> Already have an account? <a href="#" class="text-blue-600 hover:text-blue-500"> Login </a> </p> </form>

Best Practices

1. Consistent Focus Styles

html
<!-- All form elements use consistent focus styles --> <input type="text" class=" border border-gray-300 rounded focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent " >

2. Clear Labels

html
<!-- Use clear labels --> <div> <label class="block text-sm font-medium text-gray-700 mb-1"> Username <span class="text-red-500">*</span> </label> <input type="text" placeholder="Enter username" class="w-full px-4 py-2 border border-gray-300 rounded" > </div>

3. Error Messages

html
<!-- Provide clear error messages --> <div> <label class="block text-sm font-medium text-gray-700 mb-1"> Email </label> <input type="email" class=" w-full px-4 py-2 border border-red-500 rounded focus:outline-none focus:ring-2 focus:ring-red-500 focus:border-transparent " > <p class="text-red-500 text-sm mt-1"> Please enter a valid email address </p> </div>

Considerations

  1. Accessibility: Ensure form elements have proper labels and ARIA attributes
  2. Mobile Optimization: Consider touch targets and input experience on mobile devices
  3. Validation Feedback: Provide immediate validation feedback
  4. Error Handling: Display error messages clearly
  5. Submit State: Handle submission state and disable buttons

Summary

TailwindCSS Forms plugin provides:

  • Beautiful default form styles
  • Consistent cross-browser behavior
  • Flexible customization options
  • Simplified form development workflow

By properly using the Forms plugin, you can quickly create beautiful and user-friendly form interfaces.

标签:Tailwind CSS