Animated SVG Icons

Animated icons provide subtle but effective feedback to users. One of the most common examples is the "Hamburger" menu icon that transforms into an "X" when clicked.

SVG is the perfect tool for this because you can animate the individual lines or even morph the entire shape using the techniques we've learned.

The Hamburger to "X" Transition

There are two main ways to achieve this transition:

  1. CSS Transforms: Rotate the top and bottom lines and hide the middle line. This is the simplest method and highly performant.
  2. Path Morphing (SMIL/JS): Literally move the coordinates of the path points to create the "X" shape. This allows for more organic or unique transitions.

Example: Interactive Menu Icon

In the example below, click the hamburger icon to see it morph into an "X" using a combination of SMIL path data animation and opacity.

View Output

Accessibility for Animated Icons

When an icon changes its state (like a hamburger menu becoming an "X"), it's crucial that users with assistive technologies (like screen readers) can understand what the icon represents and its current state. SVG provides two primary tags for this:

In the example above, we've enhanced the <svg> tag with several accessibility features:

Accessibility Best Practices