Toggle Switch
An on/off switch with a sliding thumb for settings that apply immediately.
AI prompt
Create a toggle switch using a real input type="checkbox" with role="switch" visually hidden but focusable, and a label wrapper. The track is 52x32px with a 16px radius; off colour is the surface-2 tone with a border, on colour is the accent. The thumb is a 24px circle at 4px inset that translates 20px on checked, with 220ms cubic-bezier(0.3,1.3,0.5,1). While pressed the thumb widens to 28px. Show a 4px halo on hover and a 2px focus ring (via :focus-visible on the hidden input using sibling selectors). Add a text "On"/"Off" next to the label updated with JS and aria-live not required. Provide a disabled example at 40% opacity. Clicking the label text toggles the switch. Remove transitions under prefers-reduced-motion.About this pattern
Definition
A binary control that looks like a physical switch. A circular thumb slides along a track, and the track changes colour to show the on state. It is used for settings that take effect immediately.
Purpose & UX principles
- Use a switch for instant settings ("Notifications on") and a checkbox for choices that are submitted later with a form.
- The label states the setting, not the action. "Dark mode" is clearer than "Toggle dark mode".
- Do not rely on colour alone: the thumb position and an optional On/Off text also show the state.
Interaction states
- Off, on, hover (thumb halo), pressed (thumb stretches slightly), focus-visible ring, disabled.
- Built on a real
input type="checkbox" role="switch"so it is keyboard and screen-reader accessible (Space toggles).
Motion specification
- Thumb: translateX 220ms cubic-bezier(0.3, 1.3, 0.5, 1).
- Pressed thumb stretches from 24px to 28px wide.
- Track colour: 180ms.
- Reduced motion: no slide, state changes instantly.
Responsive behaviour
- Track 52×32px so the hit area is comfortable; the whole row including the label is clickable.
Code
<div class="list">
<label class="row"><span>Notifications<small id="s1">Off</small></span><input type="checkbox" role="switch" class="sw"><i class="trk"><b></b></i></label>
<label class="row"><span>Dark mode<small id="s2">On</small></span><input type="checkbox" role="switch" class="sw" checked><i class="trk"><b></b></i></label>
<label class="row dis"><span>Beta features<small>Unavailable</small></span><input type="checkbox" role="switch" class="sw" disabled><i class="trk"><b></b></i></label>
</div>:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
.list{width:min(340px,100%);border:1px solid var(--line);border-radius:18px;background:var(--surface);overflow:hidden}
.row{display:flex;align-items:center;justify-content:space-between;gap:16px;padding:14px 18px;cursor:pointer;border-bottom:1px solid var(--line);position:relative}
.row:last-child{border:0}
.row span{display:flex;flex-direction:column;font-size:15px}
.row small{color:var(--muted);font-size:12.5px}
.sw{position:absolute;opacity:0;width:1px;height:1px}
.trk{position:relative;flex:none;width:52px;height:32px;border-radius:16px;background:var(--surface-2);border:1px solid var(--line);transition:background .18s,border-color .18s}
.trk b{position:absolute;top:3px;left:3px;width:24px;height:24px;border-radius:14px;background:#fff;box-shadow:0 1px 4px rgba(0,0,0,.28);transition:transform .22s cubic-bezier(.3,1.3,.5,1),width .12s}
.sw:checked~.trk{background:var(--accent);border-color:var(--accent)}
.sw:checked~.trk b{transform:translateX(20px)}
.row:active .sw:not(:disabled)~.trk b{width:28px}
.row:active .sw:checked:not(:disabled)~.trk b{transform:translateX(16px)}
.row:hover .sw:not(:disabled)~.trk b{box-shadow:0 0 0 4px rgba(0,0,0,.08),0 2px 6px rgba(0,0,0,.18)}
.sw:focus-visible~.trk{outline:2px solid var(--accent);outline-offset:3px}
.dis{opacity:.4;cursor:not-allowed}
@media (prefers-reduced-motion:reduce){.trk,.trk b{transition:none}}[['s1',0],['s2',1]].forEach(([id,i])=>{const sw=document.querySelectorAll('.sw')[i],t=document.getElementById(id);sw.addEventListener('change',()=>{t.textContent=sw.checked?'On':'Off';});});One complete HTML file. Save it and open it in a browser.
<!doctype html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Toggle Switch — demo</title>
<style>
:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
html,body{height:100%}body{background:var(--bg);color:var(--text);font:15px/1.5 -apple-system,BlinkMacSystemFont,'Segoe UI',Inter,Roboto,sans-serif;-webkit-font-smoothing:antialiased}.stage{min-height:100%;display:grid;place-items:center;padding:24px}button,input,select,textarea{font-family:inherit}@media (prefers-reduced-motion:reduce){html{scroll-behavior:auto}}
.list{width:min(340px,100%);border:1px solid var(--line);border-radius:18px;background:var(--surface);overflow:hidden}
.row{display:flex;align-items:center;justify-content:space-between;gap:16px;padding:14px 18px;cursor:pointer;border-bottom:1px solid var(--line);position:relative}
.row:last-child{border:0}
.row span{display:flex;flex-direction:column;font-size:15px}
.row small{color:var(--muted);font-size:12.5px}
.sw{position:absolute;opacity:0;width:1px;height:1px}
.trk{position:relative;flex:none;width:52px;height:32px;border-radius:16px;background:var(--surface-2);border:1px solid var(--line);transition:background .18s,border-color .18s}
.trk b{position:absolute;top:3px;left:3px;width:24px;height:24px;border-radius:14px;background:#fff;box-shadow:0 1px 4px rgba(0,0,0,.28);transition:transform .22s cubic-bezier(.3,1.3,.5,1),width .12s}
.sw:checked~.trk{background:var(--accent);border-color:var(--accent)}
.sw:checked~.trk b{transform:translateX(20px)}
.row:active .sw:not(:disabled)~.trk b{width:28px}
.row:active .sw:checked:not(:disabled)~.trk b{transform:translateX(16px)}
.row:hover .sw:not(:disabled)~.trk b{box-shadow:0 0 0 4px rgba(0,0,0,.08),0 2px 6px rgba(0,0,0,.18)}
.sw:focus-visible~.trk{outline:2px solid var(--accent);outline-offset:3px}
.dis{opacity:.4;cursor:not-allowed}
@media (prefers-reduced-motion:reduce){.trk,.trk b{transition:none}}
</style></head>
<body><div class="stage">
<div class="list">
<label class="row"><span>Notifications<small id="s1">Off</small></span><input type="checkbox" role="switch" class="sw"><i class="trk"><b></b></i></label>
<label class="row"><span>Dark mode<small id="s2">On</small></span><input type="checkbox" role="switch" class="sw" checked><i class="trk"><b></b></i></label>
<label class="row dis"><span>Beta features<small>Unavailable</small></span><input type="checkbox" role="switch" class="sw" disabled><i class="trk"><b></b></i></label>
</div>
</div>
<script>
[['s1',0],['s2',1]].forEach(([id,i])=>{const sw=document.querySelectorAll('.sw')[i],t=document.getElementById(id);sw.addEventListener('change',()=>{t.textContent=sw.checked?'On':'Off';});});
</script>
</body></html>