AI CSS Animations

Morphing Underline Animation

The underline gathers into a dot, hops over the word and lands as its full stop, then drops and rolls back to stretch out again. One pseudo-element: clip-path trims it, transform moves it.

Also known as: underline to dot animation, bouncing dot underline css, playful underline loop.

Say hello

HTML

<div class="txt-morphing-underline">Say <span class="txt-morphing-underline-word">hello</span></div>

CSS

.txt-morphing-underline {
  --c: var(--txt-color, #3b82f6);
  font-size: var(--txt-size, 32px);
  font-family: system-ui, sans-serif;
  font-weight: 700;
  line-height: 1.2;
  padding: 0.6em 0.4em 0.2em 0;
  white-space: nowrap;}
.txt-morphing-underline-word {
  position: relative;
  display: inline-block;
  color: var(--c);
}
.txt-morphing-underline-word::after {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  bottom: -0.1em;
  height: 0.1em;
  background: var(--c);
  clip-path: inset(0 0 round 0.05em);
  transform-origin: 50% 100%;
  animation: txt-morphing-underline-hop 4s cubic-bezier(0.65, 0, 0.35, 1) infinite;
}
/*
 * calc(50% - 0.05em) trims the full-width bar to a 0.1em dot at its center.
 * The hop is five points on a parabola; translateX percentages are of the
 * bar, which is the word's width, so 50% + 0.1em lands just past the last letter.
 */
@keyframes txt-morphing-underline-hop {
  0%, 10%   { clip-path: inset(0 0 round 0.05em); transform: none; }
  22%       { clip-path: inset(0 calc(50% - 0.05em) round 0.05em); transform: none; }
  24%       { transform: scale(1.3, 0.7); animation-timing-function: linear; }
  28%       { transform: translate(calc(12.5% + 0.025em), -1.18em); animation-timing-function: linear; }
  32%       { transform: translate(calc(25% + 0.05em), -1.62em); animation-timing-function: linear; }
  36%       { transform: translate(calc(37.5% + 0.075em), -1.35em); animation-timing-function: linear; }
  40%       { transform: translate(calc(50% + 0.1em), -0.35em) scale(1.8, 1); animation-timing-function: ease-out; }
  46%, 72%  { transform: translate(calc(50% + 0.1em), -0.35em) scale(1.4); }
  78%       { transform: translate(calc(50% + 0.1em), 0); animation-timing-function: ease-in-out; }
  84%       { clip-path: inset(0 calc(50% - 0.05em) round 0.05em); transform: none; }
  94%, 100% { clip-path: inset(0 0 round 0.05em); transform: none; }
}

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