Preview brand Custom

Switch

One whole thing turned on or off: a feature, a section of the site, a setting. The word On or Off sits beside it, so colour is never the only sign.

Beta

Show code
html
<div class="sn-switch-row">
  <input type="checkbox" role="switch" class="sn-switch" id="reviews-show" checked>
  <label for="reviews-show">Show reviews on the site</label>
  <span class="sn-switch-state" aria-hidden="true"></span>
</div>

Switch or tickbox?

SwitchTickbox
MeansThis is on, or it is offYes to this, among other things
Takes effectStraight away, or with the page's own saveWhen the form is sent
Use forShowing a section, turning a feature onAgreeing to terms, choosing several from a list

If turning it on needs anything else filled in before it can work, it isn't a switch: it's a form.

Anatomy

  • The switch: a track 44px by 26px with a knob. Off, the knob is grey at the left; on, the track takes the accent and the knob moves right.
  • The label: what is on when it's on, as a statement: "Show reviews on the site". Never a question, and never "Enable" or "Disable".
  • On or Off: the word at the end of the row follows the switch with no script, in the accent when on.
  • A hint, if there is one, goes under the row as a field hint.

The row can be a div with a label for the switch (above), or the label itself with its words in .sn-switch-label, which makes the whole row the target.

Do and don't

Do

  • Say what happens in the label: "Show reviews on the site"
  • Put the switch first on its page when it turns the rest of the page on or off

Don't

  • Use a switch for a choice between two different things (Grid or List): that's a segmented control
  • Ask for confirmation after every switch

Accessibility

What it does for you

  • A real checkbox with role="switch": a screen reader says "switch", Space turns it, and a form sends it like any checkbox.
  • The word On or Off beside it, so the state is never told by colour or position alone.
  • A 44 by 26px target, and the usual focus ring.
  • The knob’s slide switches off with reduced motion.

What you need to do

  • A label that says what is on when it is on ("Show reviews on the site"). The switch can’t know.
  • Tie the label to the switch: label for, or make the row itself the label.
  • If turning it on changes something elsewhere on the page, say so in a hint under the row.

Keyboard

KeyWhat it does
TabMoves to the switch. A disabled switch is skipped.
SpaceTurns it on or off.

What a screen reader is told

  • Name: its label ("Show reviews on the site"). Role: switch. State: on or off.
  • The visible word On or Off is hidden from it: the switch has already said so.

This is what the browser reports to assistive technology. What each screen reader then says varies, and has not been listened to yet.

WCAG 2.2

CriterionLevelHow this meets it
1.4.1 Use of ColorAThe word On or Off, and the knob’s place, say the state as well as the colour.
2.1.1 KeyboardAIt is a native checkbox: Tab and Space work without script.
2.5.8 Target Size (Minimum)AAThe switch is 44 by 26px.
4.1.2 Name, Role, ValueARole switch, the label as its name, checked or not as its state.

The criteria this component has a part in. A page that uses it still has to meet the rest.

Checked

CheckHowResult
Automated (axe-core)WCAG 2.0, 2.1 and 2.2 rules at A and AA, the page in light and in darkPassed
Nothing found, light or dark
Narrow screenNo sideways scrolling at 320px wide (WCAG 1.4.10)Passed
No sideways scrolling
Keyboard and what the browser reportsReal key presses on the live example, and the role, name and state given to assistive technologyPassed
6 of 6 checks pass
On paperSomnia’s print check: no overlaps, stranded headings or controls on the pagePassed
No faults on 4 sheets
Screen readerListened to by a person, in VoiceOver and NVDANot yet
Not yet checked by a person

The 6 checks made with the keyboard, on the example at the top of this page:

  • Passed Tab reaches the switch
  • Passed Space turns it on and off
  • Passed The word beside it follows: On when on, Off when off
  • Passed The browser reports a switch, with its label and whether it is on
  • Passed A switch that can’t be changed is skipped by Tab
  • Passed The knob stands out from its track by 3:1 or more, off and on, in light and in dark (WCAG 1.4.11)

Run by script on against Somnia DS 1.89.0, in Chromium with axe-core 4.14.0. How these checks work, and every component's results.

Not yet checked

  • How it sounds in a screen reader (VoiceOver, NVDA, TalkBack) has not been checked by a person. The roles, names and states the browser reports are checked by script; what is actually spoken is not.
  • Windows high contrast (forced colours) has not been looked at by a person.

Evidence

Where it's used

WhereFor
Somnia CMSTurning a whole feature on or off: Reviews’ "Show on the site", first on its page.

What we saw, and what changed

WhenWhat we sawWhat changed
Headland wanted its Google reviews off every page for now, without deleting them.A switch, first on the page, with On or Off written beside it (Somnia 1.49.0).
Documenting it showed its row (label and On or Off) existed only inside Somnia CMS.The row is now part of Somnia DS, and the word follows the switch by CSS, with no script to forget.

From building and using it on real sites, each entry taken from the changelog or the build's own notes. This is observation by the studio and its clients, not research sessions with users.

What it rests on

What we don't know yet

  • Not yet watched in use by anyone outside the studio and Headland’s two editors.