AI CSS Animations

Icon Reveal Button

A cart icon drives in from the left edge on hover while the label steps aside to make room. The icon waits outside the clipped button until the cursor arrives, then lands with a small overshoot.

Also known as: icon slide in button, add to cart button hover, hidden icon button.

HTML

<button class="btn-icon-slide-in">
  <svg class="btn-icon-slide-in-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><circle cx="9" cy="20" r="1.5"/><circle cx="18" cy="20" r="1.5"/><path d="M2 3h3l2.6 12.2a2 2 0 0 0 2 1.6h8.2a2 2 0 0 0 2-1.5L21.5 8H6.2"/></svg>
  <span class="btn-icon-slide-in-label">Add to cart</span>
</button>

CSS

.btn-icon-slide-in {
  --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;
  position: relative;
  overflow: hidden;
  padding: 0.75em 1.8em;
  border: 0;
  border-radius: 0.5em;
  background: var(--c);
  color: #fff;
  transition: background-color 0.3s ease;}
.btn-icon-slide-in-icon {
  position: absolute;
  top: 50%;
  left: 0.9em;
  width: 1.2em;
  height: 1.2em;
  margin-top: -0.6em;
  transform: translateX(-2.4em);
  transition: transform 0.4s cubic-bezier(0.34, 1.4, 0.64, 1);
}
.btn-icon-slide-in-label {
  transition: transform 0.4s cubic-bezier(0.34, 1.4, 0.64, 1);
}
.btn-icon-slide-in:is(:hover, :focus-visible) { background-color: color-mix(in srgb, var(--c) 85%, #000); }
.btn-icon-slide-in:is(:hover, :focus-visible) .btn-icon-slide-in-icon { transform: translateX(0); }
.btn-icon-slide-in:is(:hover, :focus-visible) .btn-icon-slide-in-label { transform: translateX(0.8em); }

@media (prefers-reduced-motion: reduce) {
  .btn-icon-slide-in, .btn-icon-slide-in *, .btn-icon-slide-in::before, .btn-icon-slide-in::after, .btn-icon-slide-in *::before, .btn-icon-slide-in *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

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