AI CSS Animations

Rotating Words with Animated Underline

Each word rises into the headline, an underline draws in beneath it, and before the swap the line retracts to the right. The underline is the word's own ::after, so it always fits the word.

Also known as: underline word swap, animated underline headline, rotating text underline css.

Hire top designers engineers writers

HTML

<div class="txt-underline-rotating-words">Hire top
  <span class="txt-underline-rotating-words-words">
    <span style="--i: 0">designers</span>
    <span style="--i: 1">engineers</span>
    <span style="--i: 2">writers</span>
  </span>
</div>

CSS

.txt-underline-rotating-words {
  --c: var(--txt-color, #3b82f6);
  font-size: var(--txt-size, 32px);
  font-family: system-ui, sans-serif;
  font-weight: 700;
  line-height: 1.2;
  display: flex;
  align-items: baseline;
  gap: 0.28em;
  white-space: nowrap;}
.txt-underline-rotating-words-words {
  display: inline-grid;
  color: var(--c);
}
.txt-underline-rotating-words-words span {
  grid-area: 1 / 1;
  justify-self: start;
  position: relative;
  /* 3 words x 3s = 9s */
  animation: txt-underline-rotating-words-word 9s cubic-bezier(0.2, 0.7, 0.2, 1) infinite;
  animation-delay: calc(var(--i) * 3s - 10s);
}
.txt-underline-rotating-words-words span::after {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0.02em;
  height: 0.09em;
  border-radius: 0.05em;
  background: currentColor;
  animation: txt-underline-rotating-words-line 9s cubic-bezier(0.65, 0, 0.35, 1) infinite;
  animation-delay: inherit;
}
.txt-underline-rotating-words-words span:not(:first-child) {
  opacity: 0;
}
@keyframes txt-underline-rotating-words-word {
  0%      { opacity: 0; transform: translateY(0.35em); }
  4.44%   { opacity: 1; transform: translateY(0); }
  29.44% {
    opacity: 1; transform: translateY(0);
    animation-timing-function: cubic-bezier(0.5, 0, 0.75, 0);
  }
  33.33%  { opacity: 0; transform: translateY(-0.35em); }
  100%    { opacity: 0; transform: translateY(0.35em); }
}
@keyframes txt-underline-rotating-words-line {
  0%, 3.33% { transform: scaleX(0); transform-origin: left; }
  9.44%    { transform: scaleX(1); transform-origin: left; }
  25%       { transform: scaleX(1); transform-origin: right; }
  28.89%, 100% { transform: scaleX(0); transform-origin: right; }
}

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