AI CSS Animations

Padlock Unlock Icon Hover

The shackle lifts out of the padlock body, then swings open around one leg, faked in 2D by flipping it through scaleX. Separate translate and scale transitions run in sequence.

Also known as: lock icon animation css, unlock icon hover, padlock open animation.

HTML

<button class="hfx-lock-icon-unlock" aria-label="Unlock"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path class="hfx-lock-icon-unlock-shackle" d="M8 11V7a4 4 0 0 1 8 0v4"/><rect x="4" y="11" width="16" height="10.5" rx="2"/><path d="M12 15.2v2.3"/></svg></button>

CSS

.hfx-lock-icon-unlock {
  --c: var(--hfx-color, #3b82f6);
  font-size: var(--hfx-size, 16px);
  position: relative;
  display: grid;
  place-items: center;
  width: 3.5em;
  height: 3.5em;
  padding: 0;
  border: 0;
  border-radius: 50%;
  cursor: pointer;
  border-radius: 0.85em;
  background: color-mix(in srgb, var(--c) 12%, transparent);
  color: var(--c);
  transition: background-color 0.25s ease, color 0.25s ease;}
.hfx-lock-icon-unlock svg {
  width: 1.6em;
  height: 1.6em;
  overflow: visible;
}
.hfx-lock-icon-unlock-shackle {
  transform-box: view-box;
  transform-origin: 33.33% 50%;
  /* leaving: swing back first, then drop in */
  transition: scale 0.25s ease, translate 0.25s cubic-bezier(0.34, 1.56, 0.64, 1) 0.2s;
}
.hfx-lock-icon-unlock:is(:hover, :focus-visible) {
  background: var(--c);
  color: #fff;
}
.hfx-lock-icon-unlock:is(:hover, :focus-visible) .hfx-lock-icon-unlock-shackle {
  translate: 0 -12.5%;
  scale: -1 1;
  /* entering: lift first, then swing */
  transition: translate 0.25s cubic-bezier(0.34, 1.56, 0.64, 1), scale 0.35s cubic-bezier(0.65, 0, 0.35, 1) 0.2s;
}

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

Customizing this snippet

  • Color: set --hfx-color on the element or a parent. Currently previewing #3b82f6.
  • Base size: set --hfx-size. Every other dimension is in em, so the whole thing scales together. Currently previewing 16px.
  • 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 hover effects