# Toggle Switch

> An accessible mobile-style toggle switch built on a real checkbox.

- Category: [CSS Form Elements](https://copy-css.com/category/forms)
- Tags: switch, toggle, checkbox, mobile
- Live preview: https://copy-css.com/snippets/toggle-switch
- License: Free for personal and commercial use. No signup, no attribution required.

## HTML

```html
<label class="switch">
  <input type="checkbox" checked aria-label="Notifications">
  <span class="slider"></span>
</label>
```

## CSS

```css
.switch {
  position: relative;
  display: inline-block;
  width: 52px;
  height: 30px;
}

.switch input {
  width: 0;
  height: 0;
  opacity: 0;
}

.switch .slider {
  position: absolute;
  inset: 0;
  border-radius: 999px;
  background: #d1d5db;
  cursor: pointer;
  transition: background 0.25s ease;
}

.switch .slider::before {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25);
  transition: transform 0.25s ease;
}

.switch input:checked + .slider {
  background: #22c55e;
}

.switch input:checked + .slider::before {
  transform: translateX(22px);
}

.switch input:focus-visible + .slider {
  outline: 2px solid #22c55e;
  outline-offset: 2px;
}
```

## Notes

- This CSS Toggle Switch is 47 lines of pure CSS (0.8 KB) with no JavaScript and no dependencies. It uses transitions, transforms, box shadows and the :checked state (no JavaScript).
- In JSX use `className` instead of `class`. In Tailwind projects put the CSS in the global stylesheet.
