Skip to content

Segmented ControlForm-associated

Baseline 2023 Widely available

Every feature it needs is Baseline widely available.

Works in Chrome 112+, Chrome Android 112+, Edge 112+, Firefox 119+, Firefox for Android 119+, Safari 16.5+, Safari on iOS 16.5+.

Why these versions:

  • Nesting needs Chrome 112+, Chrome Android 112+, Edge 112+, Safari 16.5+ and Safari on iOS 16.5+.
    Chrome before 120 and Safari before 17.2 drop a nested rule that starts with a type selector. This rule has none.
  • ARIA attribute reflection needs Firefox 119+ and Firefox for Android 119+.

Partially supported:

  • Nesting: Chrome, Chrome Android and Edge ship it partially before 120 and Safari and Safari on iOS ship it partially before 17.2.
    Chrome before 120 and Safari before 17.2 drop a nested rule that starts with a type selector. This rule has none.

Full browser support report

sv-segmented-control is a single-selection control, like a radio group, that submits its value with forms.

Usage

<label for="plan">Choose a plan</label>
<sv-segmented-control id="plan" value="1">
<button value="1">Personal</button>
<button value="2">Business</button>
<button value="3">Enterprise</button>
<button value="4" disabled>Custom</button>
</sv-segmented-control>

Only direct child <button> elements become options, and each one needs its own value, since only the first button with a given value can be checked.

Styling the selected option

Two elements paint the selected background: the indicator while it slides, and the selected button once it settles. For a gradient or an image, which --sv-segmented-control-active-bg cannot hold, give both the same fill, or it jumps when the animation ends:

/* Sliding indicator */
sv-segmented-control::part(indicator) {
background: linear-gradient(to right, gold, hotpink);
}
/* Resting background */
sv-segmented-control button[data-sv-active] {
background: linear-gradient(to right, gold, hotpink);
}

Since data-sv-active appears only when the animation ends, style anything that has to change at once, like the text color, on aria-checked="true".

Layout

The element fills its parent's width, and its buttons share that width equally. In a parent that sizes it to its content, like a centering flex or grid container, give it a width to keep the buttons equal.

Since the buttons never shrink below their text, a wide set overflows on small screens. To stack them at a breakpoint, set --sv-segmented-control-flex-dir: column.

Setting flex-direction on the element has no effect, since the axis comes only from vertical and --sv-segmented-control-flex-dir.

Keyboard interaction

Key Behavior
ArrowRight, ArrowDown Selects the next enabled option, wrapping
ArrowLeft, ArrowUp Selects the previous enabled option, wrapping

Accessibility

Screen readers announce the control as a radio group that you name with <label for>, aria-label, or aria-labelledby.

Structure

divcontainer
divindicator - Slides behind the checked option
slotdefault - Your option buttons

API References

Methods

setValue()

Checks the option with the given value and slides the indicator to it, with an optional duration and easing.

interface IndicatorAnimationOptions {
duration?: number;
easing?: string;
}

Properties

value

The value of the checked option. Setting it slides the indicator, like setValue() does. A value that matches no option makes the control invalid.

defaultValue

The value that a form reset brings back, reflected by the value attribute.

required

Whether the control is invalid while no option is checked.

disabled

Whether the element has its own disabled attribute. It stays false when only a disabled ancestor <fieldset> disables the control.

name

The name that the value is submitted under.

vertical

Stacks the buttons in a column, unless --sv-segmented-control-flex-dir sets another direction.

Attributes

"disabled"

Prevents user interaction.

"value"

The value of the checked option. Setting it slides the indicator, like setValue() does. A value that matches no option makes the control invalid.

"required"

Whether the control is invalid while no option is checked.

"vertical"

Stacks the buttons in a column, unless --sv-segmented-control-flex-dir sets another direction.

"name"

The name that the value is submitted under.

Events

valuechange

Fired after value changes, by the user, from code, or by a form reset.

optionclicked

Fired when the user clicks an option or moves to it with an arrow key, before the value changes.

Slots

Default

The option <button> elements.

CSS Properties

--sv-segmented-control-bg

Background color of the container.

--sv-segmented-control-active-clr

Text color of the selected button.

--sv-segmented-control-hover-bg

Background color of an unselected button on hover.

--sv-segmented-control-bdr-clr

Border color of the container.

--sv-segmented-control-bdr-sz

Border width of the container.

--sv-segmented-control-active-bg

Background color of the selected button and of the sliding indicator.

--sv-segmented-control-bdr-rad

Border radius of the container. The buttons follow it with the padding taken off.

--sv-segmented-control-pad

Space between the buttons and around them.

--sv-segmented-control-flex-dir

The flex-direction of the buttons. It overrides vertical.

--sv-segmented-control-dur

Duration of the indicator slide and the text color change.

--sv-segmented-control-ease

Easing of the indicator slide and the text color change.

CSS Parts

::part(container)

The box around the buttons, with the background and the border.

::part(indicator)

The background that slides behind the selected button.