Browse docs

Switch

CSS-only toggle switch built on a native checkbox. No JavaScript for the toggle.

stableSince 0.2.0

On this page
  1. Semantic HTML
  2. Accessibility
  3. RTL support
  4. Class contract
ExampleScroll inside to see moreOpen in a new page

The .pl-c-switch is a toggle switch built on a native checkbox. The thumb slides via CSS transition on inset-inline-start. The :checked state drives the on/off styling. No JavaScript required for the toggle itself.

Semantic HTML

<!-- Cross-browser track + thumb pattern -->
<label class="pl-c-switch">
  <input type="checkbox" role="switch" class="pl-c-switch__input" />
  <span class="pl-c-switch__track">
    <span class="pl-c-switch__thumb"></span>
  </span>
  <span class="pl-c-switch__label">Enable feature</span>
</label>

<!-- Native switch attribute progressive enhancement (Safari 17.4+ / Chromium) -->
<label class="pl-c-switch">
  <input type="checkbox" switch class="pl-c-switch__control" />
  <span class="pl-c-switch__label">Enable feature</span>
</label>

Accessibility

The native checkbox is visually hidden but accessible to assistive technology. The role="switch" attribute communicates the toggle semantics. Keyboard: Space toggles. Focus ring appears on the track.

RTL support

The thumb slides in the correct logical direction automatically because it uses inset-inline-start (not left), which flips under dir="rtl".

Class contract

Selector Role
.pl-c-switch Container label (flex row).
.pl-c-switch__input Visually hidden checkbox.
.pl-c-switch__track The pill background.
.pl-c-switch__thumb The sliding circle.
.pl-c-switch__label Text label.
[data-size] sm, default, lg.

Search Porchlight