Switch

Source: https://getbootstrap.com/docs/6.0/forms/switch

Custom toggle component built on top of native <input> checkbox elements.

Basic switch

Switches work by layering an invisible checkbox over the custom switch indicator. This preserves interactivity while giving you a completely customizable layer underneath. Switches also include the switch attribute for browsers that support it, which provides haptic feedback when toggling.

&lt;input type="checkbox" value="" id="switch" role="switch" switch>

</div>`} />

With form field

Wrap the .switch in a .form-field layout wrapper and add your label. The grid layout places the switch and label side by side.

&lt;div class="switch">
  &lt;input type="checkbox" value="" id="switchMdLabel" role="switch" switch>
&lt;/div>
&lt;label for="switchMdLabel">Default switch&lt;/label>

</div>`} />

Theme colors

Modify the appearance of switches by adding the .theme-{color} class to the .switch element. This will set the checked background and border color to the theme color.

<Example class="d-flex flex-column gap-2" code={getData('theme-colors').map((themeColor) => &lt;div class="form-field"> &lt;div class="switch theme-${themeColor.name}"> &lt;input type="checkbox" value="" id="switch${themeColor.name}" role="switch" switch checked> &lt;/div> &lt;label for="switch${themeColor.name}">Example ${themeColor.name} switch&lt;/label> &lt;/div>)} />

Disabled

Add the disabled attribute and the associated <label>s are automatically styled to match with a lighter color to help indicate the input’s state.

&lt;div class="switch">
  &lt;input type="checkbox" value="" id="switchDisabledLabel" role="switch" switch disabled>
&lt;/div>
&lt;label for="switchDisabledLabel">Disabled switch&lt;/label>

</div>

<div class="form-field"> <div class="switch"> <input type="checkbox" value="" id="switchCheckedDisabledLabel" role="switch" switch disabled checked> </div> <label for="switchCheckedDisabledLabel">Disabled checked switch</label> </div>`} />

Sizes

Add a size modifier class to make your switch appear smaller or larger.

&lt;div class="switch switch-sm">
  &lt;input type="checkbox" value="" id="switchSizeSm" role="switch" switch>
&lt;/div>
&lt;label for="switchSizeSm">Small switch&lt;/label>

</div> <div class="form-field"> <div class="switch"> <input type="checkbox" value="" id="switchSizeMd" role="switch" switch> </div> <label for="switchSizeMd">Default switch</label> </div> <div class="form-field"> <div class="switch switch-lg"> <input type="checkbox" value="" id="switchSizeLg" role="switch" switch> </div> <label for="switchSizeLg">Large switch</label> </div>`} />

Custom width

Customize your switch for any width and indicator size you need by overriding the CSS variables. For example, for a more modern look, you could widen the switch and indicator.

&lt;div class="switch" style="--bs-switch-width: 2.75rem; --bs-switch-indicator-width: 1.5rem;">&lt;!--[!code highlight]-->
  &lt;input type="checkbox" value="" id="switchCustomWidth" role="switch" switch>
&lt;/div>
&lt;label for="switchCustomWidth">Custom width switch&lt;/label>

</div>`} />

CSS

Variables