An add to cart button that gives a short, decaying shake every few seconds, then rests, so it nudges without nagging. The cart icon swings a beat behind the button for a bit of follow through.
Also known as: css button shake animation, wiggle button css, attention seeker button.
<button class="btn-shake" type="button"> <svg class="btn-shake-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> Add to cart </button>
.btn-shake {
--c: var(--btn-color, #3b82f6);
display: inline-flex;
align-items: center;
justify-content: center;
gap: 0.5em;
padding: 0.75em 1.6em;
border: 0;
border-radius: 0.6em;
font: 600 var(--btn-size, 18px)/1.1 system-ui, sans-serif;
cursor: pointer;
-webkit-tap-highlight-color: transparent;
padding: 0.8em 1.6em 0.8em 1.4em;
border-radius: 0.6em;
background: var(--c);
color: #fff;
animation: btn-shake-shake 3.6s ease-in-out infinite;}
.btn-shake-icon {
width: 1.2em;
height: 1.2em;
transform-origin: 50% 20%;
animation: btn-shake-swing 3.6s ease-in-out 0.06s infinite;
}
.btn-shake:is(:hover, :focus-visible) {
background: color-mix(in srgb, var(--c) 85%, #000);
animation-play-state: paused;
}
.btn-shake:active { scale: 0.96; }
@keyframes btn-shake-shake {
0%, 26%, 100% { transform: translateX(0) rotate(0deg); }
3% { transform: translateX(-0.3em) rotate(-3deg); }
7% { transform: translateX(0.3em) rotate(3deg); }
11% { transform: translateX(-0.22em) rotate(-2deg); }
15% { transform: translateX(0.16em) rotate(1.2deg); }
19% { transform: translateX(-0.08em) rotate(-0.5deg); }
23% { transform: translateX(0.03em) rotate(0.2deg); }
}
@keyframes btn-shake-swing {
0%, 28%, 100% { transform: rotate(0deg); }
5% { transform: rotate(14deg); }
10% { transform: rotate(-12deg); }
15% { transform: rotate(8deg); }
20% { transform: rotate(-4deg); }
24% { transform: rotate(2deg); }
}
@media (prefers-reduced-motion: reduce) {
.btn-shake, .btn-shake *, .btn-shake::before, .btn-shake::after, .btn-shake *::before, .btn-shake *::after {
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
}
}--btn-color on the element or a parent.
Currently previewing #3b82f6.--btn-size. Every other dimension is in
em, so the whole thing scales together. Currently previewing 18px.transition or animation
shorthand. Staggered delays are relative, so they keep their rhythm.