AI CSS Animations

Underline Button

A quiet text button whose underline draws in from the left on hover and leaves to the right when the cursor goes, instead of shrinking back. The trick is flipping transform-origin between states.

Also known as: underline hover button, text button underline animation.

HTML

<button class="btn-underline-button"><span class="btn-underline-button-label">Read the story</span></button>

CSS

.btn-underline-button {
  --c: var(--btn-color, #3b82f6);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5em;
  font: 600 var(--btn-size, 18px)/1.2 system-ui, sans-serif;
  cursor: pointer;
  padding: 0.7em 1.3em;
  border: 0;
  border-radius: 0.5em;
  background-color: color-mix(in srgb, var(--c) 8%, transparent);
  color: var(--c);
  transition: background-color 0.3s ease;}
.btn-underline-button-label { position: relative; }
.btn-underline-button-label::after {
  content: "";
  position: absolute;
  right: 0;
  bottom: -0.2em;
  left: 0;
  height: 2px;
  background: currentColor;
  transform: scaleX(0);
  transform-origin: right;
  transition: transform 0.4s cubic-bezier(0.65, 0, 0.35, 1);
}
.btn-underline-button:is(:hover, :focus-visible) { background-color: color-mix(in srgb, var(--c) 14%, transparent); }
.btn-underline-button:is(:hover, :focus-visible) .btn-underline-button-label::after {
  transform: scaleX(1);
  transform-origin: left;
}

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

<button class="group rounded-lg bg-blue-500/10 px-5 py-3 font-semibold text-blue-500 transition hover:bg-blue-500/15">
  <span class="relative">Read the story<span class="absolute inset-x-0 -bottom-1 h-0.5 origin-right scale-x-0 bg-current transition-transform duration-300 group-hover:origin-left group-hover:scale-x-100"></span></span>
</button>

Customizing this snippet

  • Color: set --btn-color on the element or a parent. Currently previewing #3b82f6.
  • Base size: set --btn-size. Every other dimension is in em, so the whole thing scales together. Currently previewing 18px.
  • 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 buttons