Whakerexa > Components > Forms

Switch

Load button.css to use toggle switches. A switch is a styled checkbox: wrap an <input type="checkbox"> inside a <label class="switch"> and add a <span class="switch-slider"> for the visual track and knob. No JavaScript required.

Without text

The minimal form uses only the slider span with .switch-no-text. Add .round to the slider span for a pill-shaped track.

Accessibility. The slider is a purely decorative CSS element — it carries no text. The underlying <input> must therefore have an explicit aria-label so screen readers can announce the control's purpose. The checkbox is keyboard-reachable (Tab) and toggled with Space, without any JavaScript.

<label class="switch">
    <input type="checkbox" aria-label="Toggle">
    <span class="switch-slider switch-no-text"></span>
</label>

<!-- Rounded -->
<label class="switch">
    <input type="checkbox" aria-label="Toggle">
    <span class="switch-slider switch-no-text round"></span>
</label>

Variables

Size and colors are controlled by the following variables:

Variable Role Default
--switch-width Total width of the track calc(var(--font-size) * 5)
--switch-height Total height of the track calc(var(--font-size) * 2.8)
--switch-gap Room kept between the knob and the edge of the track calc(0.4 * var(--font-size))
--switch-knob-size Side of the knob and of each text box; follows the height of the track calc(var(--switch-height) - var(--switch-gap))
--switch-radius Radius of the track var(--border-radius)
--switch-knob-radius Radius of the knob and of the texts var(--border-radius)
--switch-bg-color Ground of the track, on and off alike var(--inputs-bg-color)
--switch-fg-color Color of the text written in the track var(--buttons-fg-color)
--switch-slider-off-color Knob color when OFF var(--error-color)
--switch-slider-on-color Knob color when ON var(--success-color)
--switch-slider-unselected-opacity Visibility of the inactive text label 0

With text labels

Place a .switch-off-text and a .switch-on-text span inside the slider to label each state. The visible text does not replace the accessible name: keep aria-label on the <input> to describe what the switch controls, not just its current state.

<label class="switch">
    <input type="checkbox" aria-label="Toggle ON/OFF">
    <span class="switch-slider">
        <span class="switch-off-text">OFF</span>
        <span class="switch-on-text">ON</span>
    </span>
</label>

Custom size

Override --switch-width and --switch-height inline to resize a specific switch without affecting others. The knob and text labels scale automatically with the track.

<label class="switch"
       style="--switch-width: calc(var(--font-size) * 3);
              --switch-height: calc(var(--font-size) * 1.6)">
    <input type="checkbox" aria-label="Small toggle">
    <span class="switch-slider switch-no-text round"></span>
</label>