AI CSS Animations

Overline and Underline Link Hover

Two faint rules frame the link at rest. On hover an accent line runs across the top from the left while the bottom one runs in from the right, a beat behind, so they cross.

Also known as: top and bottom line hover, border top bottom animation, overline hover effect.

HTML

<a class="hfx-link-overline-underline" href="#">Our principles</a>

CSS

.hfx-link-overline-underline {
  --c: var(--hfx-color, #3b82f6);
  position: relative;
  display: inline-block;
  color: var(--c);
  font: 600 calc(var(--hfx-size, 16px) * 1.25)/1.3 system-ui, sans-serif;
  text-decoration: none;
  padding: 0.3em 0.1em;
  background:
    linear-gradient(color-mix(in srgb, var(--c) 22%, transparent) 0 0) top / 100% 1px no-repeat,
    linear-gradient(color-mix(in srgb, var(--c) 22%, transparent) 0 0) bottom / 100% 1px no-repeat;}
.hfx-link-overline-underline::before,
.hfx-link-overline-underline::after {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  height: 0.1em;
  background: var(--c);
  transform: scaleX(0);
  transition: transform 0.4s cubic-bezier(0.65, 0, 0.35, 1);
}
.hfx-link-overline-underline::before { top: 0; transform-origin: left; }
.hfx-link-overline-underline::after { bottom: 0; transform-origin: right; transition-delay: 0.08s; }
.hfx-link-overline-underline:is(:hover, :focus-visible)::before,
.hfx-link-overline-underline:is(:hover, :focus-visible)::after { transform: scaleX(1); }

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

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