AI CSS Animations

Expanding Icon Button Hover

A round icon button that stretches into a pill and slides its label out to the right. The label column animates from 0fr to 1fr, so no width needs to be hard-coded.

Also known as: icon button expand on hover, circle to pill button, hover reveal label button.

HTML

<button class="hfx-icon-expand-label"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M21 11.5a8.4 8.4 0 0 1-12.2 7.5L3 21l2-5.3A8.5 8.5 0 1 1 21 11.5Z"/></svg><span class="hfx-icon-expand-label-label"><span>Chat with us</span></span></button>

CSS

.hfx-icon-expand-label {
  --c: var(--hfx-color, #3b82f6);
  font-size: var(--hfx-size, 16px);
  display: inline-grid;
  grid-template-columns: 1.4em 0fr;
  align-items: center;
  height: 3.5em;
  padding: 0 1.05em;
  border: 0;
  border-radius: 1.75em;
  background: var(--c);
  color: #fff;
  font-family: system-ui, sans-serif;
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 0.35em 0.9em color-mix(in srgb, var(--c) 25%, transparent);
  transition: grid-template-columns 0.5s cubic-bezier(0.22, 1, 0.36, 1);
}
.hfx-icon-expand-label svg {
  width: 1.4em;
  height: 1.4em;
  transition: transform 0.5s cubic-bezier(0.34, 1.56, 0.64, 1);
}
.hfx-icon-expand-label-label {
  overflow: hidden;
  white-space: nowrap;
}
.hfx-icon-expand-label-label > span {
  display: block;
  padding: 0 0.3em 0 0.65em;
  opacity: 0;
  transform: translateX(-0.6em);
  transition: opacity 0.2s ease, transform 0.4s cubic-bezier(0.22, 1, 0.36, 1);
}
.hfx-icon-expand-label:is(:hover, :focus-visible) { grid-template-columns: 1.4em 1fr; }
.hfx-icon-expand-label:is(:hover, :focus-visible) svg { transform: rotate(-12deg); }
.hfx-icon-expand-label:is(:hover, :focus-visible) .hfx-icon-expand-label-label > span {
  opacity: 1;
  transform: translateX(0);
  transition-delay: 0.1s;
}

@media (prefers-reduced-motion: reduce) {
  .hfx-icon-expand-label, .hfx-icon-expand-label *, .hfx-icon-expand-label::before, .hfx-icon-expand-label::after, .hfx-icon-expand-label *::before, .hfx-icon-expand-label *::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