Switch Example Using HTML Button
This example illustrates implementing the switch design 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:
- Switch Example: A switch based on a
div
element that turns a notification preference on and off. - Switch Example Using HTML Checkbox Input: A group of 2 switches based on HTML
input[type="checkbox"]
elements that turn accessibility preferences on and off.
Example
Environmental Controls
Accessibility Features
- To help assistive technology users understand that the
Environmental Controls
are a group of two switches, the switches are wrapped in agroup
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
oroff
) is displayed adjacent to the graphical state indicator. CSS attribute selectors ensure the label displayed is synchronized with the value of thearia-checked
attribute.
NOTE: To prevent redundant announcement of the state by screen readers, the text indicators of state are hidden from assistive technologies witharia-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 make the graphical representation of the state of a switch readily perceivable, two pixel stroke width is used for the switch state container and a solid color is used to fill the rectangles indicating the on and off states.
- To ensure users can perceive the difference between the container and the rectangles used to indicate the state of the switch, there are two pixels of space between the container border and the rectangles.
- To enhance perceivability when operating the switches, visual keyboard focus and hover are styled using the CSS
:hover
and:focus
pseudo-classes:- 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.
-
To ensure the SVG graphics have sufficient contrast with the background when high contrast settings invert colors, the CSS
currentColor
value for thestroke
andfill
properties is used to synchronize the colors with text content. If specific colors were used to specify thestroke
andfill
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. Thefill-opacity
of the containerrect
is set to zero for the background color of the page to provide the contrasting color to thestroke
andfill
colors.
NOTE: The SVG elements need to set the CSSforced-color-adjust
property toauto
for some browsers to support thecurrentColor
value.
Keyboard Support
Key | Function |
---|---|
Tab |
|
Space, Enter |
|
Role, Property, State, and Tabindex Attributes
Role | Attribute | Element | Usage |
---|---|---|---|
switch |
button |
Identifies the button element as a switch . |
|
aria-checked="false" |
button |
|
|
aria-checked="true" |
button |
|
|
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. |
Javascript and CSS Source Code
- CSS: switch-button.css
- Javascript: switch-button.js
HTML Source Code