@tailwindcss/forms

repository·main·Indexed 26 days ago

https://github.com/tailwindlabs/tailwindcss-forms

A Tailwind CSS plugin that provides a basic reset for form elements, making them easy to customize with standard Tailwind utility classes. It includes support for Tailwind CSS v3 and v4, offering both global base styles and specific form-* utility classes (such as .form-input, .form-checkbox, and .form-select) to ensure consistent styling across different browsers.

Tokens
1.1K
Snippets
6
Records
10
Agent score
39%

What's inside @tailwindcss/forms

  1. Configure plugin strategy (base vs class)

    main

    You can use the strategy option to control how styles are applied. This is useful when integrating into existing projects where global resets might be too heavy-handed.

    • strategy: 'base': Only generates global styles for form elements. No form-{name} classes are generated.
    • strategy: 'class': Only generates form-{name} classes. Form elements are not styled globally.
  2. Set strategy in Tailwind CSS v3

    main

    Configure the strategy by passing an options object to the require('@tailwindcss/forms') call in tailwind.config.js.

    // tailwind.config.js
    plugins: [
      require("@tailwindcss/forms")({
        strategy: 'base', // only generate global styles
        strategy: 'class', // only generate classes
      }),
    ],
  3. Use form utility classes

    main

    If you use the class strategy (or the default combined strategy), you can apply consistent styling to form elements using the following classes:

    • Inputs & Textareas: .form-input, .form-textarea
    • Selects: .form-select, .form-multiselect
    • Checkboxes: .form-checkbox
    • Radios: .form-radio

    These classes provide consistent padding, borders, focus rings, and icon resets (like the chevron for selects).

  4. Use form-* classes to style elements

    main

    The plugin generates a set of form-* classes that can be used to explicitly apply form styles to elements. This is useful for making non-form elements (like a <div>) look like form elements or when you want to avoid global resets.

    <input type="email" class="form-input rounded-full px-4 py-3" />
    
    <select class="form-select rounded-full px-4 py-3">
      <!-- ... -->
    </select>
    
    <input type="checkbox" class="form-checkbox rounded text-pink-500" />
  5. Configure the @tailwindcss/forms plugin strategy

    main

    The @tailwindcss/forms plugin accepts an optional strategy option to determine how styles are applied to form elements.

    • base (default): Applies styles globally to form elements using CSS :where() selectors. This resets default browser styles without increasing specificity, making them easy to override.
    • class (default): Applies styles using specific utility classes (e.g., .form-input, .form-checkbox).
    • Combined (default): By default, the plugin applies both base and class strategies.

    To use a specific strategy, pass an object with the strategy key to the plugin in your tailwind.config.js.

  6. Reference form-* classes

    main

    Use these classes to explicitly apply form styles to elements:

    Base ElementClass
    [type='text'], [type='email'], [type='url'], [type='password'], [type='number'], [type='date'], [type='datetime-local'], [type='month'], [type='search'], [type='tel'], [type='time'], [type='week']form-input
    textareaform-textarea
    selectform-select
    select[multiple]form-multiselect
    [type='checkbox']form-checkbox
    [type='radio']form-radio