StealThis .dev

Segmented Control

iOS-style segmented control (pill-style toggle group) — single selection, animated background slide, keyboard support.

Open in Lab
css vanilla-js
Targets: JS HTML

Code

Segmented Control

An iOS-style segmented control for single-selection tab groups. The active background slides smoothly between segments using CSS translate — no layout recalculations.

How it works

A floating <span class="sc-indicator"> element is positioned behind the active segment. On selection, JavaScript updates --offset and --width CSS custom properties which animate the indicator via transform: translateX().

Features

  • Smooth sliding indicator animation
  • Keyboard accessible (arrow keys, Enter/Space)
  • role="tablist" / role="tab" ARIA pattern
  • Works with any number of segments
  • Configurable via CSS variables

Keyboard navigation

KeyAction
Move selection
Enter / SpaceSelect focused segment
HomeJump to first
EndJump to last