AI CSS Animations

Check and Cross Toggle Switch

A knob that says what it means: a gray cross spins away when you switch it on, and a check mark draws itself in your accent. The check is an SVG path animated with stroke-dashoffset.

Also known as: toggle switch with check icon, tick cross toggle css, check x switch css.

HTML

<label class="tgl-check-cross">
  <input type="checkbox" role="switch" aria-label="Email alerts">
  <span class="tgl-check-cross-track" aria-hidden="true">
    <span class="tgl-check-cross-knob">
      <svg viewBox="0 0 24 24" fill="none" stroke-width="2.8" stroke-linecap="round" stroke-linejoin="round">
        <path class="tgl-check-cross-x" d="M8 8l8 8M16 8l-8 8"/>
        <path class="tgl-check-cross-tick" d="M6.5 12.5l3.5 3.5 7.5-8"/>
      </svg>
    </span>
  </span>
</label>

CSS

.tgl-check-cross {
  --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-check-cross input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
  z-index: 1;
}
.tgl-check-cross-track {
  position: relative;
  display: block;
  width: 3.5em;
  height: 2em;
  border-radius: 1em;
  background: color-mix(in srgb, currentColor 13%, transparent);
  transition: background-color 0.3s ease;
}
.tgl-check-cross-knob {
  position: absolute;
  top: 0.2em;
  left: 0.2em;
  display: grid;
  place-items: center;
  width: 1.6em;
  height: 1.6em;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 0.1em 0.3em rgb(0 0 0 / 0.22);
  transition: transform 0.4s cubic-bezier(0.4, 1.3, 0.5, 1);
}
.tgl-check-cross-knob svg {
  width: 1.1em;
  height: 1.1em;
  overflow: visible;
}
.tgl-check-cross-x {
  stroke: #9ca3af;
  transform-box: fill-box;
  transform-origin: center;
  transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1), opacity 0.2s ease;
}
.tgl-check-cross-tick {
  stroke: var(--c);
  stroke-dasharray: 17;
  stroke-dashoffset: 17;
  transition: stroke-dashoffset 0.15s ease;
}
.tgl-check-cross input:checked + .tgl-check-cross-track { background: var(--c); }
.tgl-check-cross input:checked + .tgl-check-cross-track .tgl-check-cross-knob { transform: translateX(1.5em); }
.tgl-check-cross input:checked + .tgl-check-cross-track .tgl-check-cross-x {
  opacity: 0;
  transform: rotate(90deg) scale(0);
}
.tgl-check-cross input:checked + .tgl-check-cross-track .tgl-check-cross-tick {
  stroke-dashoffset: 0;
  transition: stroke-dashoffset 0.35s cubic-bezier(0.65, 0, 0.35, 1) 0.15s;
}
.tgl-check-cross input:focus-visible + .tgl-check-cross-track {
  outline: 2px solid var(--c);
  outline-offset: 2px;
}

@media (prefers-reduced-motion: reduce) {
  .tgl-check-cross, .tgl-check-cross *, .tgl-check-cross::before, .tgl-check-cross::after, .tgl-check-cross *::before, .tgl-check-cross *::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