A Pen by Prashant Nayak on CodePen.
Created
August 21, 2021 12:03
-
-
Save prashant2k15/bdb4f8827519562bda5c109b7b473981 to your computer and use it in GitHub Desktop.
Responsive Simple Navigation Menu
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<nav class="navbar"> | |
<div class="container"> | |
<section class="wrapper"> | |
<h1 class="brand"><a href="./index.html" class="brand-link">LOGO</a></h1> | |
<button type="button" class="burger" id="burger"> | |
<span class="burger-line"></span> | |
<span class="burger-line"></span> | |
<span class="burger-line"></span> | |
<span class="burger-line"></span> | |
</button> | |
<div class="menu" id="menu"> | |
<ul class="menu-inner"> | |
<li class="menu-item"><a href="#" class="menu-link active">Home</a></li> | |
<li class="menu-item"><a href="#" class="menu-link">Profile</a></li> | |
<li class="menu-item"><a href="#" class="menu-link">Project</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> | |
</div> | |
</section> | |
</div> | |
</nav> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Initialize All Required DOM Element | |
const burgerMenu = document.getElementById("burger"); | |
const navbarMenu = document.getElementById("menu"); | |
// Initialize Responsive Navbar Menu | |
burgerMenu.addEventListener("click", () => { | |
burgerMenu.classList.toggle("active"); | |
navbarMenu.classList.toggle("active"); | |
if (navbarMenu.classList.contains("active")) { | |
navbarMenu.style.maxHeight = navbarMenu.scrollHeight + "px"; | |
} else { | |
navbarMenu.removeAttribute("style"); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:root { | |
--color-white: #fff; | |
--color-light: #f1f5f8; | |
--color-black: #121212; | |
--color-night: #001633; | |
--color-purple: #9b27b0; | |
--color-indigo: #63f; | |
--shadow-small: 0 1px 3px 0 rgba(0, 0, 0, 0.1), 0 1px 2px 0 rgba(0, 0, 0, 0.06); | |
--shadow-medium: 0 4px 6px -1px rgba(0, 0, 0, 0.1), | |
0 2px 4px -1px rgba(0, 0, 0, 0.06); | |
--shadow-large: 0 10px 15px -3px rgba(0, 0, 0, 0.1), | |
0 4px 6px -2px rgba(0, 0, 0, 0.05); | |
} | |
html { | |
font-size: 100%; | |
box-sizing: border-box; | |
scroll-behavior: smooth; | |
} | |
*, | |
*::before, | |
*::after { | |
padding: 0; | |
margin: 0; | |
box-sizing: inherit; | |
list-style: none; | |
list-style-type: none; | |
text-decoration: none; | |
-webkit-font-smoothing: antialiased; | |
-moz-osx-font-smoothing: grayscale; | |
text-rendering: optimizeLegibility; | |
} | |
body { | |
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; | |
font-size: 1rem; | |
font-weight: 400; | |
line-height: 1.5; | |
color: var(--color-black); | |
background: var(--color-white); | |
} | |
a, | |
button { | |
font-family: inherit; | |
font-size: inherit; | |
line-height: inherit; | |
cursor: pointer; | |
border: none; | |
outline: none; | |
background: none; | |
text-decoration: none; | |
} | |
img, | |
video { | |
display: block; | |
max-width: 100%; | |
height: auto; | |
object-fit: cover; | |
} | |
.container { | |
max-width: 83rem; | |
width: 100%; | |
height: auto; | |
margin: 0 auto; | |
padding: 0 2rem; | |
} | |
.brand-link { | |
font-family: inherit; | |
font-size: 1.75rem; | |
font-weight: 700; | |
line-height: inherit; | |
border: none; | |
outline: none; | |
color: var(--color-indigo); | |
text-transform: uppercase; | |
text-rendering: optimizeLegibility; | |
} | |
.navbar { | |
max-width: 100%; | |
height: auto; | |
margin: 0 auto; | |
padding: 0.75rem 0; | |
border: none; | |
outline: none; | |
color: var(--color-black); | |
background: var(--color-white); | |
box-shadow: var(--shadow-large); | |
} | |
.navbar .wrapper { | |
display: flex; | |
flex-direction: row; | |
flex-wrap: wrap; | |
justify-content: space-between; | |
align-items: center; | |
width: 100%; | |
height: auto; | |
margin: 0 auto; | |
} | |
.navbar .burger { | |
position: relative; | |
display: none; | |
cursor: pointer; | |
width: 2rem; | |
height: 1rem; | |
border: none; | |
outline: none; | |
opacity: 0; | |
visibility: hidden; | |
background: none; | |
transform: rotate(0deg); | |
transition: 0.35s ease-in-out; | |
} | |
.navbar .burger-line { | |
display: block; | |
position: absolute; | |
width: 100%; | |
height: 2px; | |
left: 0; | |
border: none; | |
outline: none; | |
opacity: 1; | |
border-radius: 0.25rem; | |
background: var(--color-black); | |
transform: rotate(0deg); | |
transition: 0.25s ease-in-out; | |
} | |
.navbar .burger-line:nth-child(1) { | |
top: 0; | |
} | |
.navbar .burger-line:nth-child(2), | |
.navbar .burger-line:nth-child(3) { | |
top: 0.5rem; | |
} | |
.navbar .burger-line:nth-child(4) { | |
top: 1rem; | |
} | |
.navbar .burger.active .burger-line:nth-child(1), | |
.navbar .burger.active .burger-line:nth-child(4) { | |
top: 1.25rem; | |
width: 0%; | |
left: 50%; | |
} | |
.navbar .burger.active .burger-line:nth-child(2) { | |
transform: rotate(45deg); | |
} | |
.navbar .burger.active .burger-line:nth-child(3) { | |
transform: rotate(-45deg); | |
} | |
.navbar .menu-inner { | |
display: flex; | |
flex-direction: row; | |
justify-content: center; | |
align-items: center; | |
gap: 2rem; | |
} | |
.navbar .menu-link { | |
font-family: inherit; | |
font-size: 1rem; | |
font-weight: 600; | |
line-height: inherit; | |
border: none; | |
color: var(--color-black); | |
text-transform: uppercase; | |
text-rendering: optimizeLegibility; | |
transition: all 0.35s ease-in-out; | |
} | |
.navbar .menu-link.active, | |
.navbar .menu-link:hover { | |
border: none; | |
outline: none; | |
color: var(--color-indigo); | |
} | |
@media only screen and (max-width: 768px) { | |
.navbar .burger { | |
display: block; | |
opacity: 1; | |
visibility: visible; | |
} | |
.navbar .menu { | |
width: 100%; | |
max-height: 0rem; | |
padding: 0; | |
opacity: 0; | |
visibility: hidden; | |
overflow: hidden; | |
transition: all 0.35s ease; | |
} | |
.navbar .menu.active { | |
opacity: 1; | |
visibility: visible; | |
} | |
.navbar .menu-inner { | |
display: flex; | |
flex-direction: column; | |
justify-content: center; | |
align-items: flex-start; | |
gap: 0.75rem; | |
padding: 1rem 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment