AI CSS Animations

Neumorphic Toggle Switch

A soft UI switch pressed into its own surface: an inset track, a raised knob that casts light and shadow, and a small LED in the knob that glows your accent when on.

Also known as: neumorphism toggle switch, soft ui switch, inset shadow toggle.

HTML

<label class="tgl-neumorphic">
  <input type="checkbox" role="switch" aria-label="Sound">
  <span class="tgl-neumorphic-track" aria-hidden="true">
    <span class="tgl-neumorphic-knob"></span>
  </span>
</label>

CSS

.tgl-neumorphic {
  --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;
  --surface: color-mix(in srgb, var(--c) 5%, #e4e8ef);
  --low: color-mix(in srgb, var(--c) 12%, #a3adbf);
  padding: 1.3em 1.7em;
  border-radius: 1.4em;
  background: var(--surface);
}
.tgl-neumorphic input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
  z-index: 1;
}
.tgl-neumorphic-track {
  position: relative;
  display: block;
  width: 4.4em;
  height: 2.2em;
  border-radius: 1.1em;
  background: var(--surface);
  box-shadow:
    inset 0.2em 0.2em 0.45em var(--low),
    inset -0.2em -0.2em 0.45em #fff;
}
.tgl-neumorphic-track::before {
  content: "";
  position: absolute;
  inset: 0.3em;
  border-radius: inherit;
  background: linear-gradient(90deg, color-mix(in srgb, var(--c) 30%, transparent), color-mix(in srgb, var(--c) 0%, transparent));
  opacity: 0;
  transition: opacity 0.4s ease;
}
.tgl-neumorphic-knob {
  position: absolute;
  top: 0.3em;
  left: 0.3em;
  display: grid;
  place-items: center;
  width: 1.6em;
  height: 1.6em;
  border-radius: 50%;
  background: linear-gradient(145deg, color-mix(in srgb, var(--surface), #fff 65%), var(--surface));
  box-shadow:
    0.18em 0.18em 0.4em var(--low),
    -0.12em -0.12em 0.3em #fff;
  transition: transform 0.45s cubic-bezier(0.4, 1.25, 0.5, 1);
}
.tgl-neumorphic-knob::before {
  content: "";
  width: 0.4em;
  height: 0.4em;
  border-radius: 50%;
  background: color-mix(in srgb, var(--low) 70%, #fff);
  box-shadow: inset 0.04em 0.05em 0.08em var(--low);
  transition: background-color 0.3s ease, box-shadow 0.3s ease;
}
.tgl-neumorphic input:checked + .tgl-neumorphic-track::before { opacity: 1; }
.tgl-neumorphic input:checked + .tgl-neumorphic-track .tgl-neumorphic-knob { transform: translateX(2.2em); }
.tgl-neumorphic input:checked + .tgl-neumorphic-track .tgl-neumorphic-knob::before {
  background: var(--c);
  box-shadow: 0 0 0.12em var(--c), 0 0 0.45em var(--c);
}
.tgl-neumorphic input:focus-visible + .tgl-neumorphic-track {
  outline: 2px solid var(--c);
  outline-offset: 0.2em;
}

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