Change log
Components
Switch
- Overview
- Usage
- Accessibility
Overview
When to use
- Use a switch when your intent is to turn something on or off instantly.
When not to use
- Switches should never require users to click a button to apply or save the setting.
- When the two states are not opposites or where the alternate setting is not intuitively obvious.
Variants​
Switch has two alignment variants. If you are creating a group of switches or have multiple groups close to each other on a page then stick to one variant for all switches.
Left aligned​
Switch left, label right. Recommended for short labels (5 words or less).
Right aligned​
Label left, switch right. Recommended for longer labels (more than 5 words) or in case there are multiple switches with short and long labels in a group.
This variant should always stretch to the content area width.
Labels and hints​
Make sure that the label is clear and concise. A good label describes the effect that will happen when the switch is set to “On” position. For example, use the label “Allow notifications” instead of “Do you want to receive notifications?”.
Hint text can be used to give more context to the label. Try to keep the hints as short and concise as possible.
Grouping​
When creating groups of switches, use only one variant in a group.
When using right aligned switches, it is recommended to have divider lines between them to help users to make a connection between the label and the switch.
Nesting​
In a nested group, one master switch enables or disables a group of related switches. Use right aligned switches for nested groups.
Sub switches are displayed in a disabled state when the master switch is off.
This example uses JavaScript. Source code can be found here.
Usage guidelines
Labels​
Make sure that the label is clear and concise and describes the effect that will happen when the switch is set to “On” position.
Do
Don't
Switch vs checkbox​
Do not use a switch as a replacement for a checkbox. Switches act as instant-save control for boolean settings, while checkboxes are for selections requiring explicit save actions.
Do
Don't
Keyboard​
A switch is a native checkbox with role="switch":
- Tab moves to the switch.
- Space turns it on or off. Enter does nothing, as with any checkbox.
Clicking anywhere on the label also toggles it.
Screen readers​
The switch is announced as "switch" with its state ("on" / "off"). The state comes from the native checked property, so no aria-checked is needed. The name comes from the visible label text through aria-labelledby, and hint text is linked with aria-describedby. A disabled switch uses the native disabled attribute: it leaves the Tab order and is announced as unavailable.
A master switch has aria-controls pointing at the container of its nested switches (<id>-group). The shared script disables the nested switches while the master is off and enables them when it is turned on.
Using the example​
- Ids must be unique per page; the label, hint and group ids are derived from the switch
id. - Use
ariaLabelonly when there is no visible label, and never with wording that differs from a visible label. - A switch acts immediately. For choices that are only applied after submitting a form, use a checkbox.
- Put the nested switches right after their master in the DOM, as the template does, so users reach them next.
Known limits​
Turning the master on or off changes the nested switches silently. Screen reader users only notice when they move to them. If the effect is not obvious from the master's label, say it in the hint ("Lülitab sisse kõik teavitused").