Components
Toggle switch animation in CSS: an accessible switch with a springy thumb
Build an accessible animated toggle switch in CSS: role="switch" button, sliding thumb with squash on press, springy easing, three live variants and React code.
A toggle switch is one of the smallest animated components on a page, and one of the most used. It is also easy to get slightly wrong: a thumb that slides at constant speed, a colour change that lags behind, or a switch that looks right but announces itself to screen readers as a plain button.
This guide builds an accessible switch on a real <button>, with a thumb that slides on transform, stretches a little while pressed, settles with a spring, and a track that changes colour in step. There are three variants in the demo, the timing for each part, a React version, and a reduced motion rule.
How the animation is put together
The switch has two moving parts, the thumb and the track, plus a press state that changes the thumb's width. Everything is a CSS transition between two states, so it reverses cleanly when you click again mid-animation.
| Part | Property | Duration | Delay | Easing |
|---|---|---|---|---|
| Thumb slide | transform: translateX(0 → 20px) | 340 ms | 0 | cubic-bezier(.34, 1.56, .64, 1) |
| Thumb slide (bounce) | transform: translateX(0 → 20px) | 500 ms | 0 | linear() spring |
| Thumb press | width: 22px → 28px | 160 ms | 0 | ease-out |
| Track colour | background-color | 220 ms | 0 | ease-out |
| Icon swap | opacity, transform: scale(.4) rotate() | 180 ms / 240 ms | 0 | ease-out / overshoot |
The track colour finishes before the thumb settles. That is deliberate: the colour tells you the new state, and the thumb's small overshoot is the part that gives the switch its feel. If the colour lagged behind the thumb, the switch would look undecided for a moment.
The HTML
<span id="notif-label">Email notifications</span>
<button class="switch" type="button" role="switch"
aria-checked="false" aria-labelledby="notif-label">
<span class="switch-thumb"></span>
</button>
A button with role="switch" is announced by screen readers as a switch, with its state read from aria-checked. It is focusable by default and toggles with Space and Enter, because those keys fire a click on any button. The label is real text next to it, linked with aria-labelledby, so clicking or reading the label makes sense.
If the switch sits inside a form that is submitted, use a checkbox instead: <input type="checkbox" role="switch">. The CSS below works the same with :checked in place of [aria-checked="true"]; style a sibling element as the track and hide the native box with appearance: none or by drawing the switch on the input itself.
The CSS
.switch {
position: relative;
width: 48px; height: 28px;
padding: 0; border: 0; border-radius: 999px;
background: #3f3f46;
cursor: pointer;
transition: background-color 220ms ease-out;
}
.switch[aria-checked="true"] { background: #fb9f3d; }
.switch:focus-visible { outline: 2px solid #fb9f3d; outline-offset: 3px; }
.switch-thumb {
position: absolute; top: 3px; left: 3px;
width: 22px; height: 22px; border-radius: 999px;
background: #fff;
box-shadow: 0 1px 3px rgba(0, 0, 0, .35);
transition:
transform 340ms cubic-bezier(.34, 1.56, .64, 1),
width 160ms ease-out;
}
/* on: 48 - 22 - 3 - 3 = 20px of travel */
.switch[aria-checked="true"] .switch-thumb { transform: translateX(20px); }
/* press: stretch the thumb toward the direction it will travel */
.switch:active .switch-thumb { width: 28px; }
.switch[aria-checked="true"]:active .switch-thumb { transform: translateX(14px); }
@media (prefers-reduced-motion: reduce) {
.switch-thumb { transition: none; }
.switch:active .switch-thumb { width: 22px; }
.switch[aria-checked="true"]:active .switch-thumb { transform: translateX(20px); }
}
The travel distance is the track width minus the thumb width minus the padding on both sides. Change the size and you change that one number.
The press stretch is the detail that makes a switch feel physical. While the pointer is down, the thumb widens by 6 px. When it is off, it grows to the right, toward where it is about to go. When it is on, the translateX drops by the same 6 px so it grows to the left instead. On release the thumb shrinks back to a circle at the same time as it slides, which reads as a small squash and stretch.
The width change does trigger layout, but only for one small absolutely positioned element, which is cheap. The slide itself stays on transform. If you would rather avoid layout entirely, use scale on a pseudo-element behind the thumb instead, though scaling a circle on one axis turns it into an ellipse rather than a pill.
Choosing the easing
A toggle thumb only moves 20 px, so the easing matters more than the duration. With ease it looks mechanical. With a curve that overshoots, it looks like it has some mass.
cubic-bezier(.34, 1.56, .64, 1): the second control point goes above 1, so the thumb passes its end point by a pixel or two and comes back. One overshoot, no oscillation, works everywhere.linear()spring: lets you describe a real bounce with several points. The curve in the demo overshoots by 8%, dips slightly under, and settles. It needs a longer duration, around 500 ms, because the settling happens in the second half. Browsers withoutlinear()support drop the whole declaration, so declare acubic-bezier()transition first and thelinear()one after it as an override. The linear() guide shows how to generate these curves.- Track colour: plain
ease-out. Colour should not overshoot; a bouncing colour just looks like a flicker.
.switch--bounce .switch-thumb {
/* fallback first */
transition: transform 340ms cubic-bezier(.34, 1.56, .64, 1), width 160ms ease-out;
transition: transform 500ms linear(0, 0.13 4%, 0.44 10%, 0.79 18%, 1.03 28%,
1.08 34%, 1.06 42%, 1 55%, 0.99 68%, 1), width 160ms ease-out;
}
For more on how overshoot curves are built, see CSS easing functions explained.
The JavaScript
document.querySelectorAll('.switch[role="switch"]').forEach(btn => {
btn.addEventListener('click', () => {
const on = btn.getAttribute('aria-checked') !== 'true';
btn.setAttribute('aria-checked', String(on));
// save the setting here
});
});
The script only flips the attribute. The animation follows from the CSS, and because aria-checked is both the style hook and the accessible state, the visual and the announced state can never disagree.
The React version
import { useId, useState } from 'react';
export function Switch({ label, defaultChecked = false, onChange }) {
const [checked, setChecked] = useState(defaultChecked);
const id = useId();
function toggle() {
const next = !checked;
setChecked(next);
onChange?.(next);
}
return (
<div className="switch-row">
<span id={id}>{label}</span>
<button
type="button"
role="switch"
className="switch"
aria-checked={checked}
aria-labelledby={id}
onClick={toggle}
>
<span className="switch-thumb" />
</button>
</div>
);
}
React renders aria-checked={checked} as the string "true" or "false", so the same stylesheet works without changes. If you use Framer Motion elsewhere, you can animate the thumb with animate={{ x: checked ? 20 : 0 }} and a spring transition such as { type: 'spring', stiffness: 700, damping: 30 }; the keyframes to Framer Motion guide covers the mapping.
Accessibility notes
- Use
role="switch"for settings that take effect immediately. If the change only applies after a Save button, a checkbox is the better pattern. - Do not rely on colour alone. The thumb position already carries the state; the icon variant adds a check and a cross for people who cannot tell orange from grey.
- Keep the visible focus ring. The outline offset in the CSS keeps it clear of the track.
- Make the hit area at least 24 × 24 CSS pixels. At 48 × 28 this switch passes; if you make it smaller, extend the target with padding or let the whole row toggle it.
- Under
prefers-reduced-motion: reducethe thumb jumps to its new position and the press stretch is removed. The track colour still changes, which is a state change rather than motion.
Copy one you already like
Toggles differ in small ways that are hard to see at full speed: how far the thumb stretches, how long the colour takes, whether there is any overshoot. If you have found one you want to match, open the page with the Capture Motion extension, start a capture and click the switch. It records the keyframes, durations, delays and easing of the thumb and track, and exports them as CSS, React or GSAP code, or as JSON for a Figma Motion timeline. You can also start from a ready component in the gallery and customise it there.
Questions
Should a toggle switch be a checkbox or a button?
Either works if the semantics are right. Use a button with role="switch" and aria-checked when the change applies immediately, or a checkbox input with role="switch" when it lives inside a form. Both are focusable and toggle with the keyboard.
How do I make the thumb feel springy?
Use an easing curve that overshoots, such as cubic-bezier(.34, 1.56, .64, 1), or a linear() curve with a small bounce. Keep the overshoot to a few pixels so the thumb settles quickly.
Why animate transform instead of left for the thumb?
Transform does not trigger layout, so the browser can animate it cheaply and smoothly. Animating left or margin recalculates layout on every frame.
Can I copy the toggle animation from another website?
Yes. Start a Capture Motion capture, click the toggle while it records, and it reads the keyframes, duration and easing of the thumb and track. You can export them as CSS, React or GSAP code.
