Radio
Forms
Open in Storybook: Radio (opens in a new tab)
Overview
A short list of choices where exactly one is chosen: a size, a colour, how often, yes or no. It is a fieldset with a legend and, for each choice, an input type="radio" and its label, side by side and tied by for and id. The browser gives the group, the role, the state, one Tab stop, the arrow keys that move and choose, and the value in a form. The component adds paint and nothing else.
The rule. One name for every radio. Never an input inside its label. No role="radio", no role="radiogroup", no aria-checked, no tabindex, no ul, and no script for the keys. The legend is the question and the label is the choice. Choosing changes no context: it never navigates, submits or opens.
Live example
Anatomy
- Group
fieldsetThe group. It is named by its legend and described by its hint.
- Legend
legendFirst child. The question the radios answer: the name of the group.
- Hint
pOptional. One line for the whole group, read after its name with
aria-describedbyon thefieldset. - Control
input type="radio"The browser’s own element, painted. One
namefor the group, and thevalueis what a form sends. - Label
labelNext to the control, never around it, with
forset to itsid. Its words are the name of the radio, and the whole row is the click target. - Description
pOptional. A line more for one choice, read after its name with
aria-describedbyon theinput.
Usage
Use cases
Seven recipes, each the real component and each working. They are the same parts: only the words, the paint and the layout change.
A short choice
Three words to pick one from, with a hint
fieldset·legend· onename·input type="radio"and itslabelThe group is named by the legend and described by the hint. One Tab stop, and the arrow keys move and choose.
A choice with a line more
A price and a date after the name
aria-describedbyon theinput· the line under itslabelThe label is the name and the line is the description, so the price is not part of the name.
Size
One short word each, side by side, and one that is gone
legend·disabledon the one that is sold outThe sold out size stays in sight with its reason in its name, and the arrow keys skip it.
Colour
The control is the colour, and its name is beside it
--rd-swatch· the name is the colour · a ring outside when onThe colour stays whole when it is chosen. It is never the only cue: the name says it, and the ring says it is chosen.
Colour only
Just the colours, with the labels visually hidden
swatchOnly· thelabelis visually hidden · the colour is the targetA screen reader hears “Sage” and the ring shows which is chosen. A person who talks to the page cannot say a name they cannot see.
Yes or no
Two answers that are both a choice, and none to begin with
requiredon each · “(required)” in thelegendNothing is chosen until the person does it. One answer on its own is a checkbox.
Only accent-color
The browser’s own radio, tinted
accent-color: var(--lilac)· noappearance: noneThe least CSS: the browser draws the control and its focus, and only the tint is ours.
Props
| Prop | Does | Default |
|---|---|---|
legend | Names the group: the question the radios answer | Required |
name | The one name of every radio, which is what makes them a group | Required |
options | The choices: value, label, and optionally description, swatch (a colour: the control is painted in it) and disabled | Required |
hint | One line for the whole group, read after its name. Also on the page | |
defaultValue | The value that is chosen to begin with. Without it, none is | |
value, onValueChange | For a page that owns the state: the value that is chosen, and a call for each choice | |
required | Every radio is required, and the legend says “(required)” | false |
inline | Side by side, wrapping, for choices that are one short word | false |
native | The browser’s own drawing, tinted with accent-color. A swatch has no place in it | false |
swatchOnly | Only the colours are drawn, for the options that have a swatch: the label of each is visually hidden, side by side | false |
Specs
Markup
<fieldset aria-describedby="frequency-hint">
<legend>How often should we deliver?</legend>
<p id="frequency-hint">You can change it at any time.</p>
<input type="radio" id="week" name="frequency" value="week">
<label for="week">Every week</label>
<input type="radio" id="fortnight" name="frequency" value="fortnight" checked>
<label for="fortnight">Every two weeks</label>
</fieldset>
<!-- a choice with a line more: the line is the description of the input -->
<input type="radio" id="express" name="delivery" value="express"
aria-describedby="express-line">
<label for="express">Express</label>
<p id="express-line">€6.00. Arrives tomorrow.</p>Native tags
| Tag | Attribute | What it gives |
|---|---|---|
fieldset | none | The group: the role group, named by its legend. Its own edge and padding are cleared |
legend | first child of the fieldset | The name of the group: the question |
input | type="radio" | The control: the role radio, a checked state, the arrow keys, one Tab stop for the group |
input | name | The same on every radio. It is what makes them a group, and the key of the value in a form |
input | id | What the label points at with for |
input | value | What the form sends for the one that is chosen |
input | checked | The choice made to begin with. At most one radio of a group has it |
input | required | The form is not valid until one is chosen. It is on every radio of the group, and the legend says so in words |
input | disabled | Out of the arrow keys, out of the Tab order and out of the form. It stays in sight |
label | for, next to the input | The name of the radio, and the click target of the row. It is never around the input |
fieldset | aria-describedby | The hint, read after the name of the group |
input | aria-describedby | Only when a choice has a line more: it points at the line under the label |
Native CSS properties
| Property | On | What it does here |
|---|---|---|
appearance: none | the control | Takes the browser’s drawing off, so the control is painted with CSS. It is still the browser’s own element |
accent-color | the control, native | Tints the browser’s own radio and its focus: the least CSS |
inline-size, block-size | the control | 1.5rem, which is 24px. A swatch is 2rem |
box-sizing: border-box | the control | The border does not make it bigger: the 7px ring of on is inside the same 24px |
border | the control | 2px ink when off, 0.4375rem (7px) lilac when on: the ring is thicker, not another colour |
border-radius: 50% | the control | A circle |
background-color | the control | white, wash under a pointer, and the colour of a swatch |
outline, outline-offset | an on swatch | A 3px ink ring 2px outside the colour, so the colour is not made smaller by being chosen. The site’s focus halo is a box-shadow, so both show |
position: absolute, clip-path: inset(50%) | the label, swatchOnly | A box of 1px that is not seen. It is still the name of the radio in the tree |
:checked, :disabled, :focus-visible | the control | The states, drawn from the browser’s own state: no class is added and no script runs |
:disabled + label | the label | Mutes the words of a choice that is unavailable |
@media (hover: hover), :has(+ label:hover) | the control | The hover fill exists only where there is a pointer, and follows the label under it |
@media (forced-colors: active) | the control | The ring is Highlight. A swatch has forced-color-adjust: none, so it keeps its colour |
display: grid, grid-template-columns: auto 1fr | the group | The control in the first column, its label and the line under it in the second: they are side by side in the DOM, so the grid puts each pair on a row |
min-block-size: 2.75rem | the label | A row is 44px at least: the label is the click target |
cursor | the label | pointer, and not-allowed for a choice that is unavailable |
grid-template-columns: repeat(n, auto auto) | the group, inline | A pair after a pair on one row. Under 40em, in text size and not in pixels, one on each row |
display: flex, flex-wrap: wrap | the group, swatchOnly | The colours on a row that wraps |
Styling
.rd__input {
box-sizing: border-box;
inline-size: 1.5rem;
block-size: 1.5rem;
margin: 0;
cursor: pointer;
}
.rd__input--painted {
appearance: none;
border: 2px solid var(--ink);
border-radius: 50%;
background-color: var(--white);
}
.rd__input--painted:checked {
border: 0.4375rem solid var(--lilac);
}
.rd__input--native {
accent-color: var(--lilac);
}
.rd {
display: grid;
grid-template-columns: auto 1fr;
align-items: center;
}
.rd__label {
display: flex;
align-items: center;
min-block-size: 2.75rem;
padding-inline-start: 0.75rem;
cursor: pointer;
}States
| State | Look | Exposed |
|---|---|---|
| Off | 2px ink edge, white fill, 24px | not :checked |
| Hover | wash fill, only under a pointer | the same |
| On | 7px lilac ring around a white centre | :checked |
| Focus | the site’s ring: its halo around the control | :focus-visible. The edge keeps its colour |
| Disabled | dashed ink-soft edge, ink-soft words, not-allowed cursor | disabled |
| On, a swatch | the colour whole inside its 2px edge, and a 3px ink ring outside it | :checked |
| On, forced colors | Highlight ring, 7px | the same |
Measurements
| Part | Property | Value |
|---|---|---|
| Control | inline-size, block-size | 24px, and 32px for a swatch |
| Control, off | border-width | 2px |
| Control, on | border-width | 7px, which leaves a centre of 10px |
| Swatch, on | outline-width, outline-offset | 3px and 2px, outside the 32px of colour, which is the same as off |
| Swatch only | the colour | 32px by 32px, which is the target: the label is hidden |
| Label | min-block-size | 44px, which is the row. 32px when a line is under it |
| Label | padding-inline-start | 12px, between the control and the words, and part of the click target |
| Legend | font-size, font-weight | 16px, 700 |
| Label | font-size, font-weight | 16px, 400. With a line under it the label is 700, and the line is 15px, 400 |
Where it differs from the Figma
| Thing | The Figma page “Radio groups” | The component |
|---|---|---|
| Focus | A 3px ring in #1B4FD1, 2px away from the control, on the card | The site’s ring, on the control itself: a simple radio has no card |
| Thickness when on | The Look table says 0.45rem; the drawing is 7px | 7px (0.4375rem), as drawn |
| Forced colors | A black fill and a ring in #1A80FF | The ring is Highlight, the system’s own |
| Hover | None for the control: the card has a darker border | A wash fill, only under a pointer |
| Colour name | The Swatch row of the use cases says “Visible colour name”. Section 7b, Colour only, has the label visually hidden | Both: seen by default, and hidden with swatchOnly |
| Swatch, on | Section 7b: the colour whole, with a 3px ink ring 2px outside it | The same |
| Swatch, focus | Section 7b: the page’s 3px ring, 2px outside the ring of the choice | The site’s halo, under the ring of the choice: both show |
| Disabled, native, inline | Not drawn, except a disabled colour in section 7b | Added: a sold out size, the least CSS and a row of short words need them |
Tokens
Component tokens
| Part | Property | Token | Value |
|---|---|---|---|
| Control, off | border-color | ink | #16161a |
| Control, off | background-color | white | #ffffff |
| Control, off, hover | background-color | wash | #edebf4 |
| Control, on | border-color | lilac | #5e548e |
| Control, disabled | border-color, words | ink-soft | #4b4b55 |
| Swatch | border-color, outline-color when on | ink | #16161a |
| Words | color | ink | #16161a |
| Hint, line under a choice | color | ink-soft | #4b4b55 |
| Native | accent-color | lilac | #5e548e |
| Focus | box-shadow | focus-ring-soft, focus-ring-primary | #a5c8ff, #0066ff |
Guidelines
Radio or something else?
| Need | Use |
|---|---|
| Exactly one of a short list, two to about six | Radios |
| More than one can be chosen | Checkboxes in a fieldset |
| One yes or no on its own | A checkbox, or a switch when it acts at once |
| A long list, about seven or more | A select |
| Choosing acts at once: navigate, filter, apply | Buttons, links or tabs |
| Each choice has an image, a price or controls of its own | A radio card, which is its own build: read the post |
Content
| Part | Format | Example |
|---|---|---|
| Legend | The question or the noun: no colon, and no “Choose” | “How often should we deliver?”, “Size” |
| Label | The choice, in few words, never repeating the legend | “Every two weeks”, “M” |
| Hint | One line for the whole group | “You can change it at any time.” |
| Line under a choice | One line more for that choice: a price, a date | “€6.00. Arrives tomorrow.” |
| Unavailable | The reason in the label, so it is read with the name | “XL, sold out” |
| Colour without words | The label visually hidden, with the same words as when it is seen | “Sage” |
| First choice | Made for the person when one is a fair default. None when both answers are a choice | “Every two weeks”; yes or no has none |
Hidden names
Hide the names with swatchOnly only where the colour is the choice, as a row of colours for a product. The name is still the name of the radio for a screen reader, and the ring shows which is chosen. A person who talks to the page cannot say a name they cannot see, so show the names where that matters. Anything that is not a colour keeps its words.
Behavior
| Event | Result |
|---|---|
| Choose, with a pointer or Space | the others turn off and the page is told once. Nothing else happens: no navigation, no submit, no dialog (3.2.2) |
| Tab | enters the group once, on the chosen radio, or on the first when none is. The next Tab leaves it |
| Arrow keys | move focus and choose in one step, and wrap at the ends |
| Space on the radio that is chosen | nothing, and the page is not told |
| A radio that is disabled | is skipped by the arrow keys and by Tab, and is not in the form |
required with none chosen | the form is not valid. The message is the form’s: this component shows none |
Accessibility
Keyboard
| Key | Action | Native |
|---|---|---|
| Tab, Shift+Tab | one stop for the whole group: the chosen radio, or the first when none is | yes |
| ArrowDown, ArrowRight | the next radio, which is chosen. After the last, the first | yes |
| ArrowUp, ArrowLeft | the radio before, which is chosen. Before the first, the last | yes |
| Space | chooses the radio that has focus, when it is not chosen | yes |
Roles and properties
| Property | Exposed | Note |
|---|---|---|
| role of the group | group | named by the legend |
| role of a choice | radio | implicit |
| name | the text of the label | the words of the choice, when it has a line more. Not the line, and not the paint |
| description | the hint, for the group, and the line, for a choice | aria-describedby |
checked | true or false | from the browser: there is no aria-checked |
disabled | true | a choice that is unavailable |
required | on each radio | the legend says it in words as well |
Measured behavior
Chromium 148.0.7778.96, from the tests. Nothing here was heard in a screen reader yet.
| Observation | Consequence |
|---|---|
The browser’s tree has a group named by the legend, and a radio for each choice with its name, its description and checked | no ARIA is needed for any of it |
With aria-describedby, the name is “Standard”, from its label, and the description is the line | the price is not part of the name |
A sold out radio is in the tree, disabled, with its reason in its name | it is found and understood, not hidden |
| Tab lands on the chosen radio, or on the first when none is chosen | one stop for the group, as native radios do |
| The arrow keys wrap, and skip a radio that is disabled | no script is needed, and none is wanted |
| Space on a radio that is already chosen fires no change | the page is told once for each choice |
The site’s rule for an input sets border-color on focus | the component sets its own on :focus-visible, so on is still the ring of the lilac |
| The colour of a swatch is not in the name | the name is the colour: “Sage”, not a dot |
With swatchOnly the name is the label that is visually hidden: “Sage”, “Indigo” | a screen reader gets the same words as when they are seen |
| A colour that is chosen keeps its 32px and its 2px edge, and the ring is an outline outside it | the colour is whole, and the focus halo and the ring are both there |
Support
| Browser | Checked |
|---|---|
| Chromium 148 | yes, in the tests |
| WebKit, Firefox | not run |
WCAG criteria
| Criterion | Level | Requirement here |
|---|---|---|
| 1.3.1 Info and Relationships (opens in a new tab) | A | fieldset and legend make the group, the label names each radio, aria-describedby carries the hint and the line |
| 1.4.1 Use of Color (opens in a new tab) | A | on is a thicker ring, not another colour, and the colour of a swatch is also its name |
| 1.4.6 Contrast (Enhanced) (opens in a new tab) | AAA | the words are 18.0 to 1, and the hint and the line are 8.6 to 1, over white |
| 1.4.10 Reflow (opens in a new tab) | AA | no sideways scroll at 320px with the text at 200%: the words wrap |
| 1.4.11 Non-text Contrast (opens in a new tab) | AA | the edge is 18.0 to 1 and the ring of on is 6.7 to 1 against the page. A colour has an edge of 18.0 to 1 and a ring of 18.0 to 1, so a pale colour is found |
| 2.1.1 Keyboard (opens in a new tab) | A | native radios: Tab, the arrow keys and Space |
| 2.4.7 Focus Visible (opens in a new tab) | AA | the site’s ring is shown by :focus-visible |
| 2.5.3 Label in Name (opens in a new tab) | A | the name is the visible label. With swatchOnly there is none to differ from |
| 2.5.8 Target Size (Minimum) (opens in a new tab) | AA | the row is 44px high and the control is 24px |
| 3.2.2 On Input (opens in a new tab) | A | choosing changes no context |
| 3.3.2 Labels or Instructions (opens in a new tab) | A | the legend, “(required)” and the hint are words on the page |
| 4.1.2 Name, Role, Value (opens in a new tab) | A | a native radio, with its name and its checked state |
Related
This section is still in progress.
References
W3C standards
- Web Content Accessibility Guidelines (WCAG) 2.2 (opens in a new tab)
- ARIA in HTML (opens in a new tab)
HTML Standard (WHATWG)
- The input element: radio button state (opens in a new tab)
- The fieldset element (opens in a new tab)
- The legend element (opens in a new tab)
- The label element (opens in a new tab)
W3C WAI
- ARIA Authoring Practices Guide: Radio Group pattern (opens in a new tab)
- Forms tutorial: Grouping controls (opens in a new tab)
- Forms tutorial: Labeling controls (opens in a new tab)
- Technique H44: using label elements to associate text labels with form controls (opens in a new tab)
- Technique H71: describing a group of form controls with fieldset and legend (opens in a new tab)
MDN Web Docs
- <input type="radio"> (opens in a new tab)
- <fieldset> (opens in a new tab)
- <legend> (opens in a new tab)
- <label> (opens in a new tab)
- accent-color (opens in a new tab)
- appearance (opens in a new tab)
- outline (opens in a new tab)
- :checked (opens in a new tab)
- :focus-visible (opens in a new tab)
- :has() (opens in a new tab)
- forced-colors (opens in a new tab)
- forced-color-adjust (opens in a new tab)