“How do you create an expandable hamburger menu?”
DEMO: How It Looks
My Site
Click the icon to open the menu. The links jump to the code below.
HTML
The menu icon is a real <button>, so it works with the keyboard and tells screen readers whether the menu is open (aria-expanded). Point each link to the section or page it should open.
<header class="header">
<div class="header__menu menu">
<button class="menu__icon" type="button" aria-label="Open menu" aria-expanded="false" aria-controls="menu-body">
<span></span>
</button>
<nav class="menu__body" id="menu-body" aria-label="Main menu">
<ul class="menu__list">
<li><a href="#home" class="menu__link">Home</a></li>
<li><a href="#section-1" class="menu__link">Section 1</a></li>
<li><a href="#section-2" class="menu__link">Section 2</a></li>
<li><a href="#section-3" class="menu__link">Section 3</a></li>
</ul>
</nav>
</div>
</header>
CSS
Change the colors to match your design. The menu slides in from the right and stays hidden from the keyboard until it is opened.
.header__menu {
position: fixed;
top: 34px;
right: 30px;
z-index: 10;
}
.menu__icon {
position: relative;
z-index: 5;
display: block;
width: 50px;
height: 22px;
padding: 0;
border: 0;
background: none;
cursor: pointer;
}
.menu__icon span,
.menu__icon::before,
.menu__icon::after {
position: absolute;
left: 0;
width: 100%;
height: 20%;
background-color: #111;
transition: all 0.3s ease 0s;
}
.menu__icon::before,
.menu__icon::after {
content: '';
}
.menu__icon::before {
top: 0;
}
.menu__icon::after {
bottom: 0;
}
.menu__icon span {
top: 50%;
transform: scale(1) translate(0, -50%);
}
.menu__icon._active span {
transform: scale(0) translate(0, -50%);
}
.menu__icon._active::before {
top: 50%;
transform: rotate(-45deg) translate(0, -50%);
}
.menu__icon._active::after {
bottom: 50%;
transform: rotate(45deg) translate(0, 50%);
}
.menu__body {
position: fixed;
top: 0;
right: -100%;
width: 34%;
min-width: 240px;
height: 100%;
padding: 100px 30px 30px;
box-sizing: border-box;
overflow: auto;
background-color: rgba(255, 255, 255, 0.95);
visibility: hidden;
transition: right 0.3s ease 0s, visibility 0.3s ease 0s;
}
.menu__body._active {
right: 0;
visibility: visible;
}
.menu__list {
margin: 0;
padding-left: 1rem;
list-style: none;
}
.menu__list li {
margin-bottom: 30px;
}
.menu__link {
display: block;
color: #111;
font-size: 2.2rem;
font-weight: 200;
line-height: 1.2;
text-decoration: none;
}
JavaScript
No external file is needed. Paste this script just before the closing </body> tag (or into your theme’s footer scripts). It opens and closes the menu, and closes it again when a link is chosen or the Esc key is pressed.
<script>
document.addEventListener('DOMContentLoaded', function () {
var button = document.querySelector('.menu__icon');
var menu = document.querySelector('.menu__body');
if (!button || !menu) return;
function setMenu(open) {
button.classList.toggle('_active', open);
menu.classList.toggle('_active', open);
button.setAttribute('aria-expanded', open ? 'true' : 'false');
button.setAttribute('aria-label', open ? 'Close menu' : 'Open menu');
}
button.addEventListener('click', function () {
setMenu(button.getAttribute('aria-expanded') !== 'true');
});
// Close the menu when a link is chosen or Escape is pressed
menu.addEventListener('click', function (e) {
if (e.target.closest('a')) setMenu(false);
});
document.addEventListener('keydown', function (e) {
if (e.key === 'Escape' && button.getAttribute('aria-expanded') === 'true') {
setMenu(false);
button.focus();
}
});
});
</script>
With a hamburger menu in your navigation, your website can look more sophisticated and clean.
