Checkbox
Forms
Open in Storybook: Checkbox (opens in a new tab)
Overview
One answer that is on or off, or several that can be on together: yes to a term, a few extras, a list of tasks. A box on its own is an input type="checkbox" and its label, side by side and tied by for and id. Several that answer one question are in a fieldset with a legend. The browser gives the role, the checked state, a Tab stop for each box, Space to toggle it and the value in a form. The component adds paint, a box that checks them all and a message for none checked.
The rule. One name for the boxes of a group. Never an input inside its label. No role="checkbox", no aria-checked, no tabindex, no ul, and no script for the keys. The legend is the question and the label is the choice. Toggling a box 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. A box on its own has none.
- Legend
legendFirst child. The question the boxes 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="checkbox"The browser’s own element, painted. One
namefor the boxes of a group, and thevalueis what a form sends for one that is checked. - Label
labelNext to the control, never around it, with
forset to itsid. Its words are the name of the box, 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 form around them change.
One yes or no
A box on its own, for one answer
input type="checkbox"·name·value·labelwithforChecked, it sends
updates=weekly. Unchecked, it sends nothing at all.Several, in a group
A short list where any number can be checked, and one that is gone
fieldset·legend· onename·disabledon the one that is sold outEach box is a Tab stop of its own and Space toggles it. The sold out one stays in sight and Tab skips it.
A choice with a line more
A price 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.
Select all
A first box that checks them all, and shows a dash when only some are
selectAll·indeterminate· nonameon the first boxIt is checked when all are, dashed when some are, and a click on a dashed one checks them all. The form does not send it.
Required
One box that has to be checked, and the form that sends it
required· “(required)” in thelabel·:user-invalid·validity.valueMissingThe browser stops the form and says why, in words. The box gets a ring once the form has been sent.
At least one of a group
A group cannot be required, so the first box carries the message while none is checked
atLeastOne·setCustomValidity()on the first box ·validity.customErrorThe form is not sent with none checked, and the browser says “Choose at least one delivery day.” The message goes as soon as one is checked.
Only accent-color
The browser’s own box, tinted
accent-color: var(--lilac)· noappearance: noneThe least CSS: the browser draws the box, the check, the dash and the focus, and only the tint is ours.
Props
| Prop | Does | Default |
|---|---|---|
legend | Names the group: the question the boxes answer | Required |
name | The one name of every box: the key of the values in a form | Required |
options | The choices: value, label, and optionally description and disabled | Required |
hint | One line for the whole group, read after its name. Also on the page | |
defaultValue | The values that are checked to begin with. A reset goes back to them | |
value, onValueChange | For a page that owns the state: the values that are checked, and a call with the new list, in the order of the options | |
selectAll | The label of a first box that checks every choice and clears them, with a dash when only some are checked. It has no name, so a form does not send it | |
atLeastOne | What the browser says when the form is sent with none checked. A group cannot be required: that would ask for every box | |
inline | Side by side, wrapping, for choices that are one short word | false |
native | The browser’s own drawing, tinted with accent-color | false |
| Prop | Does | Default |
|---|---|---|
label | What the box says yes to: the text of the label | Required |
name | The name of the box | Required |
value | What the form sends when it is checked | on, the browser’s own |
defaultChecked | Checked to begin with. A reset goes back to it | false |
checked, onCheckedChange | For a page that owns the state | |
indeterminate | A dash, which the page decides. It does not change what the form sends, and a click clears it | false |
required | The form needs it checked, and the label says “(required)” | false |
disabled | Out of the Tab order and out of the form. It stays in sight | false |
native | The browser’s own drawing, tinted with accent-color | false |
Specs
Markup
<input type="checkbox" id="updates" name="updates">
<label for="updates">Email me the weekly updates</label>
<fieldset>
<legend>Extras for your order</legend>
<input type="checkbox" id="wrap" name="extra" value="wrap">
<label for="wrap">Gift wrap</label>
<input type="checkbox" id="note" name="extra" value="note">
<label for="note">Handwritten note</label>
</fieldset>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="checkbox" | The control: the role checkbox, a checked state of true, false or mixed, a Tab stop for each box and Space to toggle it |
input | name | The same on the boxes of a group. It is the key of the values in a form, and a box that is not checked sends nothing |
input | id | What the label points at with for |
input | value | What the form sends for a box that is checked. on when there is none |
input | checked | Checked to begin with. Any number of the boxes of a group can have it, and a reset goes back to it |
input | required | The form is not valid until it is checked. For a box on its own: a group cannot be required, it would ask for every box |
input | disabled | Out of the Tab order and out of the form. It stays in sight |
label | for, next to the input | The name of the box, 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 |
input | the property indeterminate | A dash, mixed in the tree. It has no attribute: a script sets it. It is a look, and does not change what the form sends |
input | setCustomValidity() | The message for at least one: on the first box that can be checked, while none is. Empty, it is valid |
Native CSS properties
| Property | On | What it does here |
|---|---|---|
appearance: none | the box | Takes the browser’s drawing off, so the box is painted with CSS. It is still the browser’s own element |
accent-color | the box, native | Tints the browser’s own box, its check, its dash and its focus: the least CSS |
inline-size, block-size | the box | 1.5rem, which is 24px |
box-sizing: border-box | the box | The border does not make it bigger |
border, border-radius | the box | A 2px ink edge with 6px corners, and the lilac edge when it is on |
background-color | the box | white when it is off. lilac when it is on or a dash |
display: grid, place-content: center | the box | Puts the mark in the middle |
::before | the box | The check or the dash, drawn with borders and not with a picture, so forced colors take it |
rotate, translate | the check | Turns its two borders 45 degrees and sets it straight |
:checked | the box | The lilac fill and the check |
:indeterminate | the box | The lilac fill and the dash. It is written after :checked, so a box that is both has the dash |
:disabled | the box and its label | A dashed ink-soft edge and muted words. A box that is on and disabled keeps its fill |
:user-invalid | the box, and the group through :has() | A 3px danger ring outside the box, and round all of the group, once the person has been at it or the form was sent |
:has() | the box and the group | :has(+ .cb__label:hover) fills the box while its label is hovered. fieldset:has(:user-invalid) rings the group |
@media (hover: hover) | the box | The wash fill under a pointer is only where there is a pointer |
@media (forced-colors: active) | the box | The edge of one that is on takes Highlight. The mark is in the text colour, so it is there |
display: grid, grid-template-columns | the group | The box in the first column and the label in the second, so each pair is a row. A select all has three: the boxes under its words |
min-block-size | the label | 2.75rem, which is 44px: a row, and the click target |
States
| State | Look | Exposed |
|---|---|---|
| Off | 2px ink edge, white fill, 24px | checked: false |
| Hover | wash fill, only under a pointer | the same |
| On | lilac fill and edge, a white check | checked: true, :checked |
| Dash | lilac fill and edge, a white dash | checked: mixed, :indeterminate |
| Focus | the site’s ring: its halo around the box. The edge keeps its colour | :focus-visible |
| Disabled | dashed ink-soft edge, ink-soft words, not-allowed cursor | disabled: true |
| Disabled and on | solid ink-soft fill, a white check | disabled: true, checked: true |
| Invalid | a 3px danger ring outside the box, 2px away. The group has one round it | invalid: true, :user-invalid |
| On, forced colors | the edge in Highlight, the check in the text colour | the same |
Measurements
| Part | Property | Value |
|---|---|---|
| Box | inline-size, block-size | 24px |
| Box | border-width, border-radius | 2px and 6px |
| Check | inline-size, block-size, border-width | 7px by 13px, in 3px borders, turned 45 degrees |
| Dash | inline-size, border-width | 13px by 3px |
| Label | min-block-size | 44px, which is the row. 32px when a line is under it |
| Label | padding-inline-start | 12px, between the box and the words, and part of the click target |
| Select all | the boxes of the choices | start under the words of its label: 36px from the start of the group |
| 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 |
Tokens
Component tokens
| Part | Property | Token | Value |
|---|---|---|---|
| Box, off | border-color | ink | #16161a |
| Box, off | background-color | white | #ffffff |
| Box, off, hover | background-color | wash | #edebf4 |
| Box, on or dash | background-color, border-color | lilac | #5e548e |
| Check, dash | color of the borders | white | #ffffff |
| Box, disabled | border-color, background-color when on, words | ink-soft | #4b4b55 |
| Invalid | outline-color | danger | #82002a |
| 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
Checkbox or something else?
| Need | Use |
|---|---|
| One yes or no on its own, such as the terms of a form | A checkbox on its own |
| Several of a short list can be checked | Checkboxes in a fieldset |
| Exactly one of a short list, two to about six | Radios |
| Several of a long list, about seven or more | Checkboxes in a fieldset still, with a hint. Never select multiple: one arrow key replaces the whole choice |
| Choosing acts at once: navigate, filter, apply | Buttons, links or tabs |
Content
| Part | Format | Example |
|---|---|---|
| Legend | The question or the noun: no colon. Where none can be a valid answer, the limit in words, because a group cannot be required | “Herbs for the seed box”, “Delivery days (choose at least one)” |
| Label | What is chosen, in few words, never repeating the legend | “Basil”, “Email me the weekly updates” |
| Required | “(required)” at the end of the label, in words | “I accept the club rules (required)” |
| Hint | One line for the whole group | “Choose as many as you like.” |
| Line under a choice | One line more for that choice: a price, a date | “€4.00. Kraft paper and string.” |
| Unavailable | The reason in the label, so it is read with the name | “Thyme, sold out” |
| At least one | The message says the fix, once | “Choose at least one delivery day.” |
| Select all | What the group is, in two words | “All tasks” |
Behavior
| Event | Result |
|---|---|
| Toggle, with a pointer or Space | the box turns on or off, click, input and change fire, and the page is told the values, in the order of the options. Nothing else happens: no navigation, no submit, no dialog (3.2.2) |
| Tab | one stop for each box, in the order of the page. A box that is disabled is skipped |
| Arrow keys, Home, End | nothing: the box and the focus stay, and the page scrolls |
| Enter | does not toggle. It sends a form that has a submit button, unless a required box is unchecked |
| Select all, from a dash or from off | checks every choice that can be changed. From on, it clears them. A choice that is disabled keeps what it has |
atLeastOne with none checked | the form is not valid, and the browser says the message at the first box that can be checked. It goes as soon as one is checked |
| A form that is reset | goes back to the boxes that were checked to begin with |
required and not checked | the form is not valid, and the browser says why. This component shows no message of its own |
Accessibility
Keyboard
| Key | Action | Native |
|---|---|---|
| Tab, Shift+Tab | a stop for each box that can be checked, in the order of the page | yes |
| Space | toggles the box that has focus | yes |
| Enter | does not toggle. It sends a form that has a submit button, unless a required box is unchecked | yes |
| Arrow keys, Home, End | nothing: the box and the focus stay, and the page scrolls | yes |
Roles and properties
| Property | Exposed | Note |
|---|---|---|
| role of the group | group | named by the legend |
| role of a box | checkbox | implicit |
| name | the text of the label | the words of the choice, with “(required)” when it is. Not the line under it |
| description | the hint, for the group, and the line, for a choice | aria-describedby |
checked | true, false or mixed | from the browser: there is no aria-checked. mixed is a dash |
disabled | true | a box that is unavailable |
invalid | true | a required box that is not checked, and the first box of a group with atLeastOne and none checked |
required | not exposed in Chromium 148 | the label says “(required)” in words, which is where the tree finds it |
Support
| Browser | Checked |
|---|---|
| Chromium 148 | yes, in the tests |
| WebKit 26.4 | the selectors and the accessibility tree, by hand and not in the tests |
| 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 box, aria-describedby carries the hint and the line, and checked and mixed come from the element |
| 1.4.1 Use of Color (opens in a new tab) | A | on is a check, a dash is a dash, and neither is a colour alone. Disabled is a dashed edge, and invalid is a ring with the browser’s words |
| 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 against the page, the fill of on is 6.7 to 1, and the white check on it is 6.7 to 1 |
| 2.1.1 Keyboard (opens in a new tab) | A | native boxes: Tab 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 “(required)” when it is |
| 2.5.8 Target Size (Minimum) (opens in a new tab) | AA | the row is 44px high and the box is 24px |
| 3.2.2 On Input (opens in a new tab) | A | toggling changes no context |
| 3.3.1 Error Identification (opens in a new tab) | A | the browser says in words that a required box is not checked, and a ring marks the box |
| 3.3.2 Labels or Instructions (opens in a new tab) | A | the legend, “(required)” and the hint are words on the page |
| 3.3.3 Error Suggestion (opens in a new tab) | AA | at least one says the fix: “Choose at least one delivery day.” |
| 4.1.2 Name, Role, Value (opens in a new tab) | A | a native checkbox, with its name and its checked or mixed 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: checkbox 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: Checkbox 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="checkbox"> (opens in a new tab)
- <fieldset> (opens in a new tab)
- <legend> (opens in a new tab)
- <label> (opens in a new tab)
- HTMLInputElement: indeterminate (opens in a new tab)
- HTMLInputElement: setCustomValidity() (opens in a new tab)
- accent-color (opens in a new tab)
- appearance (opens in a new tab)
- :checked (opens in a new tab)
- :indeterminate (opens in a new tab)
- :user-invalid (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)