Forms

@sgroup/tailwind-plugins/forms

We use tailwindcss-forms as a reset for form styling. This give you a good head-start for styling forms forms from a non-opinionated base.

Unstyled forms

When you want custom styling for your forms, you can use a collection of .form-* classes.

Form label

Across the board, use the .form-label class for styling the label of a form control.

Form label

Form text

Form text should be explicitly associated with the form control it relates to using the aria-describedby attribute. This will ensure that assistive technologies—such as screen readers—will announce this form text when the user focuses or enters the control.

Form text

Inputs

Use the .form-control class on any input.

Form inputs
File input
Color input

Disabled

Add the disabled attribute on an input to give it a grayed out appearance and remove pointer events.

Disabled

Readonly

Add the readonly attribute on an input to prevent modification of the input’s value.

Readonly

Textarea

Form textarea

Select

Custom <select> menus need only a custom class, .form-select to trigger the custom styles. Custom styles are limited to the <select>’s initial appearance and cannot modify the <option>s due to browser limitations.

Form selects

Checkbox

Use the .form-check-input and .form-check-label for both radio and checkbox inputs.

Form checkbox

Radio

Form radio

Inline

Group checkboxes or radios on the same horizontal row by adding .form-check-inline to any .form-check.

Inline checkbox and radio

Floating labels

Wrap a pair of <input class="form-control"> and <label> elements in .form-floating to enable floating labels. A placeholder is required on each <input> as our method of CSS-only floating labels uses the :placeholder-shown pseudo-element. Also note that the <input> must come first so we can utilize a sibling selector (e.g., ~).

Floating label

When there’s a value already defined, <label>s will automatically adjust to their floated position.

Floating label with an existing value

Textareas

By default, <textarea>s with .form-control will be the same height as <input>s.

Floating label textarea

To set a custom height on your <textarea>, do not use the rows attribute. Instead, set an explicit height (either inline or via custom CSS).

Floating label textarea height

Selects

Other than .form-control, floating labels are only available on .form-selects. They work in the same way, but unlike <input>s, they’ll always show the <label> in its floated state. Selects with size and multiple are not supported.

Floating label select

Configuration

Configure the @sgroup/tailwind-plugins/forms plugin using theme options.

theme: {
    extend: {
        forms: (theme) => {
            return {
                // BASE STYLES
                // Set the primary colour for form-focus border/ring and the checked state for checkboxes and radios.
                // This is applicable to the base (whether you use the form classes or not).
                primaryColor: theme('colors.red.500'),

                // COMPONENT STYLES
                // Styles for form labels
                label: {
                    marginBottom: theme('margin.4'),
                    fontWeight: theme('fontWeight.bold'),
                },

                // Styles for form text
                text: {
                    fontSize: theme('fontSize.xs'),
                },

                // Styles for form inputs and selects
                control: {
                    padding: `theme('padding.6') theme('padding.4')`,
                },

                // Styles for form checkboxes and radios
                check: {
                    paddingLeft: theme('padding.6'),
                },

                // Styles for form checkboxes and radios inputs
                checkInput: {
                    // Change for just checkboxes
                    '&[type=checkbox]': {
                        borderRadius: theme('borderRadius.DEFAULT'),
                    },

                    // Change for just radios
                    '&[type=radio]': {
                        borderRadius: theme('borderRadius.full'),
                    },
                },

                // Styles for floating labels
                formFloating: {

                },
            };
        },
    },
},