AI CSS Animations

Swing

Hangs from its top edge and swings like a sign on a nail, each arc smaller than the last until it hangs still. The transform-origin sits at the top center, so the bottom does the moving.

Also known as: swing animation css, swinging sign, hanging swing.

Swing

HTML

<div class="kfa-swing">Swing</div>

CSS

.kfa-swing {
  --c: var(--kfa-color, #3b82f6);
  font-size: var(--kfa-size, 16px);
  display: grid;
  place-items: center;
  width: 5.5em;
  height: 5.5em;
  border-radius: 1em;
  background: var(--c);
  color: #fff;
  font-family: system-ui, sans-serif;
  font-weight: 700;
  box-shadow: 0 0.6em 1.4em -0.6em color-mix(in srgb, var(--c) 70%, transparent);
  transform-origin: 50% 0;
  animation: kfa-swing-sway 1.2s ease-in-out both;
}
@keyframes kfa-swing-sway {
  0%, 100% { transform: rotate(0); }
  16% { transform: rotate(15deg); }
  34% { transform: rotate(-10deg); }
  52% { transform: rotate(6deg); }
  68% { transform: rotate(-3.5deg); }
  82% { transform: rotate(1.5deg); }
}

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

Customizing this snippet

  • Color: set --kfa-color on the element or a parent. Currently previewing #3b82f6.
  • Base size: set --kfa-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 keyframe animations