Hamburger to Close Icon with Clean Stroke Rotation
A hamburger icon that transforms into a close icon is one of the most recognizable patterns in navigation and wayfinding. This SVG animation example focuses on clean stroke rotation, smooth timing, and zero wobble so the transition feels crisp on any interface.
Hamburger to Close Icon with Clean Stroke Rotation
Menu toggle animations are small, but they have a big impact on perceived quality. When a hamburger icon morphs into a close icon, users instantly understand that the navigation has opened. The challenge is making that transition feel precise: strokes should rotate around stable centers, fade at the right moment, and avoid the jitter that often appears when line caps or transform origins are not carefully handled.
This SVG animation example is built for navigation and wayfinding interfaces where clarity matters. Instead of redrawing shapes or relying on complex path morphing, it uses three simple strokes that rotate and fade into a close icon. The result is clean, fast, and easy to integrate into modern UI systems.
Why this animation works well for navigation
The hamburger icon is a universal trigger for hidden menus. Its role is to signal that additional navigation exists without overwhelming the interface. When the menu opens, the icon should change state immediately and clearly. A close icon is the most familiar counterpart, and animating between the two creates a strong sense of state change.
- Instant recognition: users understand the menu has opened.
- Better feedback: the icon confirms the tap or click action.
- Compact interaction: the control occupies very little space.
- Reusable pattern: it fits headers, side drawers, mobile nav, and app toolbars.
For wayfinding, that visual transition matters. It helps users keep track of where they are in the interface and what action they can take next.
What makes stroke rotation feel clean
Many menu icon animations wobble because the lines rotate around inconsistent centers or because the stroke ends shift during the transform. In a polished implementation, the middle stroke fades out while the top and bottom strokes rotate toward the center. If the geometry is stable, the icon appears to snap naturally into an X shape without distortion.
To keep the motion clean, this approach uses a few important rules:
- Rotate each stroke around a fixed transform origin.
- Keep stroke width, line cap, and spacing consistent.
- Fade the center bar rather than forcing it to move awkwardly.
- Use easing that feels responsive but not abrupt.
- Align the final close icon so both diagonals intersect cleanly.
These details make the difference between a casual UI effect and a refined interface component.
Example structure
The following SVG pattern uses three strokes inside a button. The top and bottom lines rotate into an X, while the middle line fades out. This creates a tidy hamburger-to-close transition with minimal code.
<button class="menu-toggle" aria-label="Toggle navigation" aria-expanded="false">
<svg viewBox="0 0 24 24" aria-hidden="true" class="menu-icon">
<line class="line top" x1="5" y1="7" x2="19" y2="7" />
<line class="line middle" x1="5" y1="12" x2="19" y2="12" />
<line class="line bottom" x1="5" y1="17" x2="19" y2="17" />
</svg>
</button>For the animation layer, the icon is typically toggled with a class such as .is-open. That state switches the transform and opacity values for the three strokes.
.menu-icon .line {
stroke: currentColor;
stroke-width: 2.5;
stroke-linecap: round;
transform-box: fill-box;
transform-origin: center;
transition: transform 220ms ease, opacity 180ms ease;
}
.menu-toggle.is-open .top {
transform: translateY(5px) rotate(45deg);
}
.menu-toggle.is-open .middle {
opacity: 0;
}
.menu-toggle.is-open .bottom {
transform: translateY(-5px) rotate(-45deg);
}How to avoid stroke wobble
Stroke wobble usually happens when the browser computes the rotation from a mismatched anchor point or when the line ends are not visually centered during the movement. A few implementation choices help prevent that:
- Use consistent viewBox spacing. Symmetry makes the animation easier to align.
- Set transform-box: fill-box. This helps the browser interpret the transform origin relative to the element itself.
- Use round line caps. Rounded ends feel smoother and reduce visual harshness.
- Keep movement short. Small translations are enough to prepare the icon for the final X shape.
- Avoid path morphing unless necessary. Simple line rotation is more predictable and easier to maintain.
If you need the strictest visual control, you can also animate the lines with SVG transforms rather than CSS transforms. However, CSS is usually sufficient for a clean navigation toggle and keeps the code lightweight.
Accessibility and interaction best practices
Since this icon controls navigation, accessibility should be part of the design from the beginning. The visual animation is only one layer of the component.
- Use a real button element for keyboard support.
- Update aria-expanded when the menu opens and closes.
- Provide an accessible label such as Toggle navigation.
- Keep the hit area large enough for touch devices.
- Respect reduced-motion preferences where possible.
If motion reduction is enabled, the icon can switch instantly between states instead of animating. That preserves usability without removing the state change.
Design variations for different interfaces
Although the classic three-stroke hamburger is the most common version, the same animation logic can be adapted to different styles. For example, thinner strokes can suit minimalist websites, while thicker strokes work better in mobile apps and bold editorial layouts. You can also adjust the stroke length, spacing, and animation timing to match your brand system.
Here are a few variation ideas:
- Minimal style: thinner strokes, shorter duration, subtle easing.
- Bold style: thicker strokes, stronger contrast, slightly more spring in the motion.
- Rounded style: fully rounded line caps for a softer feel.
- Compact style: tighter spacing for small header areas.
Regardless of the style, the key is preserving the same readable transition from menu to close. That consistency helps users understand the interface instantly.
When to use this SVG animation example
This pattern is ideal when you want a polished menu trigger that supports modern navigation and wayfinding. It works especially well in:
- Responsive site headers
- Mobile navigation drawers
- App toolbars
- Single-page applications
- Landing pages with hidden menus
Because it is lightweight and scalable, SVG is a strong choice for this interaction. It stays crisp at any size, renders clearly on high-density screens, and can be styled with the same design tokens you use elsewhere in the interface.
Final thoughts
The hamburger-to-close transition may be a small detail, but it is one of the most important microinteractions in navigation design. A clean stroke rotation with controlled fading gives users immediate feedback and keeps the interface feeling responsive. This SVG animation example is a practical, elegant solution for modern navigation and wayfinding, with a smooth transformation that avoids wobble and maintains visual precision.
For designers and developers building polished menu controls, this pattern offers the right balance of simplicity, performance, and clarity.