AI CSS Animations

Simple Toggle Switch

The everyday switch: a rounded track, a white knob drawn with ::after and a text label beside it. Short CSS that is easy to restyle, plus a matching Tailwind version built on peer-checked.

Also known as: simple toggle switch css, tailwind toggle switch, basic css switch.

HTML

<label class="tgl-simple">
  <input type="checkbox" role="switch">
  <span class="tgl-simple-track" aria-hidden="true"></span>
  Notifications
</label>

CSS

.tgl-simple {
  --c: var(--tgl-color, #3b82f6);
  font-size: var(--tgl-size, 24px);
  position: relative;
  display: inline-flex;
  vertical-align: middle;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  align-items: center;
  gap: 0.75em;
  font-family: system-ui, sans-serif;
  font-weight: 500;
  white-space: nowrap;
}
.tgl-simple input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
  z-index: 1;
}
.tgl-simple-track {
  position: relative;
  width: 2.75em;
  height: 1.5em;
  border-radius: 0.75em;
  background: color-mix(in srgb, currentColor 22%, transparent);
  transition: background-color 0.2s ease;
}
.tgl-simple-track::after {
  content: "";
  position: absolute;
  top: 0.125em;
  left: 0.125em;
  width: 1.25em;
  height: 1.25em;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 0.05em 0.15em rgb(0 0 0 / 0.3);
  transition: transform 0.2s ease;
}
.tgl-simple input:checked + .tgl-simple-track { background: var(--c); }
.tgl-simple input:checked + .tgl-simple-track::after { transform: translateX(1.25em); }
.tgl-simple input:focus-visible + .tgl-simple-track {
  outline: 2px solid var(--c);
  outline-offset: 2px;
}

@media (prefers-reduced-motion: reduce) {
  .tgl-simple, .tgl-simple *, .tgl-simple::before, .tgl-simple::after, .tgl-simple *::before, .tgl-simple *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

Tailwind CSS version

The same effect using Tailwind utility classes. No custom CSS or config needed.

<label class="relative inline-flex cursor-pointer items-center gap-3">
  <input type="checkbox" role="switch" class="peer sr-only">
  <span class="relative h-6 w-11 rounded-full bg-gray-300 transition-colors peer-checked:bg-blue-500 peer-focus-visible:ring-2 peer-focus-visible:ring-blue-500 peer-focus-visible:ring-offset-2 after:absolute after:left-0.5 after:top-0.5 after:h-5 after:w-5 after:rounded-full after:bg-white after:shadow after:transition-transform after:content-[''] peer-checked:after:translate-x-5" aria-hidden="true"></span>
  <span class="font-medium">Notifications</span>
</label>

Customizing this snippet

  • Color: set --tgl-color on the element or a parent. Currently previewing #3b82f6.
  • Size: set --tgl-size. Every other dimension is in em, so the whole thing scales together. Currently previewing 24px.
  • Speed: change the duration on the transition or animation shorthand. Staggered delays are relative, so they keep their rhythm.
  • Reduced motion: the rule at the end of the CSS turns the motion off for people who asked their OS for less of it. Keep it.

Similar toggle switches