Skip to content

Switch

Toggle switch for on/off states. Supports both semantic HTML and class-based approaches.

Track height uses --af-switch-height (defaults to --af-choice-size, 1.5rem). Width is height × (1 + --af-switch-width-ratio) (ratio default 0.8). Checkbox and radio use the same --af-choice-size so the three stay visually aligned — see Tokens.

Use role="switch" for better accessibility and semantic HTML. Labels automatically use flex layout when they contain switches:

<label>
  <input type="checkbox" role="switch" checked />
  <span>
    Enable notifications
  </span>
</label>

The traditional class-based approach is still supported:

<label class="af-switch">
  <input type="checkbox" checked />
  <span class="af-switch-slider">
  </span>
</label>
<!-- Semantic approach -->
<input type="checkbox" role="switch" checked />
<input type="checkbox" role="switch" />
<input type="checkbox" role="switch" disabled />
<!-- Class-based approach -->
<label class="af-switch">
  <input type="checkbox" checked />
  <span class="af-switch-slider">
  </span>
</label>

Semantic switch (recommended)

Class-based switch (legacy)

:root {
  --af-choice-size: 1.5rem; /* checkbox, radio, default switch height */
  --af-switch-height: var(--af-choice-size);
  --af-switch-width-ratio: 0.8;
}