Segmented Control
An iOS-style three-option segmented control with a sliding indicator.
<div class="relative grid w-full max-w-sm grid-cols-3 rounded-xl bg-slate-100 p-1"> <input id="seg-day" type="radio" name="seg" class="peer/seg1 sr-only" /> <input id="seg-week" type="radio" name="seg" checked class="peer/seg2 sr-only" /> <input id="seg-month" type="radio" name="seg" class="peer/seg3 sr-only" /> <span class="pointer-events-none absolute top-1 bottom-1 left-1 w-[calc((100%-8px)/3)] rounded-lg bg-white shadow-sm transition-transform duration-200 peer-checked/seg2:translate-x-full peer-checked/seg3:translate-x-[200%]"></span> <label for="seg-day" class="relative z-10 cursor-pointer rounded-lg px-4 py-2 text-center text-sm text-slate-500 transition-colors hover:text-slate-800 peer-checked/seg1:text-slate-900">Day</label> <label for="seg-week" class="relative z-10 cursor-pointer rounded-lg px-4 py-2 text-center text-sm font-medium text-slate-900">Week</label> <label for="seg-month" class="relative z-10 cursor-pointer rounded-lg px-4 py-2 text-center text-sm text-slate-500 transition-colors hover:text-slate-800">Month</label> </div>
Free to use in personal and commercial projects. No attribution required.
Variants
Same component, different looks. Preview each one live and copy the code you like.
Dark
The dark control matches dashboards and apps with a dark theme.
Two options
The two-option version works for binary choices like on/off or monthly/yearly billing.
Pill
The pill shape softens the control for consumer apps and mobile interfaces.
About this segmented control
The segmented control is the iOS pattern for switching between a few mutually exclusive views: Day, Week, Month on a calendar, or List versus Grid versus Map. A white pill slides behind the active option, which reads instantly without any dropdown.
Three named radio inputs sit first in the DOM, then an absolutely positioned pill, then the labels. Tailwind named peers (peer/seg2) let the pill translate exactly one segment width when each radio is checked: 0, 100 percent, or 200 percent of its own width. Since all radios share a name, only one can be active, and the sliding transition makes the pill glide instead of jump.
Use it for view switchers, time range pickers, or plan toggles with three options. The preview shows the middle option selected. Keep labels short so each segment stays tappable on small screens.
Two to four mutually exclusive views, Day Week Month or List Grid Map, sit in an awkward middle: a dropdown hides the options, full tabs eat vertical space, and separate pages are overkill. The segmented control is the iOS answer: every option visible at once, one sliding pill marking the active one, zero navigation cost. It is the highest-density switcher that still feels calm.
Three named radio inputs come first in the DOM, then an absolutely positioned pill, then the labels. Tailwind's named peers, peer/seg2, let the pill translate exactly one segment width per option: 0, 100 percent, or 200 percent of its own width. Because the radios share a name, only one can be checked, so the pill can never land between two options.
Add arrow-key navigation with roving tabindex so keyboard users can move between segments without tabbing through each one. When segments carry counts, like Inbox (12), keep the count inside the label in a muted tone so it never fights the selected state. For two-option toggles like Map and List, consider whether a simple switch would be clearer; segmented controls earn their complexity at three or more options. On narrow screens, let the labels shrink to icons with tooltips rather than crushing the text.
Unequal label widths silently break the pill math. The translate percentages assume every segment is identical, so one long label, Last 12 months next to Day, leaves the pill floating half over the wrong option. Keep labels similar in length, or enforce equal columns with a grid. Test the pill position on every option, not just the first.
Where to use it
- ✓ Calendar Day Week Month switchers
- ✓ Pricing plan toggles
- ✓ Chart time range pickers
- ✓ List grid map view switchers
Customization tips
Add a fourth option by switching to grid-cols-4 and adjusting the pill width and translate steps. Darken the track to slate-200 for more contrast. Increase the pill shadow for a more lifted look.
Design notes
When to use
A segmented control is ideal for switching between two to four mutually exclusive views of the same screen, like Day, Week, and Month on a calendar. The sliding indicator makes the selection feel physical, and because all options stay visible, users discover choices they would miss in a dropdown. It works best when the switch is instant and the content below updates in place.
What to avoid
Do not use a segmented control for more than four options, the segments get too narrow to tap or read. Avoid it for actions that navigate to different pages, it promises an instant switch and a page load breaks that promise. And do not default to no selection, a segmented control should always have one active option.
Accessibility
The radio inputs with a shared name are the right native pattern, keep them, since arrow key support comes built in. Make sure each option has a real label element so screen readers announce the choices clearly.
Frequently asked questions
How do I make a segmented control in Tailwind?
Use radio inputs with named peers, an absolutely positioned pill behind the labels, and translate-x utilities tied to each checked state. The pill width is one segment: w-[calc((100%-8px)/3)] for three options.
Why are the radio inputs before the labels?
peer-checked only styles elements that come after the input in the DOM. The inputs are sr-only so they take no space, and the for attributes on the labels keep them clickable.
Can I use this with more than three options?
Yes. Change the grid columns, set the pill width to 1/N of the track, and add translate steps (100 percent per extra option). The same named-peer pattern scales to any count.
How do I add a sliding indicator to a segmented control?
Place an absolutely positioned pill behind the options and move it with transform translateX to the selected segment, transitioning the transform over 200ms. Size it to one segment by dividing the track width by the option count. This element keeps the indicator as a separate layer so the labels stay readable above it.
Do segmented controls support arrow key navigation?
Yes, when built on native radio inputs sharing one name attribute, the browser gives you arrow key movement between options for free. This is exactly why this element uses radios under the hood. Keep the inputs in DOM order matching the visual order, or the arrow keys will jump unpredictably.
When should I use a segmented control instead of tabs?
Use a segmented control when the options switch a view, filter, or mode on the same screen, like Day, Week, Month on a chart. Use tabs when each option is a distinct page or panel with its own URL. Segmented controls imply instant in-place change, tabs imply navigation.