Skip to content

Checkbox Example (Mixed-State)

Checkbox Example (Mixed-State)

Read This First

The code in this example is not intended for production environments. Before using it for any purpose, read this to understand why.

This is an illustrative example of one way of using ARIA that conforms with the ARIA specification.

About This Example

This example demonstrates using the Checkbox Pattern to create a tri-state, or mixed-state, checkbox. In this implementation, the mixed-state checkbox represents the state of a set of standard HTML checkboxes. If none of the checkboxes in the set are checked, the mixed state checkbox is not checked, and if all members of the set are checked, the mixed state checkbox is checked. If the set contains both some checked and unchecked checkboxes, the mixed state checkbox is partially checked. Activating the tri-state checkbox changes the states of the checkboxes in the set.

Similar examples include:

Example

Sandwich Condiments

Accessibility Features

  • To help assistive technology users understand that the checkboxes are all part of a group of related checkboxes named Sandwich Condiments, the checkboxes are wrapped in a fieldset element which is labeled by a legend element.
  • 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 currentcolor value for the stroke property of the rect and polyline elements used to draw the checkbox. To make the background of the checkbox graphics match the high contrast background color, the fill-opacity attribute of the rect element is set to zero. If specific colors were instead used to specify the stroke and fill properties, 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 CSS forced-color-adjust property set to auto for the currentcolor value to be updated in high contrast mode. Some browsers do not use auto for the default value.

Keyboard Support

Key Function
Tab Moves keyboard focus among the checkboxes.
Space
  • Cycles the tri-state checkbox among unchecked, mixed, and checked states.
  • When the tri-state checkbox is unchecked, all the controlled checkboxes are unchecked.
  • When the tri-state checkbox is mixed, the controlled checkboxes return to the last combination of states they had when the tri-state checkbox was last mixed or to the default combination of states they had when the page loaded.
  • When the tri-state checkbox is checked, all the controlled checkboxes are checked.

Role, Property, State, and Tabindex Attributes

Role Attribute Element Usage
checkbox div
  • Identifies the div element as a checkbox.
  • The child text content of this div provides the accessible name of the checkbox.
tabindex="0" div Includes the checkbox in the page tab sequence.
aria-controls="[IDREFS]" div identifies the set of checkboxes controlled by the mixed checkbox.
aria-checked="false" div
  • Indicates the tri-state checkbox is not checked.
  • In this state, all controlled checkboxes are unchecked.
  • CSS attribute selectors (e.g. [aria-checked="false"]) are used to synchronize the visual states with the value of the aria-checked attribute.
  • To support operating system and browser high contrast settings, the CSS ::before pseudo element and content property are used to generate the visual indicators of the checkbox state.
aria-checked="true" div
  • Indicates the tri-state checkbox is checked.
  • In this state, all controlled checkboxes are checked.
  • CSS attribute selectors (e.g. [aria-checked="true"]) are used to synchronize the visual states with the value of the aria-checked attribute.
  • To support operating system and browser high contrast settings, the CSS ::before pseudo element and content property are used to generate the visual indicators of the checkbox state.
aria-checked="mixed" div
  • Indicates the tri-state checkbox is mixed.
  • In this state, some controlled checkboxes are unchecked and some are checked.
  • CSS attribute selectors (e.g. [aria-checked="mixed"]) are used to synchronize the visual states with the value of the aria-checked attribute.
  • To support operating system and browser high contrast settings, the CSS ::before pseudo element and content property are used to generate the visual indicators of the checkbox state.

JavaScript and CSS Source Code

HTML Source Code

To copy the following HTML code, please open it in CodePen.

Back to Top