Skip to content

Pill Button Group Toggle

Pure CSS pill button group built on radio buttons, with a colored background that slides to the selected option. No JavaScript needed.

Also known as: segmented control, pill toggle switch, radio button pill group, sliding pill selector, billing period toggle

Pill Button Group Toggle live demo

Open in new tab

Pure CSS code (HTML, CSS)

A set of rounded pill button groups built with pure CSS, where a colored pill slides behind the selected option. Use it for billing period switches, calendar or chart range pickers, theme selectors and any segmented control with two to five choices. No JavaScript is required.

How it works

Each .ptg-group is a role="radiogroup" that contains one .ptg-slider and several label.ptg-option elements. Every option wraps a native radio button (.ptg-input) and its visible text (.ptg-label). Because the controls are real radio buttons sharing a name, the browser handles selection, keyboard input and form submission for you.

The group is an inline grid with grid-auto-columns: 1fr, so all options get the width of the widest one. Two custom properties drive the slider. --ptg-count is set by :has() rules that check how many .ptg-option elements exist, and the slider width becomes calc((100% - 2 * var(--ptg-pad)) / var(--ptg-count)). --ptg-index is set by :has() rules that look for the checked input inside the second, third, fourth or fifth option. The slider then moves with transform: translateX(calc(var(--ptg-index) * 100%)) and a slightly springy cubic-bezier transition. The checked label switches to --ptg-active-text.

The billing row also shows a pure CSS result: .ptg-row:has(.ptg-input[value="yearly"]:checked) hides .ptg-price--monthly and reveals .ptg-price--yearly.

Customization

  • .ptg-group--ink, .ptg-group--coral and .ptg-group--mint: each sets --ptg-active-bg, the color or gradient of the sliding pill. Add your own modifier for a new theme.
  • --ptg-active-text on .ptg-group: text color of the selected option, for example a dark color on a light pill.
  • --ptg-speed in .ptg-demo: duration of the slide and the text color fade.
  • --ptg-pad: the gap between the track edge and the pill; larger values give a chunkier, inset look.
  • --ptg-track and --ptg-track-border: background and border of the white track.
  • .ptg-group--sm: a compact size with smaller padding and text, used for the icon group.
  • Add a fifth label.ptg-option to any group; the count and index rules already cover up to five options.

Use cases

  • Pricing pages: switch between monthly and yearly billing and show the matching price
  • Dashboards: choose a chart or calendar range such as week, month or year
  • Settings screens: a theme selector with two to five choices in one control
  • Compact toolbars: a small icon-only group saves space in tight toolbars
  • Forms without scripts: native radio inputs handle keyboard and form submission with no JavaScript

Accessibility

Each group has role="radiogroup" and an aria-labelledby pointing to its .ptg-caption. The radio inputs are hidden visually with opacity: 0 but stay in the accessibility tree, so screen readers announce "radio button, 2 of 4, checked". Users can Tab into a group and change the selection with the arrow keys. :focus-visible on the input draws a ring around the matching .ptg-label. Icons are aria-hidden, and the slider is decorative. With prefers-reduced-motion: reduce the pill jumps without animation. Active text colors meet WCAG AA contrast on every pill color.

Usage notes

The sliding pill needs :has(), available in all current browsers. Older browsers fall back through @supports not selector(:has(*)): the slider is hidden and the checked label gets the active background directly, so the control still works.

Pill Button Group Toggle at a glance

Framework
Pure CSS
Code
HTML, CSS
JavaScript
Not needed, CSS only
Dependencies
None
Layout
Responsive, works on mobile
Category
Button Groups
License
Free for personal and commercial use

Questions about this template

Can I use the Pill Button Group Toggle code for free?

Yes. You can copy or download the code and use it in personal and commercial projects, including client work. No sign-up is needed. Reselling it as part of a component library or template collection is not allowed; see the license.

How do I add it to my website?

There is nothing to install. Paste the HTML where the component should appear and add the CSS to your stylesheet. Download HTML gives you a complete page with everything already linked.

Does it need jQuery or a JavaScript framework?

No. It is built with HTML and CSS only, so there is no JavaScript to load.

Does it work on mobile?

Yes. The layout adapts to small screens. Use the Mobile and Tablet buttons above the live demo to check it at phone and tablet width.

Requirements

No dependencies. Pure HTML, CSS and JavaScript.

Pill Button Group Toggle – Pure CSS button groups component screenshot
Pill Button Group Toggle · Pure CSS Button Groups

More Button Groups

Split Button Dropdown – Bootstrap 5 button groups preview

Split Button Dropdown

Bootstrap 5 split buttons: a primary deploy action whose target is chosen from an attached dropdown, plus compact save and export split buttons.

Editor Button Toolbar – Bootstrap 5 button groups preview

Editor Button Toolbar

Dark Bootstrap 5 text editor toolbar with grouped icon buttons, toggle states, a block style dropdown, arrow key navigation and a working editable area.

Segmented Control – Tailwind CSS button groups preview

Segmented Control

Tailwind CSS segmented control with a sliding thumb, built on native radio inputs for single-choice view, range and billing toggles.