Checkbox Example (Two State)
This example implements the Checkbox Design Pattern for a two state checkbox using div elements.
Similar examples include:
- Checkbox (Mixed-State): Mixed state checkbox controlling standard input checkboxes.
Example
Sandwich Condiments
- Lettuce
- Tomato
- Mustard
- Sprouts
Accessibility Features
- To help assistive technology users understand that each checkbox is part of a set of related checkboxes named
Sandwich Condiments
, the checkboxes are wrapped in agrouplabeled by theh3heading element. - To enable assistive technology users to perceive the set of checkboxes as a list of four items, each
divelement that serves as a checkbox is contained within alielement contained by aulelement. - To make it easier to perceive that clicking either the label or checkbox will activate the checkbox, when a pointer hovers over either the checkbox or label, the background color changes, a border appears, and the cursor changes to a pointer.
- Because transparent borders are visible on some systems when operating system high contrast settings are enabled, transparency cannot be used to create a visual difference between the element that is focused and other elements. Instead of using transparency, the focused element has a thicker border and less padding. When an element receives focus, its border changes from 0 to 2 pixels and padding is reduced by 2 pixels. When an element loses focus, its border changes from 2 pixels to 0 and padding is increased by 2 pixels.
-
To ensure the borders of the inline SVG checkbox graphics in the CSS have sufficient contrast with the background when high contrast settings invert colors, the color of the borders are synchronized with the color of the text content.
For example, the color of the checkbox borders is set to match the foreground color of high contrast mode text by specifying the CSS
currentColorvalue for thestrokeproperty of therectandpolylineelements used to draw the checkbox. To make the background of the checkbox graphics match the high contrast background color, thefill-opacityattribute of therectelement is set to zero. If specific colors were instead used to specify thestrokeandfillproperties, those colors would remain the same in high contrast mode, which could lead to insufficient contrast between the checkbox and the background or even make the checkbox invisible if the color matched the high contrast mode background.
Note: The SVG element needs to have the CSSforced-color-adjustproperty set toautofor thecurrentColorvalue to be updated in high contrast mode. Some browsers do not useautofor the default value.
Keyboard Support
| Key | Function |
|---|---|
| Tab | Moves keyboard focus to the checkbox. |
| Space | Toggles checkbox between checked and unchecked states. |
Role, Property, State, and Tabindex Attributes
| Role | Attribute | Element | Usage |
|---|---|---|---|
h3 |
|
||
group |
div |
|
|
aria-labelledby |
div |
References the id attribute of the h3 element to define the accessible name for the group of checkboxes. |
|
checkbox |
div |
|
|
tabindex="0" |
div |
Includes the checkbox in the page tab sequence. | |
aria-checked="false" |
div |
|
|
aria-checked="true" |
div |
|
Javascript and CSS Source Code
- CSS: checkbox.css
- Javascript: checkbox.js
HTML Source Code
Simple Two-State Checkbox Example