Repo

A switch is a checkbox drawn as a toggle. It is still a native <input type="checkbox">, so form submission, :checked, :disabled and keyboard use (Space) all work. The component adds role="switch", so a screen reader says "on" or "off" instead of "checked".

Use a switch for a setting that takes effect straight away, such as "Notifications". Use a checkbox when the user must submit a form to apply the choice.

Usage

<label class="zui-switch">
<input type="checkbox" role="switch" checked />
Notifications
</label>

In a form

Wrap a switch in a Field like any other control. A switch with a description keeps the label short and puts the detail below.

Product news, once a month.

<div class="zui-field">
<label class="zui-switch">
  <input type="checkbox" role="switch" name="marketing" aria-describedby="marketing-hint" />
  Marketing emails
</label>
<p class="zui-field-description" id="marketing-hint">Product news, once a month.</p>
</div>

Disabled

<label class="zui-switch">
<input type="checkbox" role="switch" disabled checked />
Disabled on
</label>
<label class="zui-switch">
<input type="checkbox" role="switch" disabled />
Disabled off
</label>

Size and colour

Size scales with --zui-switch-size. Set it on the element or a wrapper.

<label class="zui-switch" style="--zui-switch-size: 1.1em">
<input type="checkbox" role="switch" checked />
Small
</label>
<label class="zui-switch" style="--zui-switch-size: 2em; --zui-switch-accent: var(--color-success)">
<input type="checkbox" role="switch" checked />
Large, success colour
</label>

Accessibility

CSS custom properties

Set these on :root, on a wrapper, or on the element. See Component custom properties.

Property Default Description
--zui-switch-size 1.5em Track height. Track width is 1.8 times this
--zui-switch-radius var(--radius-full) Border radius of the track and thumb
--zui-switch-accent var(--color-accent) Track colour when on
--zui-switch-track oklch(from var(--color-text) l c h / 25%) Track colour when off
--zui-switch-thumb var(--color-background) Thumb colour

Theme

Copy this CSS to your project: