AI CSS Animations

Toggle Switch with Label

A switch with its setting name beside it and a small status chip that rolls from Off to On and takes your accent. The chip is aria-hidden, since the switch role already announces the state.

Also known as: css toggle switch with label, toggle switch with status text, on off label beside switch.

HTML

<label class="tgl-with-label">
  <input type="checkbox" role="switch">
  <span class="tgl-with-label-track" aria-hidden="true"></span>
  Airplane mode
  <span class="tgl-with-label-chip" aria-hidden="true">
    <span class="tgl-with-label-roll"><span>Off</span><span>On</span></span>
  </span>
</label>

CSS

.tgl-with-label {
  --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.7em;
  font-family: system-ui, sans-serif;
  font-weight: 500;
  white-space: nowrap;
}
.tgl-with-label input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
  z-index: 1;
}
.tgl-with-label-track {
  position: relative;
  width: 3em;
  height: 1.65em;
  border-radius: 0.825em;
  background: color-mix(in srgb, currentColor 20%, transparent);
  transition: background-color 0.25s ease;
}
.tgl-with-label-track::after {
  content: "";
  position: absolute;
  top: 0.15em;
  left: 0.15em;
  width: 1.35em;
  height: 1.35em;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 0.08em 0.2em rgb(0 0 0 / 0.25);
  transition: transform 0.3s cubic-bezier(0.3, 0.7, 0.3, 1);
}
.tgl-with-label-chip {
  height: 1.8em;
  padding: 0 0.6em;
  border-radius: 0.9em;
  overflow: hidden;
  font-size: 0.72em;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  background: color-mix(in srgb, currentColor 10%, transparent);
  color: color-mix(in srgb, currentColor 65%, transparent);
  transition: background-color 0.3s ease, color 0.3s ease;
}
.tgl-with-label-roll {
  display: grid;
  grid-auto-rows: 1.8em;
  align-items: center;
  text-align: center;
  transition: transform 0.35s cubic-bezier(0.4, 1.3, 0.5, 1);
}
.tgl-with-label input:checked + .tgl-with-label-track { background: var(--c); }
.tgl-with-label input:checked + .tgl-with-label-track::after { transform: translateX(1.35em); }
.tgl-with-label input:checked ~ .tgl-with-label-chip {
  background: color-mix(in srgb, var(--c) 16%, transparent);
  color: var(--c);
}
.tgl-with-label input:checked ~ .tgl-with-label-chip .tgl-with-label-roll { transform: translateY(-1.8em); }
.tgl-with-label input:focus-visible + .tgl-with-label-track {
  outline: 2px solid var(--c);
  outline-offset: 2px;
}

@media (prefers-reduced-motion: reduce) {
  .tgl-with-label, .tgl-with-label *, .tgl-with-label::before, .tgl-with-label::after, .tgl-with-label *::before, .tgl-with-label *::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="group 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">Airplane mode</span>
  <span class="h-5 overflow-hidden rounded-full bg-gray-100 px-2 text-xs font-bold uppercase text-gray-500 transition-colors peer-checked:bg-blue-100 peer-checked:text-blue-600" aria-hidden="true">
    <span class="flex flex-col transition-transform duration-300 group-has-[:checked]:-translate-y-5">
      <span class="h-5 leading-5">Off</span>
      <span class="h-5 leading-5">On</span>
    </span>
  </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