AI CSS Animations

Swinging Sign Button

A button that hangs from a nail on two strings like a shop sign. On hover it swings, loses energy in smaller arcs, then keeps a lazy sway. The pivot is transform-origin set above the button.

Also known as: hanging sign hover, pendulum swing animation, swing button css.

HTML

<button class="btn-swinging-sign">We're open</button>

CSS

.btn-swinging-sign {
  --c: var(--btn-color, #3b82f6);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5em;
  font: 600 var(--btn-size, 18px)/1.2 system-ui, sans-serif;
  cursor: pointer;
  position: relative;
  margin-top: 1.8em;
  padding: 0.75em 1.7em;
  border: 2px solid color-mix(in srgb, var(--c) 70%, #000);
  border-radius: 0.5em;
  background: var(--c);
  color: #fff;
  transform-origin: 50% -1.5em;
  transition: transform 0.3s ease;}
.btn-swinging-sign::before {
  content: "";
  position: absolute;
  bottom: 100%;
  left: 22%;
  right: 22%;
  height: 1.5em;
  background:
    linear-gradient(to bottom right, transparent calc(50% - 1px), color-mix(in srgb, var(--c) 70%, transparent) 0 calc(50% + 1px), transparent 0) left / 50% 100% no-repeat,
    linear-gradient(to bottom left, transparent calc(50% - 1px), color-mix(in srgb, var(--c) 70%, transparent) 0 calc(50% + 1px), transparent 0) right / 50% 100% no-repeat;
}
.btn-swinging-sign::after {
  content: "";
  position: absolute;
  bottom: calc(100% + 1.3em);
  left: 50%;
  width: 0.45em;
  height: 0.45em;
  margin-left: -0.225em;
  border-radius: 50%;
  background: color-mix(in srgb, var(--c) 60%, #000);
}
.btn-swinging-sign:is(:hover, :focus-visible) {
  animation:
    btn-swinging-sign-swing 1.4s ease-in-out both,
    btn-swinging-sign-sway 2.4s ease-in-out 1.4s infinite;
}
@keyframes btn-swinging-sign-swing {
  0%   { transform: rotate(0deg); }
  15%  { transform: rotate(11deg); }
  35%  { transform: rotate(-8deg); }
  55%  { transform: rotate(5deg); }
  75%  { transform: rotate(-3deg); }
  100% { transform: rotate(1.5deg); }
}
@keyframes btn-swinging-sign-sway {
  0%, 100% { transform: rotate(1.5deg); }
  50%      { transform: rotate(-1.5deg); }
}

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

Customizing this snippet

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