third_party/aria-practices/src/content/patterns/switch/examples/switch-button.html
This example illustrates implementing the Switch Pattern with an HTML button as a switch element and using an SVG element to provide graphical rendering of switch states. It also demonstrates using the group role to present multiple switches in a labeled group.
Similar examples include:
div element that turns a notification preference on and off.input[type="checkbox"] elements that turn accessibility preferences on and off.Living Room LightsOnOffOutdoor LightsOnOff
To help assistive technology users understand that the Environmental Controls are a group of two switches, the switches are wrapped in a group labeled by the heading that labels the set of switches.
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 aria-checked attribute.
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, stroke widths, and fill are important to ensure the graphical states will be 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 and :focus pseudo-classes:
To ensure the SVG graphics have sufficient contrast with the background when high contrast settings invert colors, the CSS currentcolor value for the stroke and fill properties is used to synchronize the colors with text content. If specific colors were used to specify the stroke and fill properties, the color of these elements would remain the same in high contrast mode, which could lead to insufficient contrast between them and their background or even make them invisible if their color were to match the high contrast mode background. The fill-opacity of the container rect is set to zero for the background color of the page to provide the contrasting color to the stroke and fill colors.
NOTE: The SVG elements need to set the CSS forced-color-adjust property to auto for some browsers to support the currentcolor value.
| Key | Function |
|---|---|
| Tab |
switch.| | Space, Enter |
|
| Role | Attribute | Element | Usage |
|---|---|---|---|
switch | button | Identifies the button element as a switch. | |
aria-checked="false" | button |
switch is off.[aria-checked="false"]) are used to synchronize the visual states with the value of the aria-checked attribute.|
| | aria-checked="true" | button |
switch is on.[aria-checked="true"]) are used to synchronize the visual states with the value of the aria-checked attribute.|
| | aria-hidden="true" | span.on and span.off |
|
| | | h3 | Provides a grouping label for the group of switches. |
| group | | div | Identifies the div element as a group container for the switches. |
| | aria-labelledby | div | References the h3 element to define the accessible name for the group of switches. |
Learn how to interpret and use assistive technology support data
To copy the following HTML code, please open it in CodePen.