CSS Toggle Switches: 16 Accessible Examples to Copy

Safari 17.4, released March 2024, is still the only browser that draws a real switch from a checkbox. Chrome, Edge and Firefox show a plain checkbox, so most people style a CSS toggle switch themselves.

Below are 16 CSS toggle switch examples you can try and copy, followed by a guide to building an accessible one.

By Bogdan Sandu · Updated October 28, 2026

Switches in settings screens

Four switches in the places they usually live: a phone settings list, a notification matrix, an accessibility panel and a security page. Every one is a real checkbox with role="switch", or a button with role="switch" and aria-checked where a script owns the state.

Phone settings list

  • appearance: none
  • role="switch"
  • grouped list

Settings

Connections

Focus

Turning on Airplane Mode switches off Wi-Fi and Bluetooth. You can turn them back on by hand.

Each row is a label around a checkbox with appearance: none, so the input itself is the track and its ::before is the thumb. The whole 48px row is the tap target. A small script copies the phone behavior where Airplane Mode turns Wi-Fi and Bluetooth off.

View code
.ts-ios {
  position: relative;
  height: 100%;
  overflow: auto;
  padding: 6px 14px 24px;
  background: #f2f2f7;
  color: #1c1c1e;
  font: inherit;
  font-size: 15px;
}
.ts-ios__title {
  margin: 6px 4px 6px;
  font-size: 30px;
  font-weight: 700;
  letter-spacing: -.02em;
}
.ts-ios__cap {
  margin: 18px 14px 6px;
  color: #5c5c63;
  font-size: 12px;
  letter-spacing: .04em;
  text-transform: uppercase;
}
.ts-ios__group {
  margin: 0;
  padding: 0;
  overflow: hidden;
  list-style: none;
  border-radius: 12px;
  background: #fff;
}
.ts-ios__row {
  position: relative;
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 48px;
  padding: 6px 12px;
  cursor: pointer;
}
.ts-ios__group li + li .ts-ios__row::before {
  content: "";
  position: absolute;
  top: 0;
  right: 0;
  left: 54px;
  height: 1px;
  background: #dcdce1;
}
.ts-ios__icon {
  display: grid;
  flex: none;
  place-items: center;
  width: 30px;
  height: 30px;
  border-radius: 8px;
  color: #fff;
}
.ts-ios__icon svg {
  width: 18px;
  height: 18px;
  fill: currentColor;
}
.ts-ios__icon .ts-ios__stroke {
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.ts-ios__icon--plane { background: #c2410c; }
.ts-ios__icon--wifi { background: #1d5fd6; }
.ts-ios__icon--bt { background: #1d5fd6; }
.ts-ios__icon--spot { background: #1f8a3c; }
.ts-ios__icon--moon { background: #5b3cc4; }
.ts-ios__icon--dev { background: #4b5563; }
.ts-ios__label {
  flex: 1;
  min-width: 0;
}
.ts-ios__sw {
  -webkit-appearance: none;
  appearance: none;
  position: relative;
  flex: none;
  width: 51px;
  height: 31px;
  margin: 0;
  border-radius: 999px;
  background: #78787f;
  cursor: pointer;
}
.ts-ios__sw::before {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 27px;
  height: 27px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 2px 4px rgba(0, 0, 0, .25);
}
.ts-ios__sw:checked {
  background: #1f8a3c;
}
.ts-ios__sw:checked::before {
  transform: translateX(20px);
}
.ts-ios__sw:focus-visible {
  outline: 3px solid #1d5fd6;
  outline-offset: 2px;
}
.ts-ios__foot {
  margin: 10px 14px 0;
  color: #5c5c63;
  font-size: 13px;
  line-height: 1.45;
}
@media (prefers-reduced-motion: no-preference) {
  .ts-ios__sw {
    transition: background-color .2s ease;
  }
  .ts-ios__sw::before {
    transition: transform .25s cubic-bezier(.3, .7, .4, 1);
  }
}
@media (forced-colors: active) {
  .ts-ios__sw {
    border: 2px solid CanvasText;
  }
  .ts-ios__sw::before {
    top: 1px;
    left: 1px;
    background: CanvasText;
  }
  .ts-ios__sw:checked {
    background: Highlight;
  }
  .ts-ios__sw:checked::before {
    background: HighlightText;
  }
}

Notification preferences table

  • aria-labelledby
  • table headers
  • CSS counters
Notify me about
EmailPushSMS
New comments
Mentions
Review requests
Weekly digest
Security alertsEmail stays on for your safety

Fifteen switches in a real table. Each one takes its name from its row and column headers through aria-labelledby, so a screen reader hears Mentions Email instead of fifteen unnamed switches. A CSS counter totals the checked inputs with no script.

View code
.ts-nt {
  width: min(100%, 520px);
  padding: 18px 16px 14px;
  border: 1px solid #e3e5ee;
  border-radius: 16px;
  background: #fff;
  color: #1e2235;
  box-shadow: 0 12px 32px rgba(30, 34, 53, .08);
  counter-reset: ts-nt-on;
}
.ts-nt__table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}
.ts-nt__cap {
  padding: 0 4px 10px;
  font-size: 17px;
  font-weight: 700;
  text-align: start;
}
.ts-nt__table thead th {
  width: 56px;
  padding: 0 0 8px;
  color: #535872;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: .03em;
  text-align: center;
  text-transform: uppercase;
}
.ts-nt__table tbody th {
  padding: 12px 8px 12px 4px;
  font-weight: 500;
  text-align: start;
}
.ts-nt__table tbody th small {
  display: block;
  margin-top: 2px;
  color: #616680;
  font-size: 12px;
  font-weight: 400;
}
.ts-nt__table tbody tr + tr > * {
  border-top: 1px solid #eceef4;
}
.ts-nt__table td {
  text-align: center;
  vertical-align: middle;
}
.ts-nt__sw {
  -webkit-appearance: none;
  appearance: none;
  position: relative;
  display: inline-block;
  width: 40px;
  height: 24px;
  margin: 0;
  vertical-align: middle;
  border-radius: 999px;
  background: #767a8c;
  cursor: pointer;
}
.ts-nt__sw::before {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #fff;
}
.ts-nt__sw:checked {
  background: #4338ca;
  counter-increment: ts-nt-on;
}
.ts-nt__sw:checked::before {
  transform: translateX(16px);
}
.ts-nt__sw:disabled {
  background: #a5a1e0;
  cursor: not-allowed;
}
.ts-nt__sw:focus-visible {
  outline: 3px solid #4338ca;
  outline-offset: 2px;
}
.ts-nt__sum {
  margin: 10px 4px 0;
  padding-top: 10px;
  border-top: 1px solid #eceef4;
  color: #535872;
  font-size: 13px;
}
.ts-nt__sum::before {
  content: counter(ts-nt-on) " of 15 channels on";
}
@media (prefers-reduced-motion: no-preference) {
  .ts-nt__sw {
    transition: background-color .18s ease;
  }
  .ts-nt__sw::before {
    transition: transform .2s ease;
  }
}
@media (forced-colors: active) {
  .ts-nt__sw {
    border: 2px solid CanvasText;
  }
  .ts-nt__sw::before {
    top: 1px;
    left: 1px;
    background: CanvasText;
  }
  .ts-nt__sw:checked {
    background: Highlight;
  }
  .ts-nt__sw:checked::before {
    background: HighlightText;
  }
  .ts-nt__sw:disabled {
    border-color: GrayText;
    background: GrayText;
  }
}

Accessibility panel with live preview

  • :has()
  • matchMedia
  • preview pane

Accessibility

Now playing Low Tide Radio, episode 41

Tonight the show visits a harbor town where the ferries still run on a paper timetable. Read the episode notes

Four switches restyle the preview beside them through :has(), with no script for the styling. The Reduce motion switch starts checked when the operating system already asks for reduced motion, read once with matchMedia, and the equalizer only animates inside a no-preference media query anyway.

View code
.ts-ax {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 250px), 1fr));
  gap: 14px;
  width: min(100%, 640px);
  color: #2b2118;
}
.ts-ax__panel,
.ts-ax__preview {
  padding: 16px;
  border-radius: 16px;
  background: #fffaf2;
  box-shadow: 0 0 0 1px #eadfcd;
}
.ts-ax__title {
  margin: 0 0 4px;
  font-family: "Fraunces", Georgia, serif;
  font-size: 20px;
  font-weight: 600;
}
.ts-ax__row {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 52px;
  padding: 6px 0;
  cursor: pointer;
}
.ts-ax__row + .ts-ax__row {
  border-top: 1px solid #eee3d1;
}
.ts-ax__text {
  flex: 1;
  min-width: 0;
}
.ts-ax__text strong {
  display: block;
  font-size: 15px;
  font-weight: 600;
}
.ts-ax__text small {
  color: #6a5a48;
  font-size: 12.5px;
}
.ts-ax__sw {
  -webkit-appearance: none;
  appearance: none;
  position: relative;
  flex: none;
  width: 46px;
  height: 28px;
  margin: 0;
  border-radius: 999px;
  background: #7d6f60;
  cursor: pointer;
}
.ts-ax__sw::before {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: #fff;
}
.ts-ax__sw:checked {
  background: #b4441c;
}
.ts-ax__sw:checked::before {
  transform: translateX(18px);
}
.ts-ax__sw:focus-visible {
  outline: 3px solid #b4441c;
  outline-offset: 2px;
}
.ts-ax__preview {
  display: grid;
  align-content: start;
  gap: 14px;
  background: #f4ead9;
  font-size: 15px;
  line-height: 1.55;
}
.ts-ax__player {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px;
  border-radius: 12px;
  background: #2b2118;
  color: #fbf3e6;
  font-size: 13.5px;
}
.ts-ax__now strong {
  display: block;
  color: #f2b680;
  font-size: 11.5px;
  letter-spacing: .06em;
  text-transform: uppercase;
}
.ts-ax__bars {
  display: flex;
  flex: none;
  align-items: flex-end;
  gap: 3px;
  height: 26px;
}
.ts-ax__bars i {
  width: 5px;
  height: 100%;
  border-radius: 2px;
  background: #f2b680;
  transform-origin: bottom;
}
.ts-ax__bars i:nth-child(1) { transform: scaleY(.45); }
.ts-ax__bars i:nth-child(2) { transform: scaleY(.9); }
.ts-ax__bars i:nth-child(3) { transform: scaleY(.6); }
.ts-ax__bars i:nth-child(4) { transform: scaleY(.75); }
.ts-ax__copy {
  margin: 0;
}
.ts-ax__link {
  color: #9a3412;
  font-weight: 600;
  text-decoration: none;
}
.ts-ax__link:focus-visible {
  outline: 2px solid #9a3412;
  outline-offset: 2px;
}
.ts-ax:has(.ts-ax__size:checked) .ts-ax__copy {
  font-size: 1.2em;
}
.ts-ax:has(.ts-ax__links:checked) .ts-ax__link {
  text-decoration: underline;
  text-underline-offset: 3px;
}
.ts-ax:has(.ts-ax__contrast:checked) .ts-ax__preview {
  background: #fff;
  color: #000;
  box-shadow: 0 0 0 2px #000;
}
.ts-ax:has(.ts-ax__contrast:checked) .ts-ax__player {
  background: #000;
  color: #fff;
}
.ts-ax:has(.ts-ax__contrast:checked) .ts-ax__link {
  color: #000;
}
@keyframes ts-ax-eq {
  0%, 100% { transform: scaleY(.3); }
  50% { transform: scaleY(1); }
}
@media (prefers-reduced-motion: no-preference) {
  .ts-ax__bars i {
    animation: ts-ax-eq .9s ease-in-out infinite;
  }
  .ts-ax__bars i:nth-child(2) { animation-delay: -.3s; }
  .ts-ax__bars i:nth-child(3) { animation-delay: -.6s; }
  .ts-ax__bars i:nth-child(4) { animation-delay: -.15s; }
  .ts-ax:has(.ts-ax__motion:checked) .ts-ax__bars i {
    animation: none;
  }
  .ts-ax__sw {
    transition: background-color .2s ease;
  }
  .ts-ax__sw::before {
    transition: transform .22s ease;
  }
}
@media (forced-colors: active) {
  .ts-ax__sw {
    border: 2px solid CanvasText;
  }
  .ts-ax__sw::before {
    top: 1px;
    left: 1px;
    background: CanvasText;
  }
  .ts-ax__sw:checked {
    background: Highlight;
  }
  .ts-ax__sw:checked::before {
    background: HighlightText;
  }
}

Switch that asks before turning off

  • button role="switch"
  • aria-checked
  • inline confirm

Sign-in and security

Two-step sign-inA code from your phone on every new device

Turning off two-step sign-in is risky, so this switch is a button with role="switch" and aria-checked, and the script decides when the state changes. Switching off opens an inline confirmation with focus on the safe choice, and Escape cancels. Switching on needs no question.

View code
.ts-cf {
  width: min(100%, 460px);
  padding: 18px 18px 8px;
  border-radius: 14px;
  background: #fff;
  color: #111827;
  box-shadow: 0 0 0 1px #e5e7eb, 0 14px 34px rgba(17, 24, 39, .08);
}
.ts-cf__title {
  margin: 0 0 6px;
  font-size: 17px;
  font-weight: 700;
}
.ts-cf__row {
  display: flex;
  align-items: center;
  gap: 14px;
  min-height: 60px;
  padding: 8px 0;
  border-top: 1px solid #eef0f3;
}
label.ts-cf__row {
  cursor: pointer;
}
.ts-cf__text {
  flex: 1;
  min-width: 0;
}
.ts-cf__text strong {
  display: block;
  font-size: 15px;
  font-weight: 600;
}
.ts-cf__text small {
  color: #4b5563;
  font-size: 13px;
}
.ts-cf__sw,
.ts-cf__input {
  -webkit-appearance: none;
  appearance: none;
  position: relative;
  flex: none;
  width: 48px;
  height: 28px;
  margin: 0;
  padding: 0;
  border: 0;
  border-radius: 999px;
  background: #6b7280;
  cursor: pointer;
}
.ts-cf__thumb,
.ts-cf__input::before {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: #fff;
}
.ts-cf__sw[aria-checked="true"],
.ts-cf__input:checked {
  background: #0b6b5c;
}
.ts-cf__sw[aria-checked="true"] .ts-cf__thumb,
.ts-cf__input:checked::before {
  transform: translateX(20px);
}
.ts-cf__sw:focus-visible,
.ts-cf__input:focus-visible {
  outline: 3px solid #0b6b5c;
  outline-offset: 2px;
}
.ts-cf:has(.ts-cf__confirm:not([hidden])) .ts-cf__sw {
  outline: 2px dashed #b42318;
  outline-offset: 3px;
}
.ts-cf__confirm {
  margin: 0 0 10px;
  padding: 14px;
  border-radius: 10px;
  background: #fef3f2;
  box-shadow: inset 0 0 0 1px #fecdca;
}
.ts-cf__confirm[hidden] {
  display: none;
}
.ts-cf__q {
  margin: 0 0 12px;
  color: #3f1d1a;
  font-size: 14px;
  line-height: 1.5;
}
.ts-cf__q strong {
  display: block;
}
.ts-cf__actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
.ts-cf__actions button {
  min-height: 44px;
  padding: 0 16px;
  border: 0;
  border-radius: 8px;
  font: inherit;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}
.ts-cf__keep {
  background: #111827;
  color: #fff;
}
.ts-cf__off {
  background: #fff;
  color: #b42318;
  box-shadow: inset 0 0 0 1.5px #b42318;
}
.ts-cf__actions button:focus-visible {
  outline: 3px solid #111827;
  outline-offset: 2px;
}
.ts-cf__status {
  min-height: 1.4em;
  margin: 4px 0 6px;
  color: #4b5563;
  font-size: 13px;
}
@media (prefers-reduced-motion: no-preference) {
  .ts-cf__sw,
  .ts-cf__input {
    transition: background-color .2s ease;
  }
  .ts-cf__thumb,
  .ts-cf__input::before {
    transition: transform .2s ease;
  }
}
@media (forced-colors: active) {
  .ts-cf__sw,
  .ts-cf__input {
    border: 2px solid ButtonText;
  }
  .ts-cf__thumb,
  .ts-cf__input::before {
    top: 1px;
    left: 1px;
    background: ButtonText;
  }
  .ts-cf__sw[aria-checked="true"],
  .ts-cf__input:checked {
    background: Highlight;
  }
  .ts-cf__sw[aria-checked="true"] .ts-cf__thumb,
  .ts-cf__input:checked::before {
    background: HighlightText;
  }
}

Theme, mode and pricing switches

Switches that change something you can see right away: the color scheme, the billing period, a whole card. The segmented control is included on purpose, as the honest answer when there are three options and a switch would lie.

Dark mode switch with sun and moon

  • aria-checked
  • :has() theming
  • icon crossfade
Field Notes

Journal, 6 min read

Sketching on trains, one stop at a time

I keep a pocket notebook for the 40 minute ride into the city. One rule: a drawing has to be finished before the doors open again.

An icon-only switch, so the button carries a fixed aria-label of Dark mode and aria-checked holds the value. The page section reads that attribute through :has() and swaps its color custom properties, which is the whole theme change. The sun rotates out and the moon in only when motion is allowed.

View code
.ts-dm {
  --ts-dm-bg: #fbf8f1;
  --ts-dm-ink: #26211b;
  --ts-dm-muted: #6a6052;
  --ts-dm-line: #e6dfd0;
  --ts-dm-track: #6f6657;
  --ts-dm-thumb: #fff;
  --ts-dm-icon: #b45309;
  display: grid;
  gap: 28px;
  min-height: 360px;
  padding: 22px clamp(18px, 5vw, 48px) 36px;
  background: var(--ts-dm-bg);
  color: var(--ts-dm-ink);
  transition: background-color .3s ease, color .3s ease;
}
.ts-dm:has(.ts-dm__sw[aria-checked="true"]) {
  --ts-dm-bg: #15171d;
  --ts-dm-ink: #eceef4;
  --ts-dm-muted: #a4aabb;
  --ts-dm-line: #2a2e38;
  --ts-dm-track: #8aa0d6;
  --ts-dm-thumb: #15171d;
  --ts-dm-icon: #e6eaf5;
  color-scheme: dark;
}
.ts-dm__bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding-bottom: 16px;
  border-bottom: 1px solid var(--ts-dm-line);
}
.ts-dm__brand {
  font-family: "Fraunces", Georgia, serif;
  font-size: 22px;
  font-weight: 600;
}
.ts-dm__sw {
  position: relative;
  flex: none;
  width: 64px;
  height: 36px;
  padding: 0;
  border: 0;
  border-radius: 999px;
  background: var(--ts-dm-track);
  cursor: pointer;
}
.ts-dm__thumb {
  position: absolute;
  top: 4px;
  left: 4px;
  display: grid;
  place-items: center;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--ts-dm-thumb);
  color: var(--ts-dm-icon);
}
.ts-dm__thumb svg {
  grid-area: 1 / 1;
  width: 18px;
  height: 18px;
}
.ts-dm__sun {
  fill: currentColor;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
}
.ts-dm__moon {
  fill: currentColor;
  opacity: 0;
}
.ts-dm__sw[aria-checked="true"] .ts-dm__thumb {
  transform: translateX(28px);
}
.ts-dm__sw[aria-checked="true"] .ts-dm__sun {
  opacity: 0;
}
.ts-dm__sw[aria-checked="true"] .ts-dm__moon {
  opacity: 1;
}
.ts-dm__sw:focus-visible {
  outline: 3px solid var(--ts-dm-ink);
  outline-offset: 3px;
}
.ts-dm__post {
  max-width: 34em;
}
.ts-dm__meta {
  margin: 0 0 8px;
  color: var(--ts-dm-muted);
  font-size: 13px;
}
.ts-dm__title {
  margin: 0 0 12px;
  font-family: "Fraunces", Georgia, serif;
  font-size: clamp(24px, 4vw, 32px);
  font-weight: 600;
  line-height: 1.15;
}
.ts-dm__text {
  margin: 0;
  color: var(--ts-dm-muted);
  font-size: 16px;
  line-height: 1.6;
}
@media (prefers-reduced-motion: no-preference) {
  .ts-dm__sw {
    transition: background-color .3s ease;
  }
  .ts-dm__thumb {
    transition: transform .35s cubic-bezier(.4, .1, .2, 1), background-color .3s ease;
  }
  .ts-dm__sun,
  .ts-dm__moon {
    transition: opacity .25s ease, transform .35s ease;
  }
  .ts-dm__moon {
    transform: rotate(-90deg);
  }
  .ts-dm__sw[aria-checked="true"] .ts-dm__sun {
    transform: rotate(90deg);
  }
  .ts-dm__sw[aria-checked="true"] .ts-dm__moon {
    transform: none;
  }
}
@media (forced-colors: active) {
  .ts-dm__sw {
    border: 2px solid ButtonText;
  }
  .ts-dm__thumb {
    top: 2px;
    left: 2px;
    background: ButtonText;
    color: ButtonFace;
  }
  .ts-dm__sw[aria-checked="true"] {
    background: Highlight;
  }
}

Monthly or yearly pricing switch

  • aria-labelledby
  • :has() content swap
  • auto-fit grid
Save 20%

Starter

$10 per month$8 per month, $96 billed yearly

  • 3 projects
  • 5 GB of storage
  • Email support

Studio

$25 per month$20 per month, $240 billed yearly

  • Unlimited projects
  • 100 GB of storage
  • Priority support

The switch is named Yearly, so on means yearly billing and off means monthly, with no ambiguity about which side is which. The prices swap through :has(), and display: none keeps the hidden price out of the accessibility tree. The Monthly word is decoration for sighted users.

View code
.ts-pr {
  display: grid;
  justify-items: center;
  gap: 22px;
  width: min(100%, 600px);
  color: #0b0b0f;
}
.ts-pr__toggle {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 10px 12px;
  font-size: 15px;
}
.ts-pr__word {
  color: #55565f;
  font-weight: 500;
}
.ts-pr__word--y {
  cursor: pointer;
}
.ts-pr:not(:has(.ts-pr__sw:checked)) .ts-pr__word--m,
.ts-pr:has(.ts-pr__sw:checked) .ts-pr__word--y {
  color: #0b0b0f;
  font-weight: 700;
}
.ts-pr__sw {
  -webkit-appearance: none;
  appearance: none;
  position: relative;
  width: 52px;
  height: 30px;
  margin: 0;
  border-radius: 999px;
  background: #0b0b0f;
  cursor: pointer;
}
.ts-pr__sw::before {
  content: "";
  position: absolute;
  top: 4px;
  left: 4px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: #d9f99d;
}
.ts-pr__sw:checked::before {
  transform: translateX(22px);
}
.ts-pr__sw:focus-visible {
  outline: 3px solid #0b0b0f;
  outline-offset: 3px;
}
.ts-pr__save {
  padding: 4px 10px;
  border-radius: 999px;
  background: #d9f99d;
  color: #1a2e05;
  font-size: 12.5px;
  font-weight: 700;
}
.ts-pr__plans {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 220px), 1fr));
  gap: 14px;
  width: 100%;
}
.ts-pr__plan {
  padding: 20px;
  border-radius: 18px;
  background: #fff;
  box-shadow: 0 0 0 1px #e4e4e7;
}
.ts-pr__plan--pro {
  background: #0b0b0f;
  color: #fafafa;
  box-shadow: none;
}
.ts-pr__name {
  margin: 0 0 8px;
  font-size: 14px;
  font-weight: 700;
  letter-spacing: .04em;
  text-transform: uppercase;
}
.ts-pr__price {
  min-height: 3.2em;
  margin: 0 0 14px;
  font-size: 14px;
  line-height: 1.35;
}
.ts-pr__price b {
  display: block;
  font-size: 38px;
  font-weight: 700;
  letter-spacing: -.02em;
  line-height: 1.1;
}
.ts-pr__y {
  display: none;
}
.ts-pr:has(.ts-pr__sw:checked) .ts-pr__m {
  display: none;
}
.ts-pr:has(.ts-pr__sw:checked) .ts-pr__y {
  display: block;
}
.ts-pr__list {
  display: grid;
  gap: 6px;
  margin: 0;
  padding: 14px 0 0;
  border-top: 1px solid #e4e4e7;
  list-style: none;
  font-size: 14px;
}
.ts-pr__plan--pro .ts-pr__list {
  border-color: #33343c;
}
@media (prefers-reduced-motion: no-preference) {
  .ts-pr__sw::before {
    transition: transform .22s cubic-bezier(.3, .7, .4, 1);
  }
}
@media (forced-colors: active) {
  .ts-pr__sw {
    border: 2px solid CanvasText;
  }
  .ts-pr__sw::before {
    top: 2px;
    left: 2px;
    background: CanvasText;
  }
  .ts-pr__sw:checked {
    background: Highlight;
  }
  .ts-pr__sw:checked::before {
    background: HighlightText;
  }
}

Three-way theme picker (not a switch)

  • radio group
  • fieldset + legend
  • :has() indicator
Appearance

Always light, whatever the device says.

Follows the setting on this device.

Always dark, even at noon.

Light, System and Dark are three values, and a switch only has two, so this is a radio group styled as a segmented control. Screen readers announce radio buttons, 1 of 3, and the arrow keys move between options natively. A :has() rule slides the white pill under the checked label.

View code
.ts-seg {
  width: min(100%, 380px);
  margin: 0;
  padding: 18px;
  border: 0;
  border-radius: 16px;
  background: #fff;
  color: #1f2328;
  box-shadow: 0 0 0 1px #e1e4e8, 0 10px 28px rgba(31, 35, 40, .08);
}
.ts-seg__legend {
  float: left;
  width: 100%;
  margin: 0 0 12px;
  padding: 0;
  font-size: 16px;
  font-weight: 700;
}
.ts-seg__track {
  position: relative;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  clear: both;
  padding: 4px;
  border-radius: 12px;
  background: #e9ebef;
}
.ts-seg__opt {
  position: relative;
  z-index: 1;
  display: grid;
  place-items: center;
  min-height: 44px;
  border-radius: 9px;
  color: #4a5059;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
}
.ts-seg__opt input {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}
.ts-seg__opt:has(input:checked) {
  color: #1f2328;
  font-weight: 700;
}
.ts-seg__opt:has(input:focus-visible) {
  outline: 3px solid #0969da;
  outline-offset: 1px;
}
.ts-seg__pill {
  position: absolute;
  top: 4px;
  bottom: 4px;
  left: 4px;
  width: calc((100% - 8px) / 3);
  border-radius: 9px;
  background: #fff;
  box-shadow: 0 1px 3px rgba(31, 35, 40, .18);
  transform: translateX(100%);
}
.ts-seg__track:has(input[value="light"]:checked) .ts-seg__pill {
  transform: translateX(0);
}
.ts-seg__track:has(input[value="dark"]:checked) .ts-seg__pill {
  transform: translateX(200%);
}
.ts-seg__hint {
  display: none;
  margin: 12px 2px 0;
  color: #57606a;
  font-size: 13.5px;
}
.ts-seg:has(input[value="light"]:checked) .ts-seg__hint--light,
.ts-seg:has(input[value="system"]:checked) .ts-seg__hint--system,
.ts-seg:has(input[value="dark"]:checked) .ts-seg__hint--dark {
  display: block;
}
@media (prefers-reduced-motion: no-preference) {
  .ts-seg__pill {
    transition: transform .25s cubic-bezier(.3, .7, .4, 1);
  }
}
@media (forced-colors: active) {
  .ts-seg__track {
    border: 1px solid CanvasText;
  }
  .ts-seg__pill {
    border: 2px solid Highlight;
  }
}

Card that reacts to its switch

  • :has()
  • grid-template-rows 0fr
  • visibility

Thanks for your note. I am hiking in the Dolomites until the 14th and will reply when I am back.

The out-of-office card restyles itself from the state of the switch inside it with :has(): border, status text and a revealed message. The extra panel opens with the 0fr to 1fr grid trick, and visibility keeps its button out of the tab order while it is closed.

View code
.ts-ooo {
  width: min(100%, 420px);
  padding: 6px 18px;
  border: 2px solid #e2e5e9;
  border-radius: 16px;
  background: #fff;
  color: #172026;
  transition: border-color .25s ease, background-color .25s ease;
}
.ts-ooo:has(.ts-ooo__sw:checked) {
  border-color: #c2410c;
  background: #fff8f1;
}
.ts-ooo__head {
  display: flex;
  align-items: center;
  gap: 14px;
  min-height: 72px;
  cursor: pointer;
}
.ts-ooo__icon {
  display: grid;
  flex: none;
  place-items: center;
  width: 42px;
  height: 42px;
  border-radius: 12px;
  background: #eef1f4;
  color: #3d4852;
}
.ts-ooo:has(.ts-ooo__sw:checked) .ts-ooo__icon {
  background: #c2410c;
  color: #fff;
}
.ts-ooo__icon svg {
  width: 22px;
  height: 22px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linejoin: round;
}
.ts-ooo__text {
  flex: 1;
  min-width: 0;
}
.ts-ooo__text strong {
  display: block;
  font-size: 15.5px;
}
.ts-ooo__text small {
  color: #52606b;
  font-size: 13px;
}
.ts-ooo__on {
  display: none;
}
.ts-ooo:has(.ts-ooo__sw:checked) .ts-ooo__on {
  display: inline;
  color: #9a3412;
  font-weight: 600;
}
.ts-ooo:has(.ts-ooo__sw:checked) .ts-ooo__off {
  display: none;
}
.ts-ooo__sw {
  -webkit-appearance: none;
  appearance: none;
  position: relative;
  flex: none;
  width: 48px;
  height: 28px;
  margin: 0;
  border-radius: 999px;
  background: #6b7680;
  cursor: pointer;
}
.ts-ooo__sw::before {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: #fff;
}
.ts-ooo__sw:checked {
  background: #c2410c;
}
.ts-ooo__sw:checked::before {
  transform: translateX(20px);
}
.ts-ooo__sw:focus-visible {
  outline: 3px solid #c2410c;
  outline-offset: 2px;
}
.ts-ooo__more {
  display: grid;
  grid-template-rows: 0fr;
  visibility: hidden;
}
.ts-ooo:has(.ts-ooo__sw:checked) .ts-ooo__more {
  grid-template-rows: 1fr;
  visibility: visible;
}
.ts-ooo__inner {
  min-height: 0;
  overflow: hidden;
}
.ts-ooo__msg {
  margin: 0 0 12px;
  padding: 12px 14px;
  border-radius: 10px;
  background: #fff;
  color: #2c363e;
  font-size: 14px;
  line-height: 1.5;
  box-shadow: inset 0 0 0 1px #f3d9c4;
}
.ts-ooo__edit {
  min-height: 40px;
  margin: 0 0 14px;
  padding: 0 14px;
  border: 0;
  border-radius: 8px;
  background: #172026;
  color: #fff;
  font: inherit;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}
.ts-ooo__edit:focus-visible {
  outline: 3px solid #c2410c;
  outline-offset: 2px;
}
@media (prefers-reduced-motion: no-preference) {
  .ts-ooo__sw {
    transition: background-color .2s ease;
  }
  .ts-ooo__sw::before {
    transition: transform .22s ease;
  }
  .ts-ooo__more {
    transition: grid-template-rows .3s ease, visibility 0s .3s;
  }
  .ts-ooo:has(.ts-ooo__sw:checked) .ts-ooo__more {
    transition: grid-template-rows .3s ease;
  }
}
@media (forced-colors: active) {
  .ts-ooo__sw {
    border: 2px solid CanvasText;
  }
  .ts-ooo__sw::before {
    top: 1px;
    left: 1px;
    background: CanvasText;
  }
  .ts-ooo__sw:checked {
    background: Highlight;
  }
  .ts-ooo__sw:checked::before {
    background: HighlightText;
  }
}

Track and thumb styles

Four ways to draw the control itself: text inside the track, icons inside the thumb, an elastic thumb and a large touch size. The accessible name stays the same in every state, only the on or off value changes.

On and off text inside the track

  • hidden checkbox
  • aria-hidden text
  • sibling selector

Click a switch, or tab to it and press Space.

The guide build with text in the track. The words On and Off are aria-hidden, because the switch role already announces the state, and the name stays Autosave in both states. A 2px light border keeps the off track at 3:1 against the near-black panel.

View code
.ts-oo {
  display: grid;
  gap: 6px;
  width: min(100%, 300px);
  padding: 14px 18px;
  border-radius: 14px;
  background: #1a1d24;
  color: #e7e9ee;
  box-shadow: 0 0 0 1px #2b303b;
}
.ts-oo__row {
  position: relative;
  display: flex;
  align-items: center;
  gap: 14px;
  min-height: 48px;
  cursor: pointer;
}
.ts-oo__input {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}
.ts-oo__track {
  position: relative;
  display: flex;
  flex: none;
  align-items: center;
  justify-content: space-between;
  width: 68px;
  height: 32px;
  padding: 0 10px;
  border: 2px solid #8a92a3;
  border-radius: 999px;
  background: #3a404c;
  font-family: ui-monospace, Consolas, monospace;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: .06em;
  text-transform: uppercase;
}
.ts-oo__track::before {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: #e7e9ee;
}
.ts-oo__on {
  color: #241a00;
  visibility: hidden;
}
.ts-oo__off {
  color: #e7e9ee;
}
.ts-oo__input:checked + .ts-oo__track {
  border-color: #f5b021;
  background: #f5b021;
}
.ts-oo__input:checked + .ts-oo__track::before {
  background: #241a00;
  transform: translateX(36px);
}
.ts-oo__input:checked + .ts-oo__track .ts-oo__on {
  visibility: visible;
}
.ts-oo__input:checked + .ts-oo__track .ts-oo__off {
  visibility: hidden;
}
.ts-oo__input:focus-visible + .ts-oo__track {
  outline: 3px solid #f5b021;
  outline-offset: 3px;
}
.ts-oo__label {
  font-size: 15px;
}
@media (prefers-reduced-motion: no-preference) {
  .ts-oo__track {
    transition: background-color .2s ease, border-color .2s ease;
  }
  .ts-oo__track::before {
    transition: transform .22s ease, background-color .2s ease;
  }
}
@media (forced-colors: active) {
  .ts-oo__track {
    border-color: CanvasText;
  }
  .ts-oo__track::before {
    background: CanvasText;
  }
  .ts-oo__input:checked + .ts-oo__track {
    border-color: Highlight;
    background: Highlight;
  }
  .ts-oo__input:checked + .ts-oo__track::before {
    background: HighlightText;
  }
  .ts-oo__on {
    color: HighlightText;
  }
}

Check and cross icons in the thumb

  • inline SVG
  • opacity crossfade
  • not color alone

Click a switch, or tab to it and press Space.

A check for on and a cross for off means the state never depends on color alone, which helps color-blind users and anyone in bright sunlight. Both icons sit in the same grid cell inside the thumb and crossfade, and they are aria-hidden like the rest of the track.

View code
.ts-ic {
  width: min(100%, 440px);
  padding: 6px 18px;
  border-radius: 18px;
  background: #f0faf8;
  color: #12302c;
  box-shadow: 0 0 0 1px #cfe9e4;
}
.ts-ic__row {
  position: relative;
  display: flex;
  align-items: center;
  gap: 14px;
  min-height: 66px;
  cursor: pointer;
}
.ts-ic__row + .ts-ic__row {
  border-top: 1px solid #d5ebe6;
}
.ts-ic__text {
  flex: 1;
  min-width: 0;
}
.ts-ic__text strong {
  display: block;
  font-size: 15px;
}
.ts-ic__text small {
  color: #3f5f5a;
  font-size: 13px;
}
.ts-ic__input {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}
.ts-ic__track {
  position: relative;
  flex: none;
  width: 56px;
  height: 32px;
  border-radius: 999px;
  background: #64748b;
}
.ts-ic__thumb {
  position: absolute;
  top: 3px;
  left: 3px;
  display: grid;
  place-items: center;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(18, 48, 44, .3);
}
.ts-ic__thumb svg {
  grid-area: 1 / 1;
  width: 14px;
  height: 14px;
  fill: none;
  stroke-width: 2.2;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.ts-ic__yes {
  stroke: #0f766e;
  opacity: 0;
}
.ts-ic__no {
  stroke: #475569;
}
.ts-ic__input:checked + .ts-ic__track {
  background: #0f766e;
}
.ts-ic__input:checked + .ts-ic__track .ts-ic__thumb {
  transform: translateX(24px);
}
.ts-ic__input:checked + .ts-ic__track .ts-ic__yes {
  opacity: 1;
}
.ts-ic__input:checked + .ts-ic__track .ts-ic__no {
  opacity: 0;
}
.ts-ic__input:focus-visible + .ts-ic__track {
  outline: 3px solid #0f766e;
  outline-offset: 3px;
}
.ts-ic__track,
.ts-ic__thumb svg {
  transition: background-color .2s ease, opacity .2s ease;
}
@media (prefers-reduced-motion: no-preference) {
  .ts-ic__thumb {
    transition: transform .25s cubic-bezier(.3, .7, .4, 1);
  }
}
@media (forced-colors: active) {
  .ts-ic__track {
    border: 2px solid CanvasText;
  }
  .ts-ic__thumb {
    top: 1px;
    left: 1px;
    background: CanvasText;
  }
  .ts-ic__thumb svg {
    stroke: Canvas;
  }
  .ts-ic__input:checked + .ts-ic__track {
    background: Highlight;
  }
  .ts-ic__input:checked + .ts-ic__track .ts-ic__thumb {
    background: HighlightText;
  }
  .ts-ic__input:checked + .ts-ic__track .ts-ic__yes {
    stroke: Highlight;
  }
}

Squishy elastic thumb

  • overshoot cubic-bezier
  • :active squash
  • motion opt-in

Click a switch, or tab to it and press Space.

Pressing squashes the thumb wide and flat, and releasing springs it across with a cubic-bezier that overshoots past 1. Position and scale live in two custom properties that feed one transform, so the press and the slide never fight. All of the motion sits inside prefers-reduced-motion: no-preference.

View code
.ts-sq {
  display: grid;
  gap: 4px;
  width: min(100%, 320px);
  padding: 14px 20px;
  border-radius: 24px;
  background: #f1ecff;
  color: #2a1f4d;
}
.ts-sq__row {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  min-height: 56px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.ts-sq__label {
  font-size: 16px;
  font-weight: 600;
}
.ts-sq__input {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}
.ts-sq__track {
  position: relative;
  flex: none;
  width: 66px;
  height: 38px;
  border-radius: 999px;
  background: #6e6987;
}
.ts-sq__thumb {
  --ts-sq-x: 0px;
  --ts-sq-sx: 1;
  --ts-sq-sy: 1;
  position: absolute;
  top: 5px;
  left: 5px;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: #fff;
  transform: translateX(var(--ts-sq-x)) scale(var(--ts-sq-sx), var(--ts-sq-sy));
}
.ts-sq__input:checked + .ts-sq__track {
  background: #6d28d9;
}
.ts-sq__input:checked + .ts-sq__track .ts-sq__thumb {
  --ts-sq-x: 28px;
}
.ts-sq__row:active .ts-sq__thumb {
  --ts-sq-sx: 1.3;
  --ts-sq-sy: .78;
}
.ts-sq__input:focus-visible + .ts-sq__track {
  outline: 3px solid #6d28d9;
  outline-offset: 3px;
}
.ts-sq__track {
  transition: background-color .25s ease;
}
@media (prefers-reduced-motion: no-preference) {
  .ts-sq__thumb {
    transition: transform .55s cubic-bezier(.3, 1.8, .45, .9);
  }
  .ts-sq__row:active .ts-sq__thumb {
    transition-duration: .15s;
  }
}
@media (forced-colors: active) {
  .ts-sq__track {
    border: 2px solid CanvasText;
  }
  .ts-sq__thumb {
    top: 3px;
    left: 3px;
    background: CanvasText;
  }
  .ts-sq__input:checked + .ts-sq__track {
    background: Highlight;
  }
  .ts-sq__input:checked + .ts-sq__track .ts-sq__thumb {
    background: HighlightText;
  }
}

Large touch switches on smart home tiles

  • whole tile is the label
  • 44px+ target
  • :has() tile state

Living room

Click a switch, or tab to it and press Space.

Wall tablets and kitchen screens get tapped with wet or busy hands, so the switch is 76 by 44 pixels and the entire tile is its label. The status line is aria-hidden so the accessible name stays Ceiling light in both states, and :has() lights the whole tile when it is on.

View code
.ts-lg {
  width: min(100%, 460px);
  color: #ececf1;
}
.ts-lg__room {
  margin: 0 0 12px;
  font-size: 22px;
  font-weight: 700;
}
.ts-lg__grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}
.ts-lg__tile {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-height: 150px;
  padding: 16px;
  border-radius: 22px;
  background: #24262d;
  cursor: pointer;
  transition: background-color .25s ease, color .25s ease;
  -webkit-tap-highlight-color: transparent;
}
.ts-lg__name {
  font-size: 17px;
  font-weight: 700;
}
.ts-lg__state {
  color: #b8bac4;
  font-size: 13.5px;
}
.ts-lg__on {
  display: none;
}
.ts-lg__tile:has(.ts-lg__sw:checked) {
  background: #f6e7c8;
  color: #2a2112;
}
.ts-lg__tile:has(.ts-lg__sw:checked) .ts-lg__state {
  color: #5c4520;
}
.ts-lg__tile:has(.ts-lg__sw:checked) .ts-lg__on {
  display: inline;
}
.ts-lg__tile:has(.ts-lg__sw:checked) .ts-lg__off {
  display: none;
}
.ts-lg__sw {
  -webkit-appearance: none;
  appearance: none;
  position: relative;
  flex: none;
  width: 76px;
  height: 44px;
  margin: auto 0 0;
  border-radius: 999px;
  background: #8b8d97;
  cursor: pointer;
}
.ts-lg__sw::before {
  content: "";
  position: absolute;
  top: 4px;
  left: 4px;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 2px 6px rgba(0, 0, 0, .3);
}
.ts-lg__sw:checked {
  background: #a15c00;
}
.ts-lg__sw:checked::before {
  transform: translateX(32px);
}
.ts-lg__tile:has(.ts-lg__sw:focus-visible) {
  outline: 3px solid #f6c86a;
  outline-offset: 3px;
}
@media (prefers-reduced-motion: no-preference) {
  .ts-lg__sw {
    transition: background-color .2s ease;
  }
  .ts-lg__sw::before {
    transition: transform .25s cubic-bezier(.3, .7, .4, 1);
  }
}
@media (forced-colors: active) {
  .ts-lg__tile {
    border: 2px solid CanvasText;
  }
  .ts-lg__sw {
    border: 2px solid CanvasText;
  }
  .ts-lg__sw::before {
    top: 2px;
    left: 2px;
    background: CanvasText;
  }
  .ts-lg__sw:checked {
    background: Highlight;
  }
  .ts-lg__sw:checked::before {
    background: HighlightText;
  }
}

States, edge cases and the native switch

Disabled and loading states, forced colors mode, right-to-left layouts, and the switch attribute that only Safari renders so far. These are the cases tutorial code tends to skip.

Disabled and loading switches

  • aria-busy
  • disabled + reason
  • role="status"

Integrations

Calendar syncNot connected
Turned off by your workspace admin

Calendar sync needs a server round trip, so its button switch shows a spinner in the thumb with aria-busy, ignores clicks while busy and only flips aria-checked when the request settles. Read receipts is disabled, and the reason sits next to it as visible text instead of a tooltip.

View code
.ts-ld {
  width: min(100%, 440px);
  padding: 18px 18px 8px;
  border-radius: 14px;
  background: #fff;
  color: #14213d;
  box-shadow: 0 0 0 1px #dfe4ee, 0 12px 30px rgba(20, 33, 61, .07);
}
.ts-ld__title {
  margin: 0 0 6px;
  font-size: 17px;
  font-weight: 700;
}
.ts-ld__row {
  display: flex;
  align-items: center;
  gap: 14px;
  min-height: 62px;
  padding: 8px 0;
  border-top: 1px solid #edf0f5;
}
label.ts-ld__row {
  cursor: pointer;
}
.ts-ld__text {
  flex: 1;
  min-width: 0;
}
.ts-ld__text strong,
.ts-ld__lab {
  display: block;
  font-size: 15px;
  font-weight: 600;
}
.ts-ld__text small {
  color: #4a5670;
  font-size: 13px;
}
.ts-ld__sw,
.ts-ld__in {
  -webkit-appearance: none;
  appearance: none;
  position: relative;
  flex: none;
  width: 48px;
  height: 28px;
  margin: 0;
  padding: 0;
  border: 0;
  border-radius: 999px;
  background: #6b7385;
  cursor: pointer;
}
.ts-ld__thumb,
.ts-ld__in::before {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: #fff;
}
.ts-ld__sw[aria-checked="true"],
.ts-ld__in:checked {
  background: #2557d6;
}
.ts-ld__sw[aria-checked="true"] .ts-ld__thumb,
.ts-ld__in:checked::before {
  transform: translateX(20px);
}
.ts-ld__sw[aria-busy="true"] {
  background: #9aa6c2;
  cursor: progress;
}
.ts-ld__thumb::after {
  content: "";
  position: absolute;
  inset: 4px;
  border: 2.5px solid #2557d6;
  border-right-color: transparent;
  border-radius: 50%;
  opacity: 0;
}
.ts-ld__sw[aria-busy="true"] .ts-ld__thumb::after {
  opacity: 1;
}
.ts-ld__row--off .ts-ld__text {
  color: #4a5670;
}
.ts-ld__in:disabled {
  background: #c9ceda;
  cursor: not-allowed;
}
.ts-ld__in:disabled::before {
  background: #f4f6fa;
}
.ts-ld__sw:focus-visible,
.ts-ld__in:focus-visible {
  outline: 3px solid #2557d6;
  outline-offset: 2px;
}
@keyframes ts-ld-spin {
  to { transform: rotate(1turn); }
}
@media (prefers-reduced-motion: no-preference) {
  .ts-ld__sw,
  .ts-ld__in {
    transition: background-color .2s ease;
  }
  .ts-ld__thumb,
  .ts-ld__in::before {
    transition: transform .22s ease;
  }
  .ts-ld__sw[aria-busy="true"] .ts-ld__thumb::after {
    animation: ts-ld-spin .7s linear infinite;
  }
}
@media (forced-colors: active) {
  .ts-ld__sw,
  .ts-ld__in {
    border: 2px solid ButtonText;
  }
  .ts-ld__thumb,
  .ts-ld__in::before {
    top: 1px;
    left: 1px;
    background: ButtonText;
  }
  .ts-ld__sw[aria-checked="true"],
  .ts-ld__in:checked {
    background: Highlight;
  }
  .ts-ld__in:disabled {
    border-color: GrayText;
    background: Canvas;
  }
  .ts-ld__in:disabled::before {
    background: GrayText;
  }
}

High contrast switch for forced colors

  • forced-colors: active
  • system colors
  • shape cues

Captions

To test, open Chrome DevTools, Rendering, and emulate forced-colors: active.

The state is carried by shape as well as color: a solid track with a bar mark when on, an outlined track with a ring mark when off. In forced colors mode the colors are replaced with CanvasText, Canvas, Highlight and HighlightText, so the switch still reads correctly under any Windows contrast theme.

View code
.ts-fc {
  width: min(100%, 320px);
  padding: 16px 20px 10px;
  border: 3px solid #0d0d0d;
  background: #fff;
  color: #0d0d0d;
  box-shadow: 8px 8px 0 #0d0d0d;
}
.ts-fc__title {
  margin: 0 0 6px;
  font-family: ui-monospace, Consolas, monospace;
  font-size: 15px;
  font-weight: 700;
  letter-spacing: .08em;
  text-transform: uppercase;
}
.ts-fc__row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  min-height: 52px;
  border-top: 2px solid #0d0d0d;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}
.ts-fc__sw {
  -webkit-appearance: none;
  appearance: none;
  position: relative;
  flex: none;
  width: 56px;
  height: 30px;
  margin: 0;
  border: 2px solid #0d0d0d;
  border-radius: 999px;
  background: #fff;
  cursor: pointer;
}
.ts-fc__sw::before {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #0d0d0d;
}
.ts-fc__sw::after {
  content: "";
  position: absolute;
  top: 7px;
  right: 8px;
  width: 8px;
  height: 8px;
  border: 2px solid #0d0d0d;
  border-radius: 50%;
}
.ts-fc__sw:checked {
  background: #0d0d0d;
}
.ts-fc__sw:checked::before {
  background: #fff;
  transform: translateX(26px);
}
.ts-fc__sw:checked::after {
  top: 7px;
  right: auto;
  left: 11px;
  width: 0;
  height: 12px;
  border-color: #fff;
  border-width: 0 0 0 2px;
  border-radius: 0;
}
.ts-fc__sw:focus-visible {
  outline: 3px solid #0d0d0d;
  outline-offset: 3px;
}
@media (prefers-reduced-motion: no-preference) {
  .ts-fc__sw::before {
    transition: transform .18s ease;
  }
}
@media (forced-colors: active) {
  .ts-fc {
    border-color: CanvasText;
    box-shadow: none;
  }
  .ts-fc__row {
    border-color: CanvasText;
  }
  .ts-fc__sw {
    border-color: CanvasText;
    background: Canvas;
  }
  .ts-fc__sw::before {
    background: CanvasText;
  }
  .ts-fc__sw::after {
    border-color: CanvasText;
  }
  .ts-fc__sw:checked {
    border-color: Highlight;
    background: Highlight;
  }
  .ts-fc__sw:checked::before {
    background: HighlightText;
  }
  .ts-fc__sw:checked::after {
    border-color: HighlightText;
  }
  .ts-fc__sw:focus-visible {
    outline-color: CanvasText;
  }
}

Right-to-left switch

  • logical properties
  • :dir(rtl)
  • one stylesheet

Settings

الإعدادات

Both cards use the same CSS. The thumb starts at inset-inline-start, and a custom property set through :dir(rtl) flips the sign of the translate, so on is to the left in Arabic and to the right in English, matching the reading direction.

View code
.ts-rtl {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 230px), 1fr));
  gap: 14px;
  width: min(100%, 560px);
}
.ts-rtl__card {
  padding: 14px 18px 8px;
  border-radius: 16px;
  background: #fff;
  color: #1b2a3a;
  box-shadow: 0 0 0 1px #dde3ea;
}
.ts-rtl__title {
  margin: 0 0 4px;
  font-size: 17px;
  font-weight: 700;
}
.ts-rtl__row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 50px;
  border-top: 1px solid #edf1f5;
  font-size: 15px;
  cursor: pointer;
}
.ts-rtl__sw {
  --ts-rtl-dir: 1;
  -webkit-appearance: none;
  appearance: none;
  position: relative;
  flex: none;
  width: 48px;
  height: 28px;
  margin: 0;
  border-radius: 999px;
  background: #6a7685;
  cursor: pointer;
}
.ts-rtl__sw:dir(rtl) {
  --ts-rtl-dir: -1;
}
.ts-rtl__sw::before {
  content: "";
  position: absolute;
  top: 3px;
  inset-inline-start: 3px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: #fff;
}
.ts-rtl__sw:checked {
  background: #0e7490;
}
.ts-rtl__sw:checked::before {
  transform: translateX(calc(20px * var(--ts-rtl-dir)));
}
.ts-rtl__sw:focus-visible {
  outline: 3px solid #0e7490;
  outline-offset: 2px;
}
@media (prefers-reduced-motion: no-preference) {
  .ts-rtl__sw {
    transition: background-color .2s ease;
  }
  .ts-rtl__sw::before {
    transition: transform .22s ease;
  }
}
@media (forced-colors: active) {
  .ts-rtl__sw {
    border: 2px solid CanvasText;
  }
  .ts-rtl__sw::before {
    top: 1px;
    inset-inline-start: 1px;
    background: CanvasText;
  }
  .ts-rtl__sw:checked {
    background: Highlight;
  }
  .ts-rtl__sw:checked::before {
    background: HighlightText;
  }
}

Native switch attribute with fallback

  • input switch
  • accent-color
  • feature detection

Built-in look

Styled with appearance: none

Support check runs with JavaScript.

Safari 17.4 and later draw the left column as real switches, and every other browser shows checkboxes, which still work. The right column adds appearance: none and a drawn thumb, so it looks the same everywhere. role="switch" is there for the browsers that ignore the attribute.

View code
.ts-nat {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 220px), 1fr));
  gap: 14px;
  width: min(100%, 560px);
  color: #1d2433;
}
.ts-nat__col {
  padding: 14px 18px 8px;
  border-radius: 14px;
  background: #fff;
  box-shadow: 0 0 0 1px #dfe3eb;
}
.ts-nat__head {
  margin: 0 0 4px;
  color: #4b5568;
  font-family: ui-monospace, Consolas, monospace;
  font-size: 12.5px;
}
.ts-nat__row {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 48px;
  border-top: 1px solid #eef1f5;
  font-size: 15px;
  cursor: pointer;
}
.ts-nat__auto {
  width: 22px;
  height: 22px;
  margin: 0;
  accent-color: #c2410c;
  cursor: pointer;
}
.ts-nat--native .ts-nat__auto {
  width: auto;
  height: auto;
}
.ts-nat__ctl {
  position: relative;
  display: inline-block;
  flex: none;
  width: 46px;
  height: 28px;
}
.ts-nat__styled {
  -webkit-appearance: none;
  appearance: none;
  display: block;
  width: 46px;
  height: 28px;
  margin: 0;
  border-radius: 999px;
  background: #6b7385;
  cursor: pointer;
}
.ts-nat__thumb {
  position: absolute;
  top: 3px;
  left: 3px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: #fff;
  pointer-events: none;
}
.ts-nat__styled:checked {
  background: #c2410c;
}
.ts-nat__styled:checked + .ts-nat__thumb {
  transform: translateX(18px);
}
.ts-nat__auto:focus-visible,
.ts-nat__styled:focus-visible {
  outline: 3px solid #c2410c;
  outline-offset: 2px;
}
.ts-nat__status {
  grid-column: 1 / -1;
  margin: 0;
  padding: 10px 14px;
  border-radius: 10px;
  background: #fff4ec;
  color: #7c2d12;
  font-size: 13.5px;
  line-height: 1.45;
}
@media (prefers-reduced-motion: no-preference) {
  .ts-nat__styled {
    transition: background-color .2s ease;
  }
  .ts-nat__thumb {
    transition: transform .22s ease;
  }
}
@media (forced-colors: active) {
  .ts-nat__styled {
    border: 2px solid CanvasText;
  }
  .ts-nat__thumb {
    background: CanvasText;
  }
  .ts-nat__styled:checked {
    background: Highlight;
  }
  .ts-nat__styled:checked + .ts-nat__thumb {
    background: HighlightText;
  }
}

What Is a CSS Toggle Switch and How Does It Work?

It’s a checkbox wearing a different skin. CSS draws a track and a thumb over it, while the checkbox underneath still decides whether the thing is on or off.

The markup is a label, an input and one decorative span, all plain HTML.

Since the state sits in the checkbox, the switch flips before any script has loaded. CSS only reads it.

How the :checked state drives the switch

The checkbox stores the real value and responds to the Space key by itself. A label wrapped around it passes every click or tap through, so the whole control is clickable, and one sibling selector, input:checked + .track, restyles the track and thumb whenever the box is ticked.

MDN Web Docs lists :checked as Baseline widely available, supported across browsers since July 2015.

You don’t need a fallback or a feature check for it. The adjacent sibling combinator (+) wants the styled element right after the input in the markup, while the general sibling combinator (~) accepts any later sibling.

Visual state and real state are the same thing here, so they can’t drift apart. Nothing on the page has to keep two values in sync.

Switch, checkbox or toggle button

Choose by what the control means to the user. How it looks matters less.

ControlCommunicatesFits
SwitchOn or off, applied right awaySettings such as notifications or dark mode
CheckboxSelected or not, can be partialOptions sent with a form, list selection
Toggle buttonPressed or not pressedA mode, such as bold in an editor

Apple’s Human Interface Guidelines describe a toggle as a choice between a pair of opposing states, and their macOS guidance generally advises against swapping a checkbox for a switch.

The WebKit team’s post on the Safari 17.4 switch control lands in the same place. A checkbox suits something the user thinks of as an item to select.

How to Build a CSS Toggle Switch Step by Step

  1. Wrap a checkbox and a track element inside a label.
  2. Hide the checkbox visually while keeping it focusable.
  3. Draw the track as a pill and the thumb with ::before.
  4. Move the thumb and recolor the track under :checked.

Write the markup

Everything goes inside the label. Its text becomes the checkbox’s accessible name, and a click anywhere on it toggles the box.

<label class="toggle">
  <input class="toggle__input" type="checkbox">
  <span class="toggle__track"></span>
  <span class="toggle__text">Notifications</span>
</label>

Keep the track directly after the input. The selector in the last step relies on that order.

Hide the checkbox without losing keyboard access

The obvious ways to hide the input, display: none and visibility: hidden, both drop it from the tab order. Anyone who isn’t using a mouse can no longer operate the switch.

Clip the input to a 1px box and position it absolutely instead. It stays focusable and takes up no space.

.toggle {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 0.75rem;
  cursor: pointer;
}
.toggle__input {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

Alvaro Montoro’s walkthrough names the two mistakes that come up most. One is hiding the input where the keyboard can’t reach it, and the other is removing the focus style.

Most other styled checkbox designs rest on this same hiding pattern, custom radio buttons included.

Draw the track and thumb

The track is a pill. The thumb is a circle drawn with ::before, so you don’t need another element in the markup.

Set the track to 3rem wide and 1.75rem tall with a 999px radius. The thumb is a 1.25rem circle inset 0.25rem on every side, which leaves 1.25rem of travel (track width minus thumb minus both insets).

.toggle__track {
  position: relative;
  flex: none;
  width: 3rem;
  height: 1.75rem;
  border-radius: 999px;
  background: #767676;
}
.toggle__track::before {
  content: "";
  position: absolute;
  top: 0.25rem;
  left: 0.25rem;
  width: 1.25rem;
  height: 1.25rem;
  border-radius: 50%;
  background: #fff;
}

A border radius generator previews pill shapes fast, though 999px does the same job without one.

Apply the checked state

Recolor the track first, then slide the thumb across by the travel distance.

.toggle__input:checked + .toggle__track {
  background: #1a73e8;
}
.toggle__input:checked + .toggle__track::before {
  transform: translateX(1.25rem);
}
.toggle__input:focus-visible + .toggle__track {
  outline: 3px solid #1a73e8;
  outline-offset: 2px;
}

Don’t skip the focus-visible rule. A keyboard user who tabs onto the hidden checkbox would otherwise see nothing happen.

Styling the Track, Thumb and States

The shape of a switch comes down to track width, track height, thumb size and thumb inset. After that it’s just color and state.

Size and shape

Bootstrap’s 6.0 documentation (an alpha, released October 2026) sets switch width at 1.75 times switch height, with 0.0625rem of padding inside the track. Bootstrap’s switch component exposes those values as CSS variables.

That ratio works as a starting point for your own version. Thumb diameter is track height minus twice the inset, and keeping the inset equal on all sides centers the thumb. A radius of half the track height gives a pill, and any larger value gives the same shape.

Ionut Colceriu’s css-toggle-switch library builds in em by default, so each switch scales with its parent’s font size. Separate rem and px stylesheets cover fixed sizes.

Which one you want depends on context, and there’s a longer breakdown of em or rem units if you need it. Em suits a switch sitting beside text that should grow with it. Rem keeps one size across the page.

Colors and custom properties

Put every size and color into custom properties on the label and override them per variant.

.toggle {
  --toggle-width: 3rem;
  --toggle-height: 1.75rem;
  --toggle-inset: 0.25rem;
  --toggle-off: #767676;
  --toggle-on: #1a73e8;
  --toggle-thumb: #fff;
}
.toggle__track {
  width: var(--toggle-width);
  height: var(--toggle-height);
  background: var(--toggle-off);
}
.toggle__track::before {
  top: var(--toggle-inset);
  left: var(--toggle-inset);
  width: calc(var(--toggle-height) - 2 * var(--toggle-inset));
  height: calc(var(--toggle-height) - 2 * var(--toggle-inset));
  background: var(--toggle-thumb);
}
.toggle__input:checked + .toggle__track {
  background: var(--toggle-on);
}
.toggle__input:checked + .toggle__track::before {
  transform: translateX(calc(var(--toggle-width) - var(--toggle-height)));
}

Travel distance reduces to width minus height. Change either one and the thumb still ends up flush against the far inset.

Google’s Material Web component uses the same split, with tokens like –md-switch-track-color and –md-switch-selected-track-color. Its shape tokens default to a fully rounded corner.

The accent-color property only tints native controls, so it does nothing here. A hand-built switch gets its colors from your own rules.

Disabled, hover and focus states

Disabled gets lower opacity and cursor: not-allowed on input:disabled + .toggle__track. The disabled attribute already blocks clicks and keyboard focus, so that rule is purely cosmetic.

Hover is optional. If you add it, darken the off color a little and wrap the rule in @media (hover: hover) so touch screens ignore it. Focus keeps the :focus-visible outline from the build step, with a 2px offset so it doesn’t merge into the track edge.

The :has() selector can restyle the whole row from the checkbox state, as in label:has(input:checked). MDN lists :has() as Baseline widely available, supported across browsers since December 2023, and a browser that lacks it drops the entire rule.

Dark mode

A dark theme only needs new values for the same custom properties.

@media (prefers-color-scheme: dark) {
  .toggle {
    --toggle-off: #9e9e9e;
    --toggle-on: #8ab4f8;
    --toggle-thumb: #121212;
  }
}

Check the track against the dark background again. A color that stands out on white won’t always stand out on near-black.

How to Animate the Thumb Without Layout Shift

Animate transform on the thumb and background-color on the track. Leave left and margin alone.

Google’s web.dev guide says to limit animations to transform and opacity where you can, since that keeps them on the compositing stage of the rendering path. A companion guide adds that these animations can run on the compositor thread, so they keep moving even when the main thread is busy.

The slide itself is a micro-interaction, and it exists to confirm the tap.

PropertyWork per frameVerdict
transformCompositing onlyUse for the thumb
background-colorPaintFine for the track
left or margin-leftLayout, paint, compositeAvoid for the thumb

A transition handles both directions on its own, so a keyframe animation adds code and no extra control.

.toggle__track {
  transition: background-color 0.2s ease;
}
.toggle__track::before {
  transition: transform 0.2s ease;
}

The classic slip is declaring the transition only inside the :checked rule. Checking animates, unchecking snaps, so put it on the base rule.

The pseudo-element is another trap. A transition on the track doesn’t move the ::before thumb, which needs its own declaration. And transition: all will animate properties you never meant to touch, so name the ones you want.

MDN says the prefers-reduced-motion media query is Baseline widely available and has been supported across browsers since January 2020. The value to match is reduce, not none.

WCAG goes further in SC 2.3.3 (Level AAA): motion triggered by the user’s own interaction should be possible to turn off, unless the animation is essential to the functionality or the information.

@media (prefers-reduced-motion: reduce) {
  .toggle__track,
  .toggle__track::before {
    transition: none;
  }
}

Color and position still change when the switch flips. Only the slide goes away, so nothing is lost.

How to Make a CSS Toggle Switch Accessible

Use a real checkbox with a visible label, and make sure the focus ring survives hiding the input. That handles most of it.

Whether to add role=”switch” is where the sources disagree.

Native checkbox or role=”switch”

The W3C ARIA Authoring Practices Guide puts role=”switch” on an input type=”checkbox” in its checkbox-based switch example and leans on the native checked state instead of aria-checked. Bootstrap’s switch markup does the same. Some accessibility writers take the opposite view and advise against adding a redundant aria-checked or role=”switch” to a native checkbox switch.

It’s a trade between announcement and minimal ARIA. The APG notes that a screen reader reports a role=”switch” control as a switch with an on or off state, which fits a setting better than a checked checkbox does.

That same guide warns that careless ARIA does more harm than none, and it labels its examples as illustrations to test with assistive technology before production.

My pick is role=”switch” on the input with aria-checked left off. The role changes what gets announced, and the native checked state already supplies the value.

The role also gives CSS something to hook onto. Alvaro Montoro styles input[role=”switch”] separately from plain checkboxes, so a single stylesheet can serve both.

ARIA attributes really earn their place when the switch is built from a button or a div. MDN lists aria-checked as required for the switch role, and a script has to flip it on every toggle.

Keyboard and focus

A real checkbox gives you keyboard behavior for free, which is the strongest argument against rebuilding the control from a div.

KeyResultRequirement
TabMoves focus to the switchRequired
SpaceToggles on and offRequired
EnterToggles on and offOptional in the APG pattern

The APG’s checkbox-based switch example documents only Tab and Space, so a native switch needs no extra key handling.

Draw the focus ring on the track, since the input itself is clipped to 1px. The ring counts as part of the component, so it needs the same 3:1 contrast as the track.

WCAG 2.2 values to meet

  • Components and their states need 3:1 contrast against adjacent colors (SC 1.4.11, Level AA).
  • Targets should measure at least 24 by 24 CSS pixels (SC 2.5.8, Level AA).
  • An undersized target still passes when a 24 CSS pixel circle centered on it intersects no other target and no circle of another undersized target (SC 2.5.8).
  • Inactive components are exempt from the contrast requirement, so a disabled switch can go lighter (SC 1.4.11).

The track from the build step is 3rem by 1.75rem, which works out to 48 by 28 CSS pixels at the default 16px root size. Both dimensions clear the 24 pixel minimum.

A small variant under 1.5rem tall falls below it and needs the spacing exception. Measure the off track and the on track against the page background separately, because each state has to meet the color contrast threshold on its own.

Toggle Switch Methods Compared

Build on the label and hidden checkbox pattern and treat the native switch attribute as an add-on. As of October 2026 only Safari renders it.

MethodBrowser supportMain drawback
Label with hidden checkboxAll current browsersExtra track element
Styled input with appearance: noneAcross browsers since March 2022 (MDN)Older browsers show a plain checkbox
Native switch attributeSafari 17.4 and later onlyChrome, Edge and Firefox show a plain checkbox
Framework componentSame as the CSS underneathAdds a dependency for one control

WebDX browser data puts the switch attribute at limited availability. Safari 17.4, released 2024-03-05, is the only engine shipping it, and Chrome, Edge and Firefox are marked not supported.

Styling the input directly with appearance: none strips the native look but leaves behavior alone, and MDN notes that a label click still toggles the checkbox. Go Make Things builds Kelp UI’s switch this way, using ::after for the thumb and label:has([role=”switch”]) for the label layout.

The native attribute behaves oddly in practice. WebKit says a browser without support treats input type=”checkbox” switch as a plain checkbox. In Safari the control reports through the ARIA switch role as On or Off, and appearance: none hands the styling back to you.

At launch WebKit exposed the ::thumb and ::track pseudo-elements only behind a Safari feature flag. Bootstrap’s documentation adds the attribute for one reason, haptic feedback on iOS 17.4 and later.

Bootstrap’s form-switch class (just .switch in the 6.0 alpha) and Tailwind toggle components wrap the same techniques. Tailwind’s peer marker and peer-checked variant write the sibling rule for you, with the input hidden.

Reach for a framework version only if the framework already loads on the page. A new dependency costs more than the CSS it replaces.

With one engine rendering the native switch, the same page looks different from browser to browser. So cross-browser compatibility testing belongs inside the build and not at the end of it.

When a CSS-Only Toggle Switch Falls Short

A CSS-only toggle switch changes how a setting looks and nothing beyond that. It stores no value, applies no setting and sends nothing to the server.

The page keeps no record of the choice, so saving it takes a form submission or a script. A switch that reveals content can run on :checked alone, but one that triggers a request or changes data can’t.

Custom elements make it worse. A button or div with role=”switch” has no built-in state, and MDN says the developer must update aria-checked on every toggle. MDN’s switch role also doesn’t support the mixed value of aria-checked, so a partial selection needs a checkbox.

Forced colors mode is the failure tutorial code rarely shows. It’s a web accessibility setting where the browser swaps authored colors for system colors.

The track’s background-color gets overridden, so a switch that signals state only through color shows no difference between on and off. Go Make Things documents the fix, credited to Stephanie Eckles. System color keywords such as CanvasText survive as backgrounds.

@media (forced-colors: active) {
  .toggle__track {
    border: 2px solid CanvasText;
  }
  .toggle__track::before {
    background: CanvasText;
  }
  .toggle__input:checked + .toggle__track {
    background: CanvasText;
  }
  .toggle__input:checked + .toggle__track::before {
    background: Canvas;
  }
}

Put the block after the base rules so it wins the cascade. The on state then shows a filled track with a light thumb, and the off state shows an outlined track with a dark one.

GOV.UK Frontend ran into the same class of problem. The focus state on its checkboxes and radios was barely visible in forced colors mode, and a transparent outline, which turns solid in that mode, fixed it (pull request 2264).

As a rule of thumb, CSS owns the appearance and script owns anything that has to persist or leave the page.

CSS Toggle Switch FAQ

How do I show ON and OFF text inside the switch?

Put two spans inside the track, one reading ON and one reading OFF, and reveal the matching one with the :checked rule. Mark both aria-hidden=”true”, as the W3C APG examples do, since the control already announces its own state.

Which values change for small and large sizes?

Only the width and height custom properties. Add a modifier class such as .toggle–small that overrides –toggle-width and –toggle-height, and the thumb size and travel distance follow through the calc() rules.

How do I read the switch state in JavaScript?

Listen for the change event on the input and read its checked property. Change fires for mouse clicks, label taps and the Space key alike, so one handler covers every input method.

Can a dark mode switch remember the visitor’s choice without JavaScript?

No. The prefers-color-scheme media query supplies the default, but CSS stores nothing. A short script has to save the choice in localStorage and apply it as an attribute on the html element.

Does the switch send a value when a form is submitted?

Yes, but only while it’s on. A checked checkbox submits its name with its value attribute (on by default), and an unchecked one submits nothing, so the server has to treat a missing field as off.

Testing a CSS Toggle Switch Before It Ships

Start with the keyboard. Tab to the switch and flip it with Space. Then listen for the label and the on or off state in a screen reader, and finally switch on forced colors mode to see whether on and off still look different.

If the keyboard test fails, the others don’t matter yet. A switch that can’t take focus can’t be announced, and its focus ring can’t be checked.

The hidden-checkbox pattern stays the base layer until Chrome, Edge or Firefox ships the native switch attribute (position verified October 2026). After that, the pattern becomes the fallback.

The web accessibility checklist covers the rest of the page the switch sits in.