UX Collective

We believe designers are thinkers as much as they are makers. https://linktr.ee/uxc

Follow publication

The 🍔 menu

Mikael Ainalem
UX Collective
Published in
10 min readJun 2, 2020

Hamburger hero image showing the characteristic three line hamburger menu
hamburger menus

The anatomy of the Hamburger Menu

Image describing the two stats of the menu. The opened and the closed state
the two states of the hamburger menu

What is SVG line animation?

Pen drawing a line to illustrate the SVG line animation technique
Drawing a line

The three horizontally parallel lines

Video of drawing a hamburger
<svg width=”100” height=”100” viewBox=”0 0 100 100”>
<path d="M 20,29 H 80" /> // top line
<path d="M 20,50 H 80" /> // middle line
<path d="M 20,71 H 80" /> // bottom line

</svg>

The X close icon

1. Extend the line

2. Adjust the coordinates

3. Make the lines smooth

Video of extending a line
Completing the model

Move to HTML

<style>
.line { ... }
.line1 { ... }
.line2 { ... }
.line3 { ... }
</style>
...<svg width=”100” height=”100” viewBox=”0 0 100 100”>
<path class="line line1" … /> // top line
<path class="line line2" … /> // middle line
<path class="line line3" … /> // bottom line
</svg>
body {
align-items: center;
display: flex;
justify-content: center;
height: 100vh;
margin: 0
}

How long are these lines?

Finding the length of the lines

Diving into the SVG line animation technique

<style>
/* Hamburger */
.line1 {
stroke-dasharray 60 207;
stroke-dashoffset 0;
}
.line2 {
stroke-dasharray 60 60;
stroke-dashoffset 0;
}
.line3 {
stroke-dasharray 60 207;
stroke-dashoffset 0;
}
/* X icon */
.opened .line1 {
stroke-dasharray 90 207;
stroke-dashoffset -134;
}
.opened .line2 {
stroke-dasharray 1 60;
stroke-dashoffset -30;
}
.opened .line3 {
stroke-dasharray 90 207;
stroke-dashoffset -134;
}
</style>

Animation

transition: stroke-dasharray 600ms cubic-bezier(0.4, 0, 0.2, 1), stroke-dashoffset 600ms cubic-bezier(0.4, 0, 0.2, 1);

Putting it all together

<svg … onclick="this.classList.toggle('opened')">

Accessibility, updated version

<button onclick="..." aria-label="Main Menu">
<svg>
...
</svg>
</button>

The source code

Full source on Codepen

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Written by Mikael Ainalem

Enthusiastic about software & design, father of 3, freelancer and currently CTO at Norban | twitter: https://twitter.com/mikaelainalem

Responses (4)

Write a response

This is so not accessible.

--

First I have to say great job! This is amazing and overall I love it! I have worked out most everything to fit my needs. I do, however, have 2 things that are causing me a headache. 1. When I click the icon, I am getting a black border around it…

--

Hello Mikael, great stuff!
I've got 1 question; I used this in my website on my bootstrap collapse navbar.
The problem is when I press the hamburger again while it is animating - it causes problems.
Because I clicked twice really fast - the collapsed…

--