Tri-State Checkbox With Grouping Label Examples
Following are three example implementations of the design pattern for checkbox that demonstrate how to make a widget that uses the mixed value for aria-checked.
These examples also show three different ways of labeling a collection of checkboxes so screen reader users can perceive that all members of the collection are part of a logical group.
In the below examples, a set of checkboxes is used to choose condiments for a sandwich.
The user can use the first checkbox, labeled "All Condiments", to quickly specify a desire for either all or none of the condiments.
If the user chooses to have some but not all condiments, the "All Condiments" checkbox will reflect that state as "partially checked"; this is the mixed value of aria-checked.
Examples
Note that the conditions and actions that cause screen readers to announce the grouping labels in the following 3 examples vary from one screen reader to another; such assistive technology behaviors are not standardized.
Example 1: group label using fieldset and legend
This is the preferred implementation whenever feasible.
In this implementation:
- The grouping label, "sandwich condiments", is defined by putting the group label in a
legendelement within afieldsetelement that wraps the set of checkboxes. - The tri-state checkbox is labeled by content, e.g., it comes from the child text nodes of the element with
role=checkbox. - The individual condiment checkboxes are labeled by wrapping the input and the labeling text in a
labelelement.
Example 2: Group Label Using role = group
All condiments
In this implementation:
- The grouping label, “sandwich condiments”, is set by applying
aria-labelto a div with rolegroup. - The tri-state checkbox is labeled by content, e.g., it comes from the child
text nodes of the element with
role=checkbox. - The individual condiment checkboxes are labeled by wrapping the input and the
labeling text in a
labelelement.
Example 3: Group Label Appended to Individual Checkbox Labels
All condiments
In this implementation:
- A grouping label is not defined. Instead, "sandwitch condiments" is
appended to the label for each checkbox by specifying multiple ID values in the
aria-labelledbyvalue. - It is important that the group label is the second ID reference so that the condiment label, (e.g., "lettuce", is announced before the "sandwitch condiments"group label.
- Because this approach forces screen readers to repeat “sandwich condiments” for every checkbox, which may be distracting for some users, this approach is less desirable, especially when grouping a large set of controls.
Common Accessibility Features
- The examples use the native HTML5 semantics for checkboxes when possible (e.g. the non-mixed state checkboxes) and is the preferred technique for defining checkbox inputs.
divelement with rolecheckboxidentifies thedivelement as a checkbox (e.g. All Condiments checkbox), which supports a mixed state.divelement with rolecheckboxhasonkeydownevent to support keyboard activation of the checkbox.divelement with rolecheckboxhasonclickevent to support mouse pointer activation of the checkbox.divelement with rolecheckboxhastabindex="0"to become part of the tab order of the page.-
imgelement is used to visually indicate the state of checkbox (e.g. checked, mixed, unchecked) to ensure the visual state will be visible when browsers or operating system use “High Contrast” settings by people with visual impairments. (Note: CSS background image techniques or generated content result in visual state disappearing when browsers or operating systems are configured to display high contrast themes.) toggleGroupCheckbox(event)usesaria-checkedattribute to determine which image is displayed to ensure the information communicated to asssitive tecnology is the same as the visual state (e.g.imgelement).onfocusandonblurevent handlers on both aria checkbox and standard HTML checkboxes support visual focus styling for keyboard only users.
Keyboard Support
| Key | Function |
|---|---|
| Tab | Moves keyboard focus to checkbox. |
| Space | Toggle the checkbox option either true, false or mixed. False is the default. |
ARIA Roles, Properties and States
| Role | Property/State | Usage |
|---|---|---|
| checkbox | Identify div as Checkbox widget | |
| aria-checked |
Indicate state of checkbox:
|
|
| aria-labelledby | Used to include grouping label in the label of each checkbox |
Javascript and CSS Source Code
- CSS: checkbox-3.css
- Javascript: checkbox-3.js