forms

Tailwind CSS Checkbox

The checkbox component allows users to select one or more options by ticking a square box, available in various styles, sizes, colors, and variants.

Class Name
Type
Description
checkboxComponentComponent class for checkbox.
custom-optionComponentFor custom option container.
custom-soft-optionComponentFor custom option container with background.
label-textPartBase class for title label text.
helper-textPartBase class for helper label text.
checkbox-primaryColorAdds ‘primary’ color to checkbox.
checkbox-secondaryColorAdds ‘secondary’ color to checkbox.
checkbox-accentColorAdds ‘accent’ color to checkbox.
checkbox-infoColorAdds ‘info’ color to checkbox.
checkbox-successColorAdds ‘success’ color to checkbox.
checkbox-warningColorAdds ‘warning’ color to checkbox.
checkbox-errorColorAdds ’error’ color to checkbox.
is-validStateAdds success style to the input.
is-invalidStateAdds error style to the input.
checkbox-xsSizeCheckbox with extra small size.
checkbox-smSizeCheckbox with small size.
checkbox-mdSizeCheckbox with medium(default) size.
checkbox-lgSizeCheckbox with large size.
checkbox-xlSizeCheckbox with extra large size.

Basic checkbox example.

Please use label-text for the label and helper-text for the text that appears at the bottom as helper text.

Apply a semantic color to the checkbox by using the checkbox-{semantic-color} modifier class together with the checkbox component class.

To create custom checkboxes, you can use Tailwind CSS utilities like bg-*, text-* (for the tick), and border-* to style both the checked and unchecked states. With the --input-color value, you can easily modify the background and border color when the checkbox is checked. However, this will not affect the tick color, which by default uses the neutral-content text color. If you want to change the tick color, you will need to update the text color specifically for the checked state by using the checked:text-* utility.

Add responsive class checkbox-{size} where {size} = xs|sm|md|lg|xl for checkbox of different sizes.

Success state can be show using is-valid class.

Error state can be show using is-invalid class.

Prevent user interaction with the checkbox by adding the disabled attribute to make it non-selectable.

The indeterminate state can be configured using JavaScript. To gain further insight into this concept, you can explore more about it here.

A group of checkbox components.

A vertical group of checkbox components.

Below example shows a checkbox in dropdown.

This example can be used to show a list of checkbox buttons inside a grouped list.

Use this example to group up checkbox button components inside a list.

The custom-option component is a flexible element with an associated label and background color, which can include text, icons, or images. It allows customization for layout, appearance, and interaction, providing various ways to enhance option designs.

Utilize the hidden utility class to conceal the checkbox button.

The custom-soft-option component is a flexible element that includes a background color, which becomes active when the option is selected. It can be used with any form of input, providing a visually distinct way to highlight the selected option.

Customize the checkbox styling using utility classes according to your preferences, as illustrated below.

This example displays an image as a checkbox button.