AI CSS Animations

Lock Unlock Toggle

A padlock knob: unlocked, the shackle is lifted and tilted open; lock it and the knob slides over, then the shackle drops shut with a springy click. The shackle turns from its left leg.

Also known as: padlock toggle css, lock switch animation, locked unlocked switch.

HTML

<label class="tgl-lock-toggle">
  <input type="checkbox" role="switch" aria-label="Lock">
  <span class="tgl-lock-toggle-track" aria-hidden="true">
    <span class="tgl-lock-toggle-knob">
      <svg viewBox="0 0 24 24">
        <path class="tgl-lock-toggle-shackle" d="M8 11V7.5a4 4 0 0 1 8 0V11"/>
        <rect class="tgl-lock-toggle-body" x="5" y="10.5" width="14" height="10" rx="2.2"/>
        <path class="tgl-lock-toggle-hole" d="M12 14.5v2.5"/>
      </svg>
    </span>
  </span>
</label>

CSS

.tgl-lock-toggle {
  --c: var(--tgl-color, #3b82f6);
  font-size: var(--tgl-size, 24px);
  position: relative;
  display: inline-flex;
  align-items: center;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.tgl-lock-toggle input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
  z-index: 1;
}
.tgl-lock-toggle-track {
  position: relative;
  display: block;
  width: 5.6em;
  height: 2.6em;
  border-radius: 1.3em;
  background: color-mix(in srgb, currentColor 14%, transparent);
  transition: background-color 0.35s ease;
}
.tgl-lock-toggle-knob {
  position: absolute;
  top: 0.3em;
  left: 0.3em;
  display: grid;
  place-items: center;
  width: 2em;
  height: 2em;
  border-radius: 50%;
  background: #fff;
  color: #9ca3af;
  box-shadow: 0 0.1em 0.25em rgba(0, 0, 0, 0.25);
  transition: transform 0.45s cubic-bezier(0.5, 1.3, 0.4, 1), color 0.3s ease;
}
.tgl-lock-toggle-knob svg {
  display: block;
  width: 1.2em;
  height: 1.2em;
  overflow: visible;
}
.tgl-lock-toggle-shackle {
  fill: none;
  stroke: currentColor;
  stroke-width: 2.4;
  stroke-linecap: round;
  transform-box: fill-box;
  transform-origin: 0% 100%;
  transform: translateY(-10%) rotate(-24deg);
  transition: transform 0.3s ease;
}
.tgl-lock-toggle-body { fill: currentColor; }
.tgl-lock-toggle-hole {
  stroke: #fff;
  stroke-width: 2.2;
  stroke-linecap: round;
}
.tgl-lock-toggle input:checked + .tgl-lock-toggle-track { background: var(--c); }
.tgl-lock-toggle input:checked + .tgl-lock-toggle-track .tgl-lock-toggle-knob {
  transform: translateX(3em);
  color: var(--c);
}
/* the shackle snaps shut once the knob has arrived */
.tgl-lock-toggle input:checked + .tgl-lock-toggle-track .tgl-lock-toggle-shackle {
  transform: none;
  transition: transform 0.4s cubic-bezier(0.3, 1.8, 0.5, 1) 0.25s;
}
.tgl-lock-toggle input:focus-visible + .tgl-lock-toggle-track {
  outline: 2px solid var(--c);
  outline-offset: 0.2em;
}

@media (prefers-reduced-motion: reduce) {
  .tgl-lock-toggle, .tgl-lock-toggle *, .tgl-lock-toggle::before, .tgl-lock-toggle::after, .tgl-lock-toggle *::before, .tgl-lock-toggle *::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">
  <input type="checkbox" role="switch" aria-label="Lock" class="peer sr-only">
  <span aria-hidden="true" class="relative block h-[2.6rem] w-[5.6rem] rounded-full bg-gray-500/15 transition-colors duration-300 peer-checked:bg-blue-500 peer-focus-visible:outline peer-focus-visible:outline-2 peer-focus-visible:outline-offset-2 peer-focus-visible:outline-blue-500">
    <span class="absolute left-[0.3rem] top-[0.3rem] grid h-8 w-8 place-items-center rounded-full bg-white text-gray-400 shadow transition duration-[450ms] ease-[cubic-bezier(.5,1.5,.4,1)] group-has-[:checked]:translate-x-12 group-has-[:checked]:text-blue-500">
      <svg viewBox="0 0 24 24" class="h-5 w-5 overflow-visible">
        <path class="origin-bottom-left -translate-y-[10%] -rotate-[24deg] fill-none stroke-current stroke-[2.4] transition-transform duration-300 ease-[cubic-bezier(.3,1.8,.5,1)] [stroke-linecap:round] [transform-box:fill-box] group-has-[:checked]:translate-y-0 group-has-[:checked]:rotate-0 group-has-[:checked]:delay-200" d="M8 11V7.5a4 4 0 0 1 8 0V11"/>
        <rect class="fill-current" x="5" y="10.5" width="14" height="10" rx="2.2"/>
        <path class="stroke-white stroke-[2.2] [stroke-linecap:round]" d="M12 14.5v2.5"/>
      </svg>
    </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