AI CSS Animations

Gradient Toggle Switch

A switch whose track fills with a slowly drifting gradient mixed from your accent, with a soft colored glow underneath. The gradient is its own layer, so it fades in instead of snapping.

Also known as: gradient switch css, colorful toggle switch, animated gradient toggle.

HTML

<label class="tgl-gradient">
  <input type="checkbox" role="switch" aria-label="Boost mode" checked>
  <span class="tgl-gradient-track" aria-hidden="true"></span>
</label>

CSS

.tgl-gradient {
  --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-gradient input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
  z-index: 1;
}
.tgl-gradient-track {
  position: relative;
  display: block;
  width: 3.8em;
  height: 2em;
  border-radius: 1em;
  background: color-mix(in srgb, currentColor 13%, transparent);
}
.tgl-gradient-track::before {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background: linear-gradient(90deg,
    var(--c),
    color-mix(in oklch, var(--c) 45%, #d946ef),
    color-mix(in oklch, var(--c) 30%, #fb7185),
    var(--c)) 0 0 / 300% 100%;
  box-shadow: 0 0.35em 0.9em -0.25em color-mix(in srgb, var(--c) 80%, transparent);
  opacity: 0;
  transition: opacity 0.35s ease;
}
.tgl-gradient-track::after {
  content: "";
  position: absolute;
  top: 0.2em;
  left: 0.2em;
  width: 1.6em;
  height: 1.6em;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 0.1em 0.3em rgb(0 0 0 / 0.25);
  transition: transform 0.4s cubic-bezier(0.3, 0.7, 0.3, 1);
}
.tgl-gradient input:checked + .tgl-gradient-track::before {
  opacity: 1;
  animation: tgl-gradient-drift 3s ease-in-out infinite alternate;
}
.tgl-gradient input:checked + .tgl-gradient-track::after { transform: translateX(1.8em); }
.tgl-gradient input:focus-visible + .tgl-gradient-track {
  outline: 2px solid var(--c);
  outline-offset: 2px;
}
@keyframes tgl-gradient-drift {
  to { background-position: 100% 0; }
}

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