Skip to content

Instantly share code, notes, and snippets.

@lkopacz
Created November 13, 2018 01:23
Show Gist options
  • Save lkopacz/3b9f67d3ef8044c717c7870097c16f3d to your computer and use it in GitHub Desktop.
Save lkopacz/3b9f67d3ef8044c717c7870097c16f3d to your computer and use it in GitHub Desktop.
Accessible Hamburger Button
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Hamburger Menu</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://fonts.googleapis.com/css?family=Montserrat" rel="stylesheet">
<style>
body {
margin: 0;
background: #eedbff;
font-family: 'Montserrat', sans-serif;
}
.visually-hidden {
position: absolute !important;
height: 1px;
width: 1px;
overflow: hidden;
clip: rect(1px 1px 1px 1px);
/* IE6, IE7 */
clip: rect(1px, 1px, 1px, 1px);
}
.hamburger {
position: relative;
z-index: 1;
margin: 1rem 1.5rem;
width: 2.75rem;
height: 3.125rem;
border: none;
background: transparent;
padding: 0;
}
.hamburger.toggled .hamburger__line {
background: #F0F0F0;
}
.hamburger__line {
display: block;
position: absolute;
top: 1.5625rem;
height: 4px;
width: 2.5rem;
background: black;
}
.hamburger__line:first-child {
top: 0.625rem;
}
.hamburger__line:last-child {
top: 2.5rem;
}
nav {
position: fixed;
top: 0;
left: -100%;
width: 100%;
height: 100vh;
background: #6505CC;
transition: left 300ms;
}
.menu {
display: flex;
flex-direction: column;
margin-top: 5rem;
padding: 0;
list-style: none;
}
.menu__link {
display: inline-block;
position: relative;
color: #F0F0F0;
margin-left: 3rem;
padding: 1rem;
font-size: 2.5rem;
text-decoration: none;
}
.menu__link::before {
content: '';
position: absolute;
z-index: -1;
height: 3px;
width: 0;
bottom: 0;
left: 0;
background: #F0F0F0;
transition: width 300ms;
}
.menu__link:hover::before {
width: 100%;
}
nav.open {
left: 0;
}
</style>
</head>
<body>
<button class="hamburger">
<span class="hamburger__line"></span>
<span class="hamburger__line"></span>
<span class="hamburger__line"></span>
</button>
<nav>
<ul class="menu">
<li class="menu__item">
<a href="./" class="menu__link">About</a>
</li>
<li class="menu__item">
<a href="./" class="menu__link">Blog</a>
</li>
<li class="menu__item">
<a href="./" class="menu__link">Contact</a>
</li>
</ul>
</nav>
<script>
const hamburgerToggle = document.querySelector('.hamburger');
const nav = document.querySelector('nav');
hamburgerToggle.addEventListener('click', () => {
nav.classList.toggle('open');
hamburgerToggle.classList.toggle('toggled');
});
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Hamburger Menu</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://fonts.googleapis.com/css?family=Montserrat" rel="stylesheet">
<style>
body {
margin: 0;
background: #eedbff;
font-family: 'Montserrat', sans-serif;
}
.visually-hidden {
position: absolute !important;
height: 1px;
width: 1px;
overflow: hidden;
clip: rect(1px 1px 1px 1px);
/* IE6, IE7 */
clip: rect(1px, 1px, 1px, 1px);
}
.hamburger {
position: relative;
z-index: 1;
margin: 1rem 1.5rem;
width: 2.75rem;
height: 3.125rem;
border: none;
background: transparent;
padding: 0;
}
.hamburger.toggled .hamburger__line {
background: #F0F0F0;
}
.hamburger__line {
display: block;
position: absolute;
top: 1.5625rem;
height: 4px;
width: 2.5rem;
background: black;
}
.hamburger__line:first-child {
top: 0.625rem;
}
.hamburger__line:last-child {
top: 2.5rem;
}
nav {
position: fixed;
top: 0;
left: -100%;
width: 100%;
height: 100vh;
background: #6505CC;
transition: left 300ms;
}
.menu {
display: flex;
flex-direction: column;
margin-top: 5rem;
padding: 0;
list-style: none;
}
.menu__link {
display: inline-block;
position: relative;
color: #F0F0F0;
margin-left: 3rem;
padding: 1rem;
font-size: 2.5rem;
text-decoration: none;
}
.menu__link::before {
content: '';
position: absolute;
z-index: -1;
height: 3px;
width: 0;
bottom: 0;
left: 0;
background: #F0F0F0;
transition: width 300ms;
}
.menu__link:hover::before {
width: 100%;
}
nav.open {
left: 0;
}
</style>
</head>
<body>
<div class="hamburger">
<span class="hamburger__line"></span>
<span class="hamburger__line"></span>
<span class="hamburger__line"></span>
</div>
<nav>
<ul class="menu">
<li class="menu__item">
<a href="./" class="menu__link">About</a>
</li>
<li class="menu__item">
<a href="./" class="menu__link">Blog</a>
</li>
<li class="menu__item">
<a href="./" class="menu__link">Contact</a>
</li>
</ul>
</nav>
<script>
const hamburgerToggle = document.querySelector('.hamburger');
const nav = document.querySelector('nav');
hamburgerToggle.addEventListener('click', () => {
nav.classList.toggle('open');
hamburgerToggle.classList.toggle('toggled');
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment