AI CSS Animations

Accordion Text Animation

The word squeezes in like an accordion: letters slide toward the middle and fold into alternating pleats, one shade lighter on every other fold, then stretch back out flat.

Also known as: folding text animation, concertina text, squeeze text css.

HTML

<div class="txt-accordion-text" aria-label="Accordion"><span aria-hidden="true"><span style="--i: 0; --o: -4">A</span><span style="--i: 1; --o: -3">c</span><span style="--i: 2; --o: -2">c</span><span style="--i: 3; --o: -1">o</span><span style="--i: 4; --o: 0">r</span><span style="--i: 5; --o: 1">d</span><span style="--i: 6; --o: 2">i</span><span style="--i: 7; --o: 3">o</span><span style="--i: 8; --o: 4">n</span></span></div>

CSS

.txt-accordion-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);}
/* one shared vanishing point, so the folds read as pleats */
.txt-accordion-text > span {
  display: inline-block;
  perspective: 5em;
}
.txt-accordion-text span span {
  --f: 55deg;
  --k: var(--c);
  display: inline-block;
  animation: txt-accordion-text-squeeze 3s cubic-bezier(0.65, 0, 0.35, 1) infinite;
}
.txt-accordion-text span span:nth-child(even) {
  --f: -55deg;
  --k: color-mix(in srgb, var(--c) 55%, transparent);
}
@keyframes txt-accordion-text-squeeze {
  0%, 12%, 100% { transform: translateX(0) rotateY(0deg); color: var(--c); }
  /* --o is the letter's distance from the middle, so outer letters travel furthest */
  45%, 62% { transform: translateX(calc(var(--o) * -0.22em)) rotateY(var(--f)); color: var(--k); }
}

@media (prefers-reduced-motion: reduce) {
  .txt-accordion-text, .txt-accordion-text *, .txt-accordion-text::before, .txt-accordion-text::after, .txt-accordion-text *::before, .txt-accordion-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