Skip to content

Switch Example Using HTML Button

Switch Example Using HTML Button

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.

About This Example

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:

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 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 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 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.

Keyboard Support

Key Function
Tab
  • Moves keyboard focus to the switch.
Space, Enter
  • Toggle switch between on and off.

Role, Property, State, and Tabindex Attributes

Role Attribute Element Usage
switch button Identifies the button element as a switch.
aria-checked="false" button
  • Indicates the switch is off.
  • CSS attribute selectors (e.g. [aria-checked="false"]) are used to synchronize the visual states with the value of the aria-checked attribute.
aria-checked="true" button
  • Indicates the switch is on.
  • CSS attribute selectors (e.g. [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
  • Removes the strings on and off that appear to the right of the switch from the accessible name of the switch.
  • These strings are included only for enhancing visual comprehension of the state; element states are not permitted in accessible names.
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

HTML Source Code

To copy the following HTML code, please open it in CodePen.

Back to Top