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.
switch-outlineComponentComponent class outline switch.
form-controlComponentContainer element.
labelComponentFor helper text.
switch-primaryModifierAdds ‘primary’ color to switch.
switch-secondaryModifierAdds ‘secondary’ color to switch.
switch-accentModifierAdds ‘accent’ color to switch.
switch-infoModifierAdds ‘info’ color to switch.
switch-successModifierAdds ‘success’ color to switch.
switch-warningModifierAdds ‘warning’ color to switch.
switch-errorModifierAdds ’error’ color to switch.
is-validModifierAdds success style to the input.
is-invalidModifierAdds error style to the input.
switch-xsResponsiveSwitch with extra small size.
switch-smResponsiveSwitch with small size.
switch-lgResponsiveSwitch with 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.

The bg-{color} utility class within the Solid utility set assigns a color to the knob, while the var(--tglbg) variable determines the background color for the switch. var(--tglbg) can represent various color specifications, including Tailwind color variables, color names, hexadecimal codes, or theme CSS variables.

To customize the outline switch, simply modify the bg and border color properties.

The switch is available in three sizes: switch-xs for extra small, switch-sm for small, the default size, and switch-lg for large.

The switch is available in three sizes: switch-xs for extra small, switch-sm for small, the default size, and switch-lg for large.

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.