Segmented control
<forma-segmented-control>
Radio-backed compact selection with visible selected and focus states; where supported, one light-inertial selection indicator travels between segments without delaying native selection.
- Authoring tag
<forma-segmented-control>- Category
- Selection and choice
- Behavior owner
- Native HTML
- Canonical source
patterns/segmented-control.html
Purpose
A segmented control shows two to five short, mutually exclusive options side by side, with the chosen one filled. It is a native radio group: each forma-segment is a label wrapping a visually hidden input type="radio", and all radios share one name. The browser owns selection, arrow-key movement, form submission and reset. Forma draws the bordered track, the filled selected segment and the focus ring, and in browsers with CSS anchor positioning one indicator travels to the newly selected segment. The selection itself is always immediate.
Use when
- Users switch between a few short, mutually exclusive modes or views: density, units, list/board, day/week/month.
- All options should be visible at once and fit on one or two lines.
- The choice applies immediately or is submitted with a form as one value.
Avoid when
- Options need descriptions or long labels: use Choice group.
- More than about five options, or options that change often: use Select.
- Several options can be chosen together: use Multi-choice or checkboxes; this control is single-select only.
- The segments switch between panels of content with tab semantics: use Tabs.
- The setting is on/off: use Switch.
Behavior to know
- Selection is native radio state;
:checkeddrives the filled segment. - Each segment is at least 44px tall and the whole label is the hit target.
- The selected segment is shown by an inverse fill, not by color hue alone, and by system Highlight in forced-colors mode.
- On narrow screens (44rem and below) the control fills the width and segments wrap instead of shrinking.
Basic example
The smallest correct markup: the canonical pattern from patterns/segmented-control.html inside its public tag. Copy this first, then adapt labels, names and state.
Minimal segmented control
Rendered from the canonical pattern file.
<forma-segmented-control class="forma-component-tag">
<fieldset class="forma-field">
<legend class="forma-field__label">Density</legend>
<div class="forma-segmented">
<label class="forma-segment">
<input type="radio" name="density" value="comfortable" checked>
<span>Comfortable</span>
</label>
<label class="forma-segment">
<input type="radio" name="density" value="compact">
<span>Compact</span>
</label>
</div>
</fieldset>
</forma-segmented-control>
Click or tap the code once to select all of it, then copy.
Examples
Calendar range picker
Four short options with Week preselected. The fieldset legend names the choice, so screen readers announce "Show, radio group" and each option as "Week, radio button, 2 of 4, checked".
<forma-segmented-control class="forma-component-tag">
<fieldset class="forma-field">
<legend class="forma-field__label">Show</legend>
<div class="forma-segmented">
<label class="forma-segment"><input type="radio" name="segmented-control-calendar-range" value="day"><span>Day</span></label>
<label class="forma-segment"><input type="radio" name="segmented-control-calendar-range" value="week" checked><span>Week</span></label>
<label class="forma-segment"><input type="radio" name="segmented-control-calendar-range" value="month"><span>Month</span></label>
<label class="forma-segment"><input type="radio" name="segmented-control-calendar-range" value="year"><span>Year</span></label>
</div>
</fieldset>
</forma-segmented-control>
Click or tap the code once to select all of it, then copy.
No default selection, heavier indicator
A required choice with nothing preselected, so the user must decide. With no checked radio there is no indicator and no filled segment. data-forma-motion-weight="heavy" gives the indicator a slower, more settled travel once a choice is made; weight is presentation only and says nothing about the options.
<forma-segmented-control class="forma-component-tag">
<fieldset class="forma-field">
<legend class="forma-field__label">Billing cycle</legend>
<p class="forma-field__description" id="segmented-control-no-default-heavy-help">Required. You can change this before your first invoice.</p>
<div class="forma-segmented" data-forma-motion-weight="heavy">
<label class="forma-segment"><input type="radio" name="segmented-control-no-default-heavy" value="monthly" required aria-describedby="segmented-control-no-default-heavy-help"><span>Monthly</span></label>
<label class="forma-segment"><input type="radio" name="segmented-control-no-default-heavy" value="annual" aria-describedby="segmented-control-no-default-heavy-help"><span>Annual</span></label>
</div>
</fieldset>
</forma-segmented-control>
Click or tap the code once to select all of it, then copy.
Mobile
Each mobile example renders in its own document inside a real narrow viewport, so Forma's media and container queries run exactly as they would on a phone. Choose a width to compare.
Wrapping segments on a phone
Five options, one with a long label, at phone width. The control stretches to the full width and segments that do not fit move to a second row rather than shrinking below a usable size.
What changes at narrow widths
- At 44rem and below
forma-segmentedtakes the full inline size and each segment flexes from amin(10rem, 100%)basis, so segments share the width evenly. - When the options do not fit on one row they wrap to further rows; nothing scrolls horizontally at 320px.
- Every segment keeps its 44px minimum height, and the whole segment label is the tap target.
- Wrapping changes only the visual rows. Arrow-key order and the radio group's order stay in source order.
<forma-segmented-control class="forma-component-tag">
<fieldset class="forma-field">
<legend class="forma-field__label">Distance unit</legend>
<div class="forma-segmented">
<label class="forma-segment"><input type="radio" name="segmented-control-mobile-wrapping-segments" value="m" checked><span>Metres</span></label>
<label class="forma-segment"><input type="radio" name="segmented-control-mobile-wrapping-segments" value="km"><span>Kilometres</span></label>
<label class="forma-segment"><input type="radio" name="segmented-control-mobile-wrapping-segments" value="ft"><span>Feet</span></label>
<label class="forma-segment"><input type="radio" name="segmented-control-mobile-wrapping-segments" value="mi"><span>Miles</span></label>
<label class="forma-segment"><input type="radio" name="segmented-control-mobile-wrapping-segments" value="nmi"><span>Nautical miles</span></label>
</div>
</fieldset>
</forma-segmented-control>
Click or tap the code once to select all of it, then copy.
Mobile · 320px
The canonical pattern in a true 320px viewport, the minimum width every Forma component must support without page-level horizontal scrolling.
API and configuration
Forma components have no JavaScript API. Their public contract is the markup: native elements and attributes you write, and the CSS hooks below. The CSS hook table is generated from the built stylesheet, so it always matches the shipped implementation.
| Attribute | On | Values | Default | Description |
|---|---|---|---|---|
type | input | radio | — | Required. Each segment is a native radio. |
name | input | string | — | Required and shared by every radio in one control; it makes them one exclusive group and is the submitted field name. Unique per control on the page. |
value | input | string | "on" | Submitted value of the selected segment. |
checked | input | boolean | absent | Initial selection. Omit on all radios when the user must make an active choice. |
required | input | boolean | absent | Native constraint: the form cannot submit until one segment is selected. Put it on one radio of the group. |
aria-describedby | input | id | — | Links help text to the options. |
data-forma-motion-weight | .forma-segmented | light | standard | heavy | light | Perceived mass of the travelling indicator. |
| Hook | Kind | Values or default | Description |
|---|---|---|---|
.forma-segmented | Block class | — | The track: an inline flex row with a functional border, secondary surface and a 2px inner gap. Also the anchor scope and host of the travelling indicator (::before). |
[data-forma-motion-weight] | Attribute hook | "heavy", "light", "standard" | Presentation-only perceived mass for the selection indicator: light (default for this control), standard or heavy. Never encodes importance or preference. |
.forma-segment | Block class | — | One option: a label containing the radio and a text span. At least 44px tall; filled with the inverse surface when its radio is checked. |
| Keys | Result |
|---|---|
| Tab / Shift+Tab | Moves focus into the group (to the checked segment, or the first when none is checked) and out of it. The group is one tab stop. |
| Arrow Right / Arrow Down | Moves to and selects the next segment, wrapping to the first (native radio behavior). |
| Arrow Left / Arrow Up | Moves to and selects the previous segment, wrapping to the last. |
| Space | Selects the focused segment when none is selected yet. |
| Event | Description |
|---|---|
input / change | Native events fired by the newly selected radio. Listen on the fieldset for change to apply a view or setting. |
Form behavior
Submits one name=value pair for the selected segment, nothing when none is selected. Supports required and native validity; form reset restores the initially checked segment.
States
| State | How it is expressed | Presentation |
|---|---|---|
| Unselected | no checked radio | Transparent segment on the secondary surface. With no checked radio in the group, no indicator is drawn. |
| Selected | :checked radio | Inverse (dark) surface with inverse text. With anchor positioning the fill is the travelling indicator; otherwise it is the segment's own background. |
| Focus | :focus-visible on the radio | Two-tone focus ring around the focused segment. |
| Pressed | :active on the radio | The label text scales to 97% while pressed; the segment (hit target) does not move. |
| Disabled | disabled on a radio | Native: cannot be selected or focused. No distinct visual treatment is provided. |
Accessibility
Forma provides
- Keeps native radios as the focusable, labelled elements, so group semantics, position in set and arrow-key selection come from the platform.
- Keeps the radio topmost in the segment for direct and assistive hit-testing.
- Shows selection with a fill contrast (inverse surface) and, in forced-colors mode, Highlight/HighlightText with the indicator removed.
- Draws a visible focus ring on the segment that contains the focused radio.
Your application supplies
- Wrap the control in a fieldset with a legend (or give the group an accessible name).
- Use short, parallel labels; move long explanations to help text linked with aria-describedby.
- Explain disabled options in text, since they are not visually distinct.
- If selecting a segment changes content elsewhere, make that change discoverable (for example a heading update or live region).
Responsive behavior
- Above 44rem the control is inline-sized to its content (
display: inline-flex,max-inline-size: 100%) and wraps if the container is narrower. - At 44rem and below it takes the full width and segments flex evenly from a
min(10rem, 100%)basis, wrapping into additional rows. - Segments never shrink below their 44px height and labels wrap instead of being truncated.
- Equal-width versus content-sized modes, icon segments and a horizontal-overflow strategy are not provided.
Motion
- Where CSS anchor positioning with
anchor-scopeis supported, one indicator (::before) travels to the selected segment using the light inertial duration and damped easing (no overshoot), so it never implies a neighbouring option. data-forma-motion-weightchanges the indicator's perceived mass; light is the default for this control.- Segment background and text color change by perceptual interpolation; pressing compresses only the label text layer, never the hit target.
- Native selection is immediate. The indicator is presentation that follows the checked state and can be interrupted by the next selection.
- Without anchor positioning, under
prefers-reduced-motion: reduce, and in forced-colors mode the indicator is removed and the checked segment's own fill is the complete presentation; press compression is also removed under reduced motion.
Motion weights
Forma motion is physics-derived: perceived mass changes how a surface accelerates and settles, never what state it is in. Compare the three presentation weights of the same markup:
Usage guidance
Do
- Keep labels to one or two words and roughly equal length.
- Preselect a sensible default only when one exists; leave all unchecked when the user must decide.
Avoid
- Using a segmented control for navigation between pages or panels without tab semantics.
- Putting more options in than fit on two rows at 320px.
- Using motion weight to suggest one option is more important.
Verification specimens
Generated fault-injection specimens expose content, spacing, color-cue, and reduced-effective-contrast risks. They screen presentation robustness as engineering screens without claiming human-subject validation.