Home / Snippets / UI Components /

Toggle button

A smooth toggle switch — checkbox under the hood, custom UI on top.

Dark mode
Notifications
Widely Supported
uino-js

Quick implementation

/* Hide the native checkbox */
.toggle-input {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
}

/* The visible track */
.toggle-track {
  display: inline-block;
  width: 3rem;
  height: 1.625rem;
  background: oklch(0.35 0.03 260);
  border-radius: 1rem;
  position: relative;
  cursor: pointer;
  transition: background 0.25s ease;
}

/* The sliding knob */
.toggle-track::after {
  content: '';
  position: absolute;
  top: 0.1875rem;
  left: 0.1875rem;
  width: 1.25rem;
  height: 1.25rem;
  background: oklch(0.95 0 0);
  border-radius: 50%;
  transition: transform 0.25s ease;
}

/* Checked state */
.toggle-input:checked + .toggle-track {
  background: oklch(0.52 0.22 265);
}

.toggle-input:checked + .toggle-track::after {
  transform: translateX(1.375rem);
}

/* Focus ring for keyboard users */
.toggle-input:focus-visible + .toggle-track {
  outline: 2px solid oklch(0.72 0.19 265);
  outline-offset: 2px;
}

Prompt this to your LLM

Includes role, constraints, and edge cases to handle.

You are a senior frontend engineer building a settings UI.

Goal: A toggle switch component using a visually hidden checkbox and a styled label — no JavaScript for the toggle logic.

Technical constraints:
- Visually hide the checkbox with position: absolute and opacity: 0.
- Use the adjacent sibling selector (+) to style the label based on :checked state.
- The knob slides via transform: translateX() with a CSS transition.
- Include :focus-visible styling for keyboard accessibility.
- Use oklch() for colors. The track should change color when toggled on.

Accessibility:
- The checkbox must have an associated label or aria-label.
- Do NOT use display: none on the checkbox — it must remain in the tab order.
- Screen readers should announce the toggle as a checkbox with its checked state.

Why this matters in 2026

Toggle switches appear in every settings page, cookie banner, and preference panel. Using a real <input type="checkbox"> under the hood gives you free accessibility: screen readers announce it as a checkbox, keyboard users can toggle it with Space, and form submission works without JavaScript. The CSS layer is purely visual — a styled label with a sliding pseudo-element knob.

The logic

The checkbox is visually hidden but remains in the DOM and tab order. The <label> is styled as the track, and its ::after pseudo-element is the knob. When the checkbox is :checked, the adjacent sibling selector + targets the label, changing the track color and translating the knob to the right. The transition property smooths the movement. :focus-visible adds a focus ring only for keyboard navigation, not mouse clicks.

Accessibility & performance

Because the underlying element is a real checkbox, screen readers announce it correctly: "Dark mode, checkbox, checked." Keyboard users can toggle it with Space. The :focus-visible outline appears only during keyboard navigation — mouse users don't see it. Transition performance is excellent since we're only animating transform and background, both of which are GPU-composited.