Basic Full-Page Toggle Menu
Basic pure-CSS toggling menu with animated "hamburger"-style menu icon.
by the_voder
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<header>
<nav id="menu1" class="navmenu">
<!-- [MENU BUTTON] -->
<!-- Menu checkbox (hidden by CSS) -->
<input type="checkbox" id="menutoggle" aria-hidden="true" />
<!-- This element is the visible menu button -->
<label for="menutoggle" class="menuicon" aria-hidden="true">
<!-- These elements are used to form the menu icon -->
<span></span>
<span></span>
<span></span>
<span></span>
</label>
<!-- [MENU ITEMS] -->
<div class="menuitems">
<h3>
Section 1
</h3>
<ul>
<li><a href="#">Item 1</a></li>
<li><a href="#">Item 2</a></li>
<li><a href="#">Item 3</a></li>
</ul>
<h3>
Section 2
</h3>
<ul>
<li><a href="#">Item 4</a></li>
<li><a href="#">Item 5</a></li>
<li><a href="#">Item 6</a></li>
</ul>
</div>
</nav>
</header>
CSS
/*
Box-sizing reset. Prevents elements getting larger when padding is added. See:
https://css-tricks.com/box-sizing/
*/
html {
box-sizing: border-box;
}
*,
*:before,
*:after {
box-sizing: inherit;
}
body {
/* Zeroing body margin allows elements to go right to edge of page */
margin: 0;
}
/* Remove default list styling from nav menu */
nav ul { list-style-type: none; margin: 0; padding: 0; }
nav ul li { margin: 0; padding: 0; }
/* Menu button */
nav input {
/* Hide menu toggle checkbox */
display: none;
}
/* Menu label (visible button to toggle menu on/off) */
nav label {
position: fixed;
display: block;
width: 50px;
height: 50px;
right: 10px;
top: 10px;
/* Set high z-index to make sure menu checkbox label is on top of menu so it can be clicked when menu is visible */
z-index: 99;
}
/* MENU ICON STYLING */
nav label span {
display: block;
position: absolute;
height: 9px;
width: 100%;
background: #000;
border-radius: 9px;
opacity: 1;
left: 0;
transform: rotate(0deg);
transition: .25s ease-in-out;
}
nav label span:nth-child(1) {
top: 0px;
}
nav label span:nth-child(2),
nav label span:nth-child(3) {
top: 18px;
}
nav label span:nth-child(4) {
top: 36px;
}
/* Animate menu icon when checkbox checked */
.navmenu input:checked ~ label span:nth-child(1),
.navmenu input:checked ~ label span:nth-child(4){
top: 18px;
width: 0;
left: 50%;
}
.navmenu input:checked ~ label span:nth-child(2) {
transform: rotate(45deg);
}
.navmenu input:checked ~ label span:nth-child(3) {
transform: rotate(-45deg);
}
/* MENU STYLING */
/* Menu container */
.menuitems {
/* Use flexbox to align items within menu. See:
https://css-tricks.com/snippets/css/a-guide-to-flexbox/ */
display: flex;
position: fixed;
/* set menu to full page width/height using viewport units */
width: 100vw;
height: 100vh;
padding-top: 50px;
flex-direction: column;
align-items: center;
background-color: #666;
color:...