# Material Toggle Switch

> An Android-style switch made from a single input, with a slim track, an oversized thumb and a focus halo.

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

## HTML

```html
<label class="tgl-md">
  <input type="checkbox" role="switch" checked>
  Dark theme
</label>
```

## CSS

```css
.tgl-md {
  display: inline-flex;
  align-items: center;
  gap: 14px;
  color: #3b0764;
  font: 500 15px system-ui, sans-serif;
  cursor: pointer;
}

.tgl-md input {
  position: relative;
  width: 36px;
  height: 14px;
  margin: 0 4px;
  border-radius: 999px;
  background: #d4d4d8;
  -webkit-appearance: none;
  appearance: none;
  cursor: pointer;
  transition: background 0.2s ease;
}

.tgl-md input::before {
  content: "";
  position: absolute;
  top: 50%;
  left: -4px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: #fafafa;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
  transform: translateY(-50%);
  transition: transform 0.2s ease, background 0.2s ease, box-shadow 0.2s ease;
}

.tgl-md input:checked {
  background: #d8b4fe;
}

.tgl-md input:checked::before {
  background: #7e22ce;
  transform: translate(22px, -50%);
}

.tgl-md input:focus-visible {
  outline: none;
}

.tgl-md input:focus-visible::before {
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.4), 0 0 0 10px rgba(126, 34, 206, 0.18);
}

@media (prefers-reduced-motion: reduce) {
  .tgl-md input,
  .tgl-md input::before {
    transition: none;
  }
}
```

## Notes

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