Design System

Design System

Checkbox

Forms

Open in Storybook: Checkbox (opens in a new tab)

On this page

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

Herbs for the seed box

Anatomy

  1. Group

    fieldset

    The group. It is named by its legend and described by its hint. A box on its own has none.

  2. Legend

    legend

    First child. The question the boxes answer: the name of the group.

  3. Hint

    p

    Optional. One line for the whole group, read after its name with aria-describedby on the fieldset.

  4. Control

    input type="checkbox"

    The browser’s own element, painted. One name for the boxes of a group, and the value is what a form sends for one that is checked.

  5. Label

    label

    Next to the control, never around it, with for set to its id. Its words are the name of the box, and the whole row is the click target.

  6. Description

    p

    Optional. A line more for one choice, read after its name with aria-describedby on the input.

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 · label with for

    Checked, 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

    Herbs for the seed box

    Choose as many as you like.

    fieldset · legend · one name · disabled on the one that is sold out

    Each 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

    Extras for your order

    €4.00. Kraft paper and string.

    €2.00. Up to 20 words.

    €3.00. Chosen for you.

    aria-describedby on the input · the line under its label

    The 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

    Weekend tasks

    selectAll · indeterminate · no name on the first box

    It 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 the label · :user-invalid · validity.valueMissing

    The 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

    Delivery days (choose at least one)

    atLeastOne · setCustomValidity() on the first box · validity.customError

    The 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

    Herbs for the seed box

    accent-color: var(--lilac) · no appearance: none

    The least CSS: the browser draws the box, the check, the dash and the focus, and only the tint is ours.

Props

Props of CheckboxGroup
PropDoesDefault
legendNames the group: the question the boxes answerRequired
nameThe one name of every box: the key of the values in a formRequired
optionsThe choices: value, label, and optionally description and disabledRequired
hintOne line for the whole group, read after its name. Also on the page
defaultValueThe values that are checked to begin with. A reset goes back to them
value, onValueChangeFor a page that owns the state: the values that are checked, and a call with the new list, in the order of the options
selectAllThe 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
atLeastOneWhat the browser says when the form is sent with none checked. A group cannot be required: that would ask for every box
inlineSide by side, wrapping, for choices that are one short wordfalse
nativeThe browser’s own drawing, tinted with accent-colorfalse
Props of Checkbox
PropDoesDefault
labelWhat the box says yes to: the text of the labelRequired
nameThe name of the boxRequired
valueWhat the form sends when it is checkedon, the browser’s own
defaultCheckedChecked to begin with. A reset goes back to itfalse
checked, onCheckedChangeFor a page that owns the state
indeterminateA dash, which the page decides. It does not change what the form sends, and a click clears itfalse
requiredThe form needs it checked, and the label says “(required)”false
disabledOut of the Tab order and out of the form. It stays in sightfalse
nativeThe browser’s own drawing, tinted with accent-colorfalse

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

Native tags
TagAttributeWhat it gives
fieldsetnoneThe group: the role group, named by its legend. Its own edge and padding are cleared
legendfirst child of the fieldsetThe name of the group: the question
inputtype="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
inputnameThe 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
inputidWhat the label points at with for
inputvalueWhat the form sends for a box that is checked. on when there is none
inputcheckedChecked to begin with. Any number of the boxes of a group can have it, and a reset goes back to it
inputrequiredThe 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
inputdisabledOut of the Tab order and out of the form. It stays in sight
labelfor, next to the inputThe name of the box, and the click target of the row. It is never around the input
fieldsetaria-describedbyThe hint, read after the name of the group
inputaria-describedbyOnly when a choice has a line more: it points at the line under the label
inputthe property indeterminateA 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
inputsetCustomValidity()The message for at least one: on the first box that can be checked, while none is. Empty, it is valid

Native CSS properties

Native CSS properties
PropertyOnWhat it does here
appearance: nonethe boxTakes the browser’s drawing off, so the box is painted with CSS. It is still the browser’s own element
accent-colorthe box, nativeTints the browser’s own box, its check, its dash and its focus: the least CSS
inline-size, block-sizethe box1.5rem, which is 24px
box-sizing: border-boxthe boxThe border does not make it bigger
border, border-radiusthe boxA 2px ink edge with 6px corners, and the lilac edge when it is on
background-colorthe boxwhite when it is off. lilac when it is on or a dash
display: grid, place-content: centerthe boxPuts the mark in the middle
::beforethe boxThe check or the dash, drawn with borders and not with a picture, so forced colors take it
rotate, translatethe checkTurns its two borders 45 degrees and sets it straight
:checkedthe boxThe lilac fill and the check
:indeterminatethe boxThe lilac fill and the dash. It is written after :checked, so a box that is both has the dash
:disabledthe box and its labelA dashed ink-soft edge and muted words. A box that is on and disabled keeps its fill
:user-invalidthe 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 boxThe wash fill under a pointer is only where there is a pointer
@media (forced-colors: active)the boxThe edge of one that is on takes Highlight. The mark is in the text colour, so it is there
display: grid, grid-template-columnsthe groupThe 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-sizethe label2.75rem, which is 44px: a row, and the click target

States

States
StateLookExposed
Off2px ink edge, white fill, 24pxchecked: false
Hoverwash fill, only under a pointerthe same
Onlilac fill and edge, a white checkchecked: true, :checked
Dashlilac fill and edge, a white dashchecked: mixed, :indeterminate
Focusthe site’s ring: its halo around the box. The edge keeps its colour:focus-visible
Disableddashed ink-soft edge, ink-soft words, not-allowed cursordisabled: true
Disabled and onsolid ink-soft fill, a white checkdisabled: true, checked: true
Invalida 3px danger ring outside the box, 2px away. The group has one round itinvalid: true, :user-invalid
On, forced colorsthe edge in Highlight, the check in the text colourthe same

Measurements

Measurements
PartPropertyValue
Boxinline-size, block-size24px
Boxborder-width, border-radius2px and 6px
Checkinline-size, block-size, border-width7px by 13px, in 3px borders, turned 45 degrees
Dashinline-size, border-width13px by 3px
Labelmin-block-size44px, which is the row. 32px when a line is under it
Labelpadding-inline-start12px, between the box and the words, and part of the click target
Select allthe boxes of the choicesstart under the words of its label: 36px from the start of the group
Legendfont-size, font-weight16px, 700
Labelfont-size, font-weight16px, 400. With a line under it the label is 700, and the line is 15px, 400

Tokens

Component tokens

Component tokens
PartPropertyTokenValue
Box, offborder-colorink#16161a
Box, offbackground-colorwhite#ffffff
Box, off, hoverbackground-colorwash#edebf4
Box, on or dashbackground-color, border-colorlilac#5e548e
Check, dashcolor of the borderswhite#ffffff
Box, disabledborder-color, background-color when on, wordsink-soft#4b4b55
Invalidoutline-colordanger#82002a
Wordscolorink#16161a
Hint, line under a choicecolorink-soft#4b4b55
Nativeaccent-colorlilac#5e548e
Focusbox-shadowfocus-ring-soft, focus-ring-primary#a5c8ff, #0066ff

Guidelines

Checkbox or something else?

Checkbox or something else?
NeedUse
One yes or no on its own, such as the terms of a formA checkbox on its own
Several of a short list can be checkedCheckboxes in a fieldset
Exactly one of a short list, two to about sixRadios
Several of a long list, about seven or moreCheckboxes in a fieldset still, with a hint. Never select multiple: one arrow key replaces the whole choice
Choosing acts at once: navigate, filter, applyButtons, links or tabs

Content

Content
PartFormatExample
LegendThe 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)”
LabelWhat 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)”
HintOne line for the whole group“Choose as many as you like.”
Line under a choiceOne line more for that choice: a price, a date“€4.00. Kraft paper and string.”
UnavailableThe reason in the label, so it is read with the name“Thyme, sold out”
At least oneThe message says the fix, once“Choose at least one delivery day.”
Select allWhat the group is, in two words“All tasks”

Behavior

Behavior
EventResult
Toggle, with a pointer or Spacethe 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)
Tabone stop for each box, in the order of the page. A box that is disabled is skipped
Arrow keys, Home, Endnothing: the box and the focus stay, and the page scrolls
Enterdoes not toggle. It sends a form that has a submit button, unless a required box is unchecked
Select all, from a dash or from offchecks every choice that can be changed. From on, it clears them. A choice that is disabled keeps what it has
atLeastOne with none checkedthe 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 resetgoes back to the boxes that were checked to begin with
required and not checkedthe form is not valid, and the browser says why. This component shows no message of its own

Accessibility

Keyboard

Keyboard
KeyActionNative
Tab, Shift+Taba stop for each box that can be checked, in the order of the pageyes
Spacetoggles the box that has focusyes
Enterdoes not toggle. It sends a form that has a submit button, unless a required box is uncheckedyes
Arrow keys, Home, Endnothing: the box and the focus stay, and the page scrollsyes

Roles and properties

Roles and properties
PropertyExposedNote
role of the groupgroupnamed by the legend
role of a boxcheckboximplicit
namethe text of the labelthe words of the choice, with “(required)” when it is. Not the line under it
descriptionthe hint, for the group, and the line, for a choicearia-describedby
checkedtrue, false or mixedfrom the browser: there is no aria-checked. mixed is a dash
disabledtruea box that is unavailable
invalidtruea required box that is not checked, and the first box of a group with atLeastOne and none checked
requirednot exposed in Chromium 148the label says “(required)” in words, which is where the tree finds it

Support

Support
BrowserChecked
Chromium 148yes, in the tests
WebKit 26.4the selectors and the accessibility tree, by hand and not in the tests
Firefoxnot run

WCAG criteria

WCAG criteria
CriterionLevelRequirement here
1.3.1 Info and Relationships (opens in a new tab)Afieldset 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)Aon 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)AAAthe 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)AAno sideways scroll at 320px with the text at 200%: the words wrap
1.4.11 Non-text Contrast (opens in a new tab)AAthe 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)Anative boxes: Tab and Space
2.4.7 Focus Visible (opens in a new tab)AAthe site’s ring is shown by :focus-visible
2.5.3 Label in Name (opens in a new tab)Athe name is the visible label, with “(required)” when it is
2.5.8 Target Size (Minimum) (opens in a new tab)AAthe row is 44px high and the box is 24px
3.2.2 On Input (opens in a new tab)Atoggling changes no context
3.3.1 Error Identification (opens in a new tab)Athe 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)Athe legend, “(required)” and the hint are words on the page
3.3.3 Error Suggestion (opens in a new tab)AAat least one says the fix: “Choose at least one delivery day.”
4.1.2 Name, Role, Value (opens in a new tab)Aa native checkbox, with its name and its checked or mixed state

Related

This section is still in progress.

References

W3C standards

HTML Standard (WHATWG)

W3C WAI

MDN Web Docs