Blog / Development

Animated heart in SVG and CSS: like buttons that feel good

A like button is a tiny moment, but a good heart animation makes it feel rewarding. Here is one in SVG and CSS that is accessible and light.

By Abdullah NomanDecember 3, 20263 min read

Heart balloons, hairline isometric illustration

Short answer: use a real <button> with aria-pressed, draw the heart as one SVG path, fill it with your accent when pressed, and play a short scale "pop" with CSS that turns off under prefers-reduced-motion.

Markup

<button class="like" aria-pressed="false" aria-label="Like">
  <svg viewBox="0 0 24 24" width="24" height="24" aria-hidden="true">
    <path d="M12 21C12 21 3 15 3 8.5A4.5 4.5 0 0 1 12 6a4.5 4.5 0 0 1 9 2.5C21 15 12 21 12 21z"/>
  </svg>
</button>

CSS

.like { background: none; border: 0; padding: 8px; cursor: pointer; color: #6b6b74; }
.like path { fill: transparent; stroke: currentColor; stroke-width: 1.8; transition: fill .2s; }
.like[aria-pressed="true"] { color: #e5484d; }
.like[aria-pressed="true"] path { fill: currentColor; }
.like[aria-pressed="true"] svg { animation: pop .35s cubic-bezier(.3,1.6,.6,1); }
@keyframes pop { 0% { transform: scale(.8); } 60% { transform: scale(1.2); } 100% { transform: scale(1); } }
@media (prefers-reduced-motion: reduce) { .like svg { animation: none !important; } }
.like:focus-visible { outline: 2px solid currentColor; outline-offset: 2px; border-radius: 8px; }

JavaScript

document.querySelectorAll('.like').forEach(b =>
  b.addEventListener('click', () =>
    b.setAttribute('aria-pressed', b.getAttribute('aria-pressed') !== 'true')));

React version

export function Like({ initial = false, onChange }) {
  const [on, setOn] = useState(initial);
  return (
    <button className="like" aria-pressed={on} aria-label="Like"
      onClick={() => { setOn(!on); onChange?.(!on); }}>
      <svg viewBox="0 0 24 24" width="24" height="24" aria-hidden="true"><path d="…" /></svg>
    </button>
  );
}

Details that matter

  • Tap target: the padding makes the button at least 40 px, even though the icon is 24 px.
  • Optimistic update: change the state immediately and roll back if the server request fails.
  • Count: if you show a count, update it with the state, and use tabular-nums so it does not jump.
  • No glow or particle bursts on every click; a simple pop is enough.
Heart balloons, hairline isometric illustration
For bigger moments, use an illustration instead of a bigger animation. Heart balloons

Questions

How do I make an animated heart button in CSS?

Use an SVG heart inside a button, fill it when aria-pressed is true, and play a short scale keyframe animation.

How do I make a like button accessible?

Use a real button with aria-pressed and an aria-label, a visible focus style and a large tap target.

Should like animations respect reduced motion?

Yes. Turn the pop animation off under prefers-reduced-motion.

Should a like button wait for the server?

No. Update it immediately and roll back if the request fails.

Keep reading