AI CSS Animations

Text Swap Button

The label slides up and out while a second label, stored in a data attribute, slides in from below. Handy for 'Subscribe' becoming 'Let's go' without any JavaScript.

Also known as: label change on hover, sliding text button.

HTML

<button class="hfx-swap-text" data-hover="Let's go!"><span>Subscribe</span></button>

CSS

.hfx-swap-text {
  --c: var(--hfx-color, #3b82f6);
  font-size: var(--hfx-size, 16px);
  display: inline-flex;
  align-items: center;
  gap: 0.5em;
  padding: 0.7em 1.6em;
  border-radius: 0.5em;
  font: 600 1em system-ui, sans-serif;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  border: 0;
  background: var(--c);
  color: #fff;}
.hfx-swap-text span {
  display: block;
  transition: transform 0.35s ease;
}
.hfx-swap-text::after {
  content: attr(data-hover);
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  transform: translateY(100%);
  transition: transform 0.35s ease;
}
.hfx-swap-text:hover span { transform: translateY(-300%); }
.hfx-swap-text:hover::after { transform: translateY(0); }

@media (prefers-reduced-motion: reduce) {
  .hfx-swap-text, .hfx-swap-text *, .hfx-swap-text::before, .hfx-swap-text::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