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 fieldset
and legend
elements 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 Button: A group of 2 switches based on HTML
button
elements that turn lights on and off.
Example
Accessibility Features
- To help assistive technology users understand that each switch belongs to a set of switches related to
Accessibility Preferences
, the switches are wrapped in afieldset
element labeled with alegend
element. -
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 theinput
.
NOTE: To prevent redundant announcement of the state by screen readers, the text indicators of state are hidden from assistive technologies witharia-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 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
:hover
pseudo-class andonfocus
andonblur
event handlers:- 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 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 zero to two pixels and padding is reduced by two pixels. When an element loses focus, its border changes from two pixels to zero and padding is increased by two pixels.
Keyboard Support
Key | Function |
---|---|
Tab | Moves keyboard focus to the switch . |
Space | Toggles the state of the switch between on and off. |
Role, Property, State, and Tabindex Attributes
Role | Attribute | Element | Usage |
---|---|---|---|
switch |
input[type="checkbox"] |
|
|
aria-hidden="true" |
span.on and span.off |
|
JavaScript and CSS Source Code
- CSS: switch-checkbox.css
- Javascript: switch-checkbox.js
HTML Source Code
To copy the following HTML code, please open it in CodePen.