What is a flip switch?
A flip switch, also called a toggle switch, is a control that slides between two positions: on and off. You'll find them all over phones and websites, for settings like Wi-Fi, dark mode or email notifications.
In this tutorial we'll build a flip switch with HTML and CSS, step by step. Once you understand how it works, you'll be able to design your own with ease, or you can use our CSS flip switch generator to create one visually.
Here's what we'll build. Click the switches, or press Tab to reach them and Space to flip them:
Switch or checkbox?
A switch and a checkbox both store a yes-or-no choice, but they suggest different things:
- Use a switch for settings that take effect immediately, like turning on dark mode.
- Use a checkbox for choices that are confirmed later, such as ticking "I agree to the terms" before pressing a Submit button.
Step 1: the HTML
Our switch is built on a real checkbox, so it works with the keyboard, screen readers and forms automatically. All it needs is one <input> inside a <label>:
<label class="switch-label">
<input type="checkbox" role="switch" class="switch" checked>
<span>Email notifications</span>
</label>
- Wrapping the input in a
<label>connects the two, so clicking the words flips the switch too, giving a bigger area to tap. role="switch"tells screen readers (software that reads pages aloud) to announce the control as a switch that's "on" or "off", rather than a checkbox that's "checked".checkedsets the switch to on when the page loads. Leave it out to start off.
What changed since the old version?
The original version of this tutorial used a lot more HTML: a hidden checkbox, plus a label containing two empty <div> elements for the sliding parts. That approach had some real problems:
| Old approach | The problem |
|---|---|
Checkbox hidden with display: none | Keyboard users couldn't reach it, and screen readers couldn't find it |
| A label with no text | Screen readers had nothing to announce, so nobody knew what the switch was for |
<div> elements inside a <label> | Not valid HTML: labels may only contain inline content |
The opening <div class="flipswitch"> written twice | An extra unclosed element that could break the layout around it |
The new version fixes all of these, with much less code.
Step 2: the track
The track is the rounded, pill-shaped background the knob slides along. We create it by styling the checkbox itself:
.switch {
--switch-on: #1d4ed8;
--switch-off: #6b7280;
appearance: none;
margin: 0;
position: relative;
flex-shrink: 0;
font-size: 1rem;
width: 3em;
height: 1.75em;
border-radius: 999px;
background: var(--switch-off);
cursor: pointer;
transition: background-color 0.25s ease;
}
| Line | What it does |
|---|---|
--switch-on, --switch-off | Custom properties (CSS variables) holding the two colours, so they're easy to change later |
appearance: none; | Removes the browser's built-in checkbox, leaving a plain box to style |
position: relative; | Makes the track the anchor point for the knob we'll add next |
font-size: 1rem; | Form controls don't inherit the page's font size by default. This sets a reliable base for our em sizes. |
width, height | The track's size, in em so the whole switch scales together |
border-radius: 999px; | Fully rounded ends, making the pill shape |
transition | Fades smoothly between the off and on colours |
Then the :checked pseudo-class, which matches while the checkbox is ticked, switches the colour:
.switch:checked {
background: var(--switch-on);
}
Step 3: the sliding knob
The knob is the white circle that slides from side to side. We draw it with the ::before pseudo-element, an extra box CSS adds inside the element. Checkboxes styled with appearance: none can show pseudo-elements in all modern browsers.
.switch::before {
content: "";
position: absolute;
top: 0.25em;
left: 0.25em;
width: 1.25em;
height: 1.25em;
border-radius: 50%;
background: #ffffff;
box-shadow: 0 1px 3px rgb(0 0 0 / 0.3);
transition: translate 0.25s ease;
}
.switch:checked::before {
translate: 1.25em 0;
}
The knob sits 0.25em in from the top and left edge. When the switch is on, translate slides it to the right. How far? The track is 3em wide, minus the knob's 1.25em width, minus 0.25em of space on each side, which leaves exactly 1.25em to travel.
What changed since the old version? The original animated margin-left and right, which makes the browser recalculate the page layout on every frame. translate only moves what's drawn, so the animation stays smooth, even on slower phones.
Step 4: adding ON and OFF text
The original switch showed the words ON and OFF inside the track. We can do that with the second pseudo-element, ::after. First, make the track a little wider and taller to fit the text, and adjust the knob to match:
.switch--text {
width: 4.5em;
height: 2em;
}
.switch--text::before {
width: 1.5em;
height: 1.5em;
}
.switch--text:checked::before {
translate: 2.5em 0; /* 4.5 - 1.5 - 0.25 - 0.25 */
}
Then add the text. It sits on the right when the switch is off, and moves to the left when it's on, always on the opposite side to the knob:
.switch--text::after {
content: "OFF";
position: absolute;
top: 50%;
right: 0.75em;
translate: 0 -50%;
font: 700 0.7em/1 system-ui, sans-serif;
color: #ffffff;
}
.switch--text:checked::after {
content: "ON";
right: auto;
left: 0.9em;
}
top: 50% with translate: 0 -50% centres the text vertically, whatever its size.
The ON and OFF text is just decoration. Screen readers get the on or off state from role="switch", and the label tells them what the switch controls.
Focus, disabled and other states
Because we removed the browser's styling, we need to add back a few states it normally handles.
Keyboard focus
Keyboard users press Tab to reach the switch and Space to flip it. A clear outline shows where they are:
.switch:focus-visible {
outline: 3px solid #f59e0b;
outline-offset: 3px;
}
Hover
A slight brightening on hover shows the switch is interactive:
.switch:hover:not(:disabled) {
filter: brightness(1.1);
}
Disabled
Add the disabled attribute when a setting can't be changed. These rules fade the switch and its label:
.switch:disabled {
opacity: 0.5;
cursor: not-allowed;
}
.switch-label:has(.switch:disabled) {
color: #9ca3af;
cursor: not-allowed;
}
High contrast mode
Some people use Windows high contrast mode, which replaces website colours with a small set of system colours. Background colours can disappear, leaving the switch invisible. The forced-colors media query lets you add borders and use system colour keywords instead:
@media (forced-colors: active) {
.switch { border: 1px solid CanvasText; }
.switch::before { background: CanvasText; }
.switch:checked { background: Highlight; }
}
Reduced motion
Turn off the sliding animation for visitors who've asked their device to reduce motion:
@media (prefers-reduced-motion: reduce) {
.switch,
.switch::before {
transition: none;
}
}
Changing colours and sizes
Colours
Because the colours are custom properties, you can change them for one switch, or for a whole section, without rewriting any rules:
.switch--green { --switch-on: #15803d; }
.switch--purple { --switch-on: #7c3aed; }
Remember to check contrast if you change colours: the white knob and text need to stand out from the track in both positions.
Sizes
Every measurement in the switch uses em, which is relative to the font size. So to resize the whole switch, knob, text and all, you only need to change one value:
.switch--small { font-size: 0.8rem; }
.switch--large { font-size: 1.4rem; }
Tip: keep switches big enough to tap comfortably on a phone. Because the label is clickable too, the whole row acts as the tap target, which helps a lot with smaller switches.
The complete code
Here's everything together, ready to copy. Use the switch--text class when you want the ON and OFF labels:
<label class="switch-label">
<input type="checkbox" role="switch" class="switch switch--text" checked>
<span>Email notifications</span>
</label>
.switch-label {
display: inline-flex;
align-items: center;
gap: 12px;
cursor: pointer;
}
.switch {
--switch-on: #1d4ed8;
--switch-off: #6b7280;
appearance: none;
margin: 0;
position: relative;
flex-shrink: 0;
font-size: 1rem;
width: 3em;
height: 1.75em;
border-radius: 999px;
background: var(--switch-off);
cursor: pointer;
transition: background-color 0.25s ease;
}
.switch::before {
content: "";
position: absolute;
top: 0.25em;
left: 0.25em;
width: 1.25em;
height: 1.25em;
border-radius: 50%;
background: #ffffff;
box-shadow: 0 1px 3px rgb(0 0 0 / 0.3);
transition: translate 0.25s ease;
}
.switch:checked { background: var(--switch-on); }
.switch:checked::before { translate: 1.25em 0; }
/* ON and OFF text */
.switch--text { width: 4.5em; height: 2em; }
.switch--text::before { width: 1.5em; height: 1.5em; }
.switch--text:checked::before { translate: 2.5em 0; }
.switch--text::after {
content: "OFF";
position: absolute;
top: 50%;
right: 0.75em;
translate: 0 -50%;
font: 700 0.7em/1 system-ui, sans-serif;
color: #ffffff;
}
.switch--text:checked::after { content: "ON"; right: auto; left: 0.9em; }
/* States */
.switch:hover:not(:disabled) { filter: brightness(1.1); }
.switch:focus-visible { outline: 3px solid #f59e0b; outline-offset: 3px; }
.switch:disabled { opacity: 0.5; cursor: not-allowed; }
.switch-label:has(.switch:disabled) { color: #9ca3af; cursor: not-allowed; }
@media (forced-colors: active) {
.switch { border: 1px solid CanvasText; }
.switch::before { background: CanvasText; }
.switch:checked { background: Highlight; }
}
@media (prefers-reduced-motion: reduce) {
.switch, .switch::before { transition: none; }
}
Want to design your own? Our CSS flip switch generator lets you choose colours, sizes and labels visually, then copy the finished code.
Wrapping up
A flip switch is really just a checkbox in disguise. Keep the real input, reset it with appearance: none, draw the knob with ::before and slide it with translate. Add a proper label and role="switch", and your switch will look great and work for everyone, whether they use a mouse, a keyboard or a screen reader.