AI CSS Animations

Swinging Text Animation

Letters hang on strings from a rail and swing. Each one has a slightly longer period than the last, like the pendulum wave toy, so the row keeps drifting in and out of patterns.

Also known as: hanging letters, pendulum text animation, swinging sign letters.

HTML

<div class="txt-swinging-text" aria-label="Welcome"><span aria-hidden="true"><span style="--i: 0">W</span><span style="--i: 1">E</span><span style="--i: 2">L</span><span style="--i: 3">C</span><span style="--i: 4">O</span><span style="--i: 5">M</span><span style="--i: 6">E</span></span></div>

CSS

.txt-swinging-text {
  --c: var(--txt-color, #3b82f6);
  font-size: var(--txt-size, 32px);
  font-family: system-ui, sans-serif;
  font-weight: 700;
  line-height: 1.2;
  color: var(--c);}
.txt-swinging-text > span {
  display: inline-block;
  padding: 0.55em 0.2em 0;
  border-top: 0.06em solid color-mix(in srgb, var(--c) 55%, transparent);
  border-radius: 0.03em;
}
.txt-swinging-text span span {
  position: relative;
  display: inline-block;
  margin: 0 0.05em;
  /* the pivot is on the rail, 0.55em above the letter */
  transform-origin: 50% -0.55em;
  animation: txt-swinging-text-swing calc(1.3s + var(--i) * 0.07s) ease-in-out infinite alternate;
  animation-delay: calc(var(--i) * -0.2s);
}
.txt-swinging-text span span::before {
  content: "";
  position: absolute;
  top: -0.55em;
  left: 50%;
  width: 1px;
  height: 0.85em;
  background: color-mix(in srgb, var(--c) 55%, transparent);
}
@keyframes txt-swinging-text-swing {
  from { transform: rotate(10deg); }
  to   { transform: rotate(-10deg); }
}

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

Customizing this snippet

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