AI CSS Animations

Material 3 Switch

The Material You switch: an outlined track with a small handle that grows, turns white and gains a check mark when on. The handle swells while pressed and shows a state layer on hover.

Also known as: material design switch css, material you toggle, android 12 switch css.

HTML

<label class="tgl-material-3">
  <input type="checkbox" role="switch" aria-label="Wi-Fi" checked>
  <span class="tgl-material-3-track" aria-hidden="true">
    <span class="tgl-material-3-handle">
      <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"><path d="M5 12.5l4.5 4.5L19 7.5"/></svg>
    </span>
  </span>
</label>

CSS

.tgl-material-3 {
  --c: var(--tgl-color, #3b82f6);
  font-size: var(--tgl-size, 24px);
  position: relative;
  display: inline-block;
  vertical-align: middle;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;

}
.tgl-material-3 input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
  z-index: 1;
}
.tgl-material-3-track {
  position: relative;
  display: block;
  width: 3.25em;
  height: 2em;
  border: 0.125em solid color-mix(in srgb, currentColor 45%, transparent);
  border-radius: 1em;
  background: color-mix(in srgb, currentColor 8%, transparent);
  transition: background-color 0.25s ease, border-color 0.25s ease;
}
/* a 40dp box centered on the handle: it carries the hover state layer */
.tgl-material-3-handle {
  position: absolute;
  top: -0.375em;
  left: -0.375em;
  display: grid;
  place-items: center;
  width: 2.5em;
  height: 2.5em;
  border-radius: 50%;
  transition: transform 0.3s cubic-bezier(0.2, 0, 0, 1), background-color 0.2s ease;
}
/* the handle itself: 16dp off, 24dp on, 28dp pressed, all one scale */
.tgl-material-3-handle::before {
  content: "";
  position: absolute;
  inset: 0.375em;
  border-radius: 50%;
  background: color-mix(in srgb, currentColor 55%, transparent);
  transform: scale(0.571);
  transition: transform 0.3s cubic-bezier(0.2, 0, 0, 1), background-color 0.25s ease;
}
.tgl-material-3-handle svg {
  position: relative;
  width: 1em;
  height: 1em;
  color: var(--c);
  opacity: 0;
  transform: scale(0.3) rotate(-60deg);
  transition: opacity 0.2s ease, transform 0.3s cubic-bezier(0.2, 0, 0, 1);
}
.tgl-material-3 input:checked + .tgl-material-3-track {
  border-color: var(--c);
  background: var(--c);
}
.tgl-material-3 input:checked + .tgl-material-3-track .tgl-material-3-handle { transform: translateX(1.25em); }
.tgl-material-3 input:checked + .tgl-material-3-track .tgl-material-3-handle::before {
  background: #fff;
  transform: scale(0.857);
}
.tgl-material-3 input:checked + .tgl-material-3-track svg {
  opacity: 1;
  transform: none;
}
.tgl-material-3:hover .tgl-material-3-handle { background: color-mix(in srgb, currentColor 8%, transparent); }
.tgl-material-3:hover input:checked + .tgl-material-3-track .tgl-material-3-handle { background: color-mix(in srgb, var(--c) 14%, transparent); }
.tgl-material-3:active input + .tgl-material-3-track .tgl-material-3-handle::before { transform: scale(1); }
.tgl-material-3 input:focus-visible + .tgl-material-3-track {
  outline: 2px solid var(--c);
  outline-offset: 2px;
}

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

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