AI CSS Animations

Product Card Add to Cart Hover

An online store product card: the item lifts and tilts while an Add to cart button slides up from the bottom of the photo. It also opens on keyboard focus through :focus-within.

Also known as: product card hover effect, ecommerce card hover, add to cart button on hover.

Stoneware mug

$28

HTML

<article class="hfx-card-add-to-cart">
  <div class="hfx-card-add-to-cart-media">
    <svg class="hfx-card-add-to-cart-mug" viewBox="0 0 48 48" aria-hidden="true">
      <path class="hfx-card-add-to-cart-steam" d="M19 3.5c-2.2 2.5 2.2 4.5 0 7.5M26 3.5c-2.2 2.5 2.2 4.5 0 7.5"/>
      <path class="hfx-card-add-to-cart-handle" d="M36 19.5h2.5a5 5 0 0 1 0 10H36"/>
      <path d="M9 15h28v16a10 10 0 0 1-10 10h-8A10 10 0 0 1 9 31z"/>
    </svg>
    <button class="hfx-card-add-to-cart-add">Add to cart</button>
  </div>
  <div class="hfx-card-add-to-cart-body">
    <h3>Stoneware mug</h3>
    <p>$28</p>
  </div>
</article>

CSS

.hfx-card-add-to-cart {
  --c: var(--hfx-color, #3b82f6);
  font-size: var(--hfx-size, 16px);
  width: 15em;
  border-radius: 0.75em;
  overflow: hidden;
  background: #fff;
  border: 1px solid color-mix(in srgb, #000 10%, transparent);
  color: #1f2937;
  font-family: system-ui, sans-serif;
  width: 12em;
  transition: transform 0.35s cubic-bezier(0.2, 0.8, 0.2, 1), box-shadow 0.35s ease;}
.hfx-card-add-to-cart:is(:hover, :focus-within) {
  transform: translateY(-0.25em);
  box-shadow: 0 0.9em 1.4em -0.9em color-mix(in srgb, #000 35%, transparent);
}
.hfx-card-add-to-cart-media {
  position: relative;
  display: grid;
  place-items: center;
  height: 6.6em;
  overflow: hidden;
  background: radial-gradient(circle at 50% 42%, #fff 0 22%, transparent 60%), color-mix(in srgb, var(--c) 16%, transparent);
}
.hfx-card-add-to-cart-mug {
  width: 3.6em;
  height: 3.6em;
  fill: var(--c);
  transition: transform 0.5s cubic-bezier(0.34, 1.56, 0.64, 1);
}
.hfx-card-add-to-cart:is(:hover, :focus-within) .hfx-card-add-to-cart-mug {
  transform: translateY(-0.7em) rotate(-8deg);
}
.hfx-card-add-to-cart-handle {
  fill: none;
  stroke: var(--c);
  stroke-width: 3.5;
}
.hfx-card-add-to-cart-steam {
  fill: none;
  stroke: color-mix(in srgb, var(--c) 55%, transparent);
  stroke-width: 2.2;
  stroke-linecap: round;
  opacity: 0;
  transform: translateY(0.25em);
  transition: opacity 0.3s ease, transform 0.6s ease;
}
.hfx-card-add-to-cart:is(:hover, :focus-within) .hfx-card-add-to-cart-steam {
  opacity: 1;
  transform: none;
}
.hfx-card-add-to-cart-add {
  position: absolute;
  inset: auto 0.7em 0.7em;
  padding: 0.65em;
  border: 0;
  border-radius: 0.6em;
  background: var(--c);
  color: #fff;
  font: 600 0.8em system-ui, sans-serif;
  cursor: pointer;
  transform: translateY(calc(100% + 1em));
  transition: transform 0.4s cubic-bezier(0.2, 0.8, 0.2, 1), background-color 0.2s ease;
}
.hfx-card-add-to-cart:is(:hover, :focus-within) .hfx-card-add-to-cart-add { transform: none; }
.hfx-card-add-to-cart-add:hover { background: color-mix(in srgb, var(--c) 85%, #000); }
.hfx-card-add-to-cart-body {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  padding: 0.75em 0.9em 0.85em;
}
.hfx-card-add-to-cart h3 {
  font-weight: 600;
  margin: 0;
  font-size: 0.9em;
}
.hfx-card-add-to-cart p {
  margin: 0;
  font-size: 0.9em;
  font-weight: 700;
  color: var(--c);
}

@media (prefers-reduced-motion: reduce) {
  .hfx-card-add-to-cart, .hfx-card-add-to-cart *, .hfx-card-add-to-cart::before, .hfx-card-add-to-cart::after, .hfx-card-add-to-cart *::before, .hfx-card-add-to-cart *::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.

<article class="group w-48 overflow-hidden rounded-xl border bg-white transition duration-300 hover:-translate-y-1 hover:shadow-lg">
  <div class="relative grid h-28 place-items-center overflow-hidden bg-blue-50">
    <svg class="h-14 w-14 fill-blue-500 transition duration-500 group-hover:-translate-y-2 group-hover:-rotate-6" viewBox="0 0 48 48">
      <path d="M36 19.5h2.5a5 5 0 0 1 0 10H36" fill="none" stroke="currentColor" stroke-width="3.5" class="text-blue-500"/>
      <path d="M9 15h28v16a10 10 0 0 1-10 10h-8A10 10 0 0 1 9 31z"/>
    </svg>
    <button class="absolute inset-x-2 bottom-2 translate-y-[calc(100%+1rem)] rounded-lg bg-blue-500 py-2 text-sm font-semibold text-white transition duration-300 group-hover:translate-y-0">Add to cart</button>
  </div>
  <div class="flex items-baseline justify-between px-3 py-3">
    <h3 class="text-sm font-semibold">Stoneware mug</h3>
    <p class="text-sm font-bold text-blue-500">$28</p>
  </div>
</article>

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