Toggle

Tailwind CSS and Alpine JS Toggle

A switch control that allows you to toggle between two states, such as on or off.

Default Toggle

A toggle switch that uses a checkbox input to determine its state (true or false).

HTML
<label for="defaultToggle" class="">
    <input id="defaultToggle" type="checkbox" class="" role="switch" checked  />
    <div class="" aria-hidden="true"></div>
</label>

Toggle with Container

A toggle switch with a contrasting background and label that makes it easy to find and interact with it.

HTML
<label for="toggleWithContainer" class="">
    <input id="toggleWithContainer" type="checkbox" class="" role="switch" checked />
    <div class="" aria-hidden="true"></div>
</label>

Toggle Color Variations

Examples of toggle switches with different color variations.

HTML
<!-- Toggle Primary -->
<label for="togglePrimary" class="">
    <input id="togglePrimary" type="checkbox" class="" role="switch" checked />
    <div class="" aria-hidden="true"></div>
</label>

<!-- Toggle Secondary -->
<label for="toggleSecondary" class="">
    <input id="toggleSecondary" type="checkbox" class="" role="switch" checked />
    <div class="" aria-hidden="true"></div>
</label>

<!-- Toggle Success -->
<label for="toggleSuccess" class="">
    <input id="toggleSuccess" type="checkbox" class="" role="switch" checked />
    <div class="" aria-hidden="true"></div>
</label>

<!-- Toggle Warning -->
<label for="toggleWarning" class="">
    <input id="toggleWarning" type="checkbox" class="" role="switch" checked />
    <div class="" aria-hidden="true"></div>
</label>

<!-- Toggle Danger -->
<label for="toggleDanger" class="">
    <input id="toggleDanger" type="checkbox" class="" role="switch" checked />
    <div class="" aria-hidden="true"></div>
</label>

<!-- Toggle Info -->
<label for="toggleInfo" class="">
    <input id="toggleInfo" type="checkbox" class="" role="switch" checked />
    <div class="" aria-hidden="true"></div>
</label>

Keyboard Navigation

Key Action
Tab Toggle gets the focus
Space Toggles on and off