forms

Tailwind CSS Switch

Utilize the switch component to toggle between two states (true or false) with a single click. The component comes in multiple sizes, variants, and colors.

Class Name
Type
Description
switchComponentComponent class for switch.
label-textPartBase class for title label text.
helper-textPartBase class for helper label text.
switch-outlineStyleComponent class outline switch.
switch-primaryColorAdds ‘primary’ color to switch.
switch-secondaryColorAdds ‘secondary’ color to switch.
switch-accentColorAdds ‘accent’ color to switch.
switch-infoColorAdds ‘info’ color to switch.
switch-successColorAdds ‘success’ color to switch.
switch-warningColorAdds ‘warning’ color to switch.
switch-errorColorAdds ’error’ color to switch.
is-validStateAdds success style to the input.
is-invalidStateAdds error style to the input.
switch-xsSizeSwitch with extra small size.
switch-smSizeSwitch with small size.
switch-mdSizeSwitch with medium size.
switch-lgSizeSwitch with large size.
switch-xlSizeSwitch with extra large size.

Basic Switch example.

For a switch style, apply the switch component class, whereas the default switch appearance is set to a solid color.

To achieve an outline style for the switch, add component class switch-outline alongside switch.

Example of semantic color in default and outline switch.

Solid switch with semantic-color using switch-{semantic-color} modifier class with the component class switch.

Outline switch with semantic-color using switch-{semantic-color} modifier class with component classes switch & switch-outline.

Example of custom color in default and outline switch.

Use checked:text-* to customize the ball color and checked:bg-* or checked:border-* to change the background and border when the switch is checked. Alternatively, you can use checked:[--input-color:...] to set both the background and border color when the switch is checked, simplifying customization. Supports hex, named colors, CSS variables, and Tailwind colors.

For switch-outline, use --input-color to set both the border and text color when the switch is checked. This eliminates the need to apply checked:bg-* and checked:border-* individually. You can still use text-* and border-* utilities to further customize the ball and border colors separately.

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

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

Success state can be show using is-valid class.

Error state can be show using is-invalid class.

You can also add additional elements, such as icons.

Disabled switch

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 switch components in single line.

A vertical group of switch components.

Here’s an example of a switch in dropdown.

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

Use this example to group up switch components inside a list.