third_party/aria-practices/src/content/patterns/switch/examples/switch-checkbox.html
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 fieldset and legend elements to present multiple switches in a labeled group.
Similar examples include:
div element that turns a notification preference on and off.button elements that turn lights on and off.Accessibility PreferencesReduced motion On Off Show captions On 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 fieldset element labeled with a legend element.
To make understanding the state of the switch easier for users with visual or cognitive disabilities, a text equivalent of the state (on or off) is displayed adjacent to the graphical state indicator. CSS attribute selectors ensure the label displayed is synchronized with the value of the input.
NOTE: To prevent redundant announcement of the state by screen readers, the text indicators of state are hidden from assistive technologies with aria-hidden.
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 enhance perceivability when operating the switches, visual keyboard focus and hover are styled using the CSS :hover pseudo-class and onfocus and onblur event handlers:
| Key | Function |
|---|---|
| Tab | Moves keyboard focus to the switch. |
| Space | Toggles the state of the switch between on and off. |
| Role | Attribute | Element | Usage |
|---|---|---|---|
switch | input[type="checkbox"] |
switch.label element.|
| | aria-hidden="true" | span.on and span.off |
|
To copy the following HTML code, please open it in CodePen.