Switch Example Using HTML Checkbox Input
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.
- There may be support gaps in some browser and assistive technology combinations, especially for mobile/touch devices. Testing code based on this example with assistive technologies is essential before considering use in production systems.
- The ARIA and Assistive Technologies Project is developing measurements of assistive technology support for APG examples.
- Robust accessibility can be further optimized by choosing implementation patterns that maximize use of semantic HTML and heeding the warning that No ARIA is better than Bad ARIA.
About This Example
This example illustrates implementing the Switch Pattern with an HTML
input[type="checkbox"] as the switch element and using CSS borders to provide graphical rendering of switch states.
It also demonstrates using the HTML
legend elements to present multiple switches in a labeled group.
Similar examples include:
- Switch Example: A switch based on a
divelement that turns a notification preference on and off.
- Switch Example Using HTML Button: A group of 2 switches based on HTML
buttonelements that turn lights on and off.
- To help assistive technology users understand that each switch belongs to a set of switches related to
Accessibility Preferences, the switches are wrapped in a
fieldsetelement labeled with a
To make understanding the state of the switch easier for users with visual or cognitive disabilities, a text equivalent of the state (
off) is displayed adjacent to the graphical state indicator. CSS attribute selectors ensure the label displayed is synchronized with the value of the
NOTE: To prevent redundant announcement of the state by screen readers, the text indicators of state are hidden from assistive technologies with
Spacing, border widths and fill are important to ensure the graphical states are visible and discernible to people with visual impairments, including when browser or operating system high contrast settings are enabled:
- To make the graphical representation of the state of a switch readily perceivable, two pixel borders are used for the switch state container and a solid color is used for the fill of the circles indicating the on and off states.
- To ensure users can perceive the difference between the container and the circles used to indicate the state of the switch, there are two pixels of space between the container border and the circles.
To enhance perceivability when operating the switches, visual keyboard focus and hover are styled using the CSS
- To make it easier to perceive focus and the relationship between a label and its associated switch, focus creates a border around both the switch and the label and also changes the background color.
- To make it easier to perceive that clicking either the label or switch will activate the switch, the hover indicator is the same as the focus indicator.
- To help people with visual impairments identify the switch as an interactive element, the cursor is changed to a pointer when hovering over the switch.
- Note: Because transparent borders are visible on some systems with operating system high contrast settings enabled, transparency cannot be used to create a visual difference between the element that is focused an 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 zero to two pixels and padding is reduced by two pixels. When an element loses focus, its border changes from two pixels to two and padding is increased by two pixels.
|Tab||Moves keyboard focus to the
|Space||Toggles the state of the switch between on and off.|
Role, Property, State, and Tabindex Attributes
- CSS: switch-checkbox.css