AI CSS Animations

Multiline Underline Link Hover

An inline link inside a paragraph whose underline draws line by line when the link wraps, finishing one row before starting the next. It is background-size on an inline element.

Also known as: animated underline multiple lines, wrapping link underline, background size underline.

HTML

<p class="hfx-link-multiline-underline">Before you ship, read our <a href="#">field guide to accessible motion and reduced motion defaults</a> for the details.</p>

CSS

.hfx-link-multiline-underline {
  --c: var(--hfx-color, #3b82f6);
  max-width: 17em;
  margin: 0;
  font: 400 calc(var(--hfx-size, 16px) * 1.1)/1.6 system-ui, sans-serif;
}
.hfx-link-multiline-underline a {
  color: var(--c);
  font-weight: 600;
  text-decoration: none;
  background:
    linear-gradient(var(--c) 0 0) 0 100% / 0 0.1em no-repeat,
    linear-gradient(color-mix(in srgb, var(--c) 28%, transparent) 0 0) 0 100% / 100% 0.1em no-repeat;
  transition: background-size 0.7s cubic-bezier(0.65, 0, 0.35, 1);
}
.hfx-link-multiline-underline a:is(:hover, :focus-visible) {
  background-size: 100% 0.1em, 100% 0.1em;
}

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

Tailwind CSS version

The same effect using Tailwind utility classes. No custom CSS or config needed.

<p class="max-w-xs text-lg leading-relaxed">
  Before you ship, read our
  <a href="#" class="bg-gradient-to-r from-blue-500 to-blue-500 bg-[length:0%_2px] bg-left-bottom bg-no-repeat font-semibold text-blue-500 transition-[background-size] duration-700 hover:bg-[length:100%_2px]">field guide to accessible motion and reduced motion defaults</a>
  for the details.
</p>

Customizing this snippet

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

Underline Grow Link
Underline from Center Link
Highlighter Link
Slide Up Text Link