In this post, I will share with you a project of a responsive navigation bar with dropdown menu.
Here is the live demo of the navigation bar –
<header class="header">
<div class="header-wrap flex">
<section class="logo"><a href="">DevBabu.Com</a></section>
<section class="navbar">
<button class="close-navbar">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-x"><line x1="18" y1="6" x2="6" y2="18"></line><line x1="6" y1="6" x2="18" y2="18"></line></svg>
</button>
<nav>
<ul class="flex">
<li><a href="">Home</a></li>
<li><a href="">About</a></li>
<li><a href="">Menu</a>
<ul>
<li><a href="">Item 1 is red</a></li>
<li><a href="">Item 2 is blue</a></li>
<li><a href="">Item 3 is green</a></li>
<li><a href="">Item 4 is yellow</a></li>
</ul>
</li>
<li><a href="">Contact</a></li>
</ul>
</nav>
</section>
<section class="open-navbar">
<button>
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-menu"><line x1="3" y1="12" x2="21" y2="12"></line><line x1="3" y1="6" x2="21" y2="6"></line><line x1="3" y1="18" x2="21" y2="18"></line></svg>
</button>
</section>
</div>
</header>
@charset "UTF-8";
@import url("https://fonts.googleapis.com/css2?family=Lobster&family=Open+Sans:wght@400;700&display=swap");
*,
*::before,
*::after {
box-sizing: border-box;
line-height: 1.5em;
}
html {
font-size: 16px;
-webkit-text-size-adjust: 100%;
}
body {
margin: 0;
overflow-wrap: break-word;
font-family: "Open Sans", sans-serif;
background-color: #f7f7f7;
-moz-osx-font-smoothing: grayscale;
-webkit-font-smoothing: antialiased;
}
.flex {
display: flex;
flex-wrap: wrap;
}
.header {
background-color: #19191c;
z-index: 1;
}
.header a {
color: #cccccc;
text-decoration: none;
}
.header-wrap {
max-width: 900px;
margin: 0 auto;
padding: 0 20px;
display: flex;
flex-wrap: wrap;
align-items: center;
}
.header-wrap ul {
padding: 0;
margin: 0;
align-items: stretch;
z-index: 9;
}
.header-wrap ul a {
text-transform: uppercase;
font-weight: bold;
}
.header-wrap ul ul {
display: block;
}
.header-wrap ul ul a {
text-transform: capitalize;
}
.header-wrap .logo {
flex-grow: 1;
font-size: 28px;
font-family: "Lobster", cursive;
}
.header-wrap .logo a {
color: #f9f9f9;
}
.navbar {
background-color: #19191c;
position: fixed;
top: 0;
right: -120%;
width: 70%;
height: 100%;
z-index: 999;
padding: 30px;
opacity: 0;
transition: right 0.3s, opacity 0.4s;
}
.navbar ul {
flex-direction: column;
gap: 20px;
list-style-type: none;
font-size: 22px;
}
.navbar ul a:hover {
color: #f9f9f9;
}
.navbar ul ul {
margin-left: 1.25rem;
font-weight: normal;
}
.navbar ul ul a {
font-weight: normal;
font-size: 0.8em;
}
.navbar ul ul li::before {
content: "⁍";
margin-right: 5px;
color: #cccccc;
}
.open-navbar button,
.close-navbar {
all: unset;
color: #cccccc;
cursor: pointer;
padding: 20px 10px;
display: flex;
}
.open-navbar button:hover,
.close-navbar:hover {
color: #f9f9f9;
}
.close-navbar {
position: absolute;
top: -10px;
right: 0%;
}
.show-navbar {
right: 0;
opacity: 1;
}
.show-navbar ul ul {
height: 0;
overflow: hidden;
}
.show-navbar ul ul a {
text-decoration: none;
}
.show-navbar ul ul.show-submenu {
height: auto;
}
.show-navbar ul .submenu-btn::after {
content: "⭥";
margin-left: 5px;
}
@media (width >= 720px) {
.header-wrap ul a {
padding-left: 20px;
padding-right: 20px;
}
}
@media (width >= 620px) {
.navbar {
display: block;
position: relative;
padding: 0;
inset: 0 !important;
width: auto;
height: auto;
opacity: 1 !important;
}
.navbar ul {
flex-direction: row;
font-size: 1rem;
}
.navbar ul a {
display: block;
padding: 30px 15px;
}
.navbar ul a:hover {
text-decoration: underline;
background-color: rgba(0, 0, 0, 0.9);
}
.navbar ul li ul {
background-color: #19191c;
position: absolute;
opacity: 0;
visibility: hidden;
translate: 0 24px;
transition: 0.3s;
margin: 0;
}
.navbar ul li ul a {
padding: 9px 12px;
font-size: 14px;
text-decoration: none !important;
}
.navbar ul ul li::before {
all: unset;
}
.navbar ul li:hover ul {
visibility: visible;
translate: 0 0;
opacity: 1;
}
.navbar ul li:hover a {
color: #f9f9f9;
text-decoration: underline;
}
.close-navbar,
.open-navbar {
display: none;
}
}
This javascript code is for mobile devices only –
const navbar = document.querySelector('.navbar');
const openNav = document.querySelector('.open-navbar button');
const closeNav = document.querySelector('.close-navbar');
const innerUl = document.querySelectorAll('.navbar ul ul');
function submenuTrigger(e, el){
e.preventDefault();
el.classList.toggle('show-submenu');
}
innerUl.forEach((el) => {
if(el.previousElementSibling.nodeName === 'A'){
el.previousElementSibling.onclick = (e) => submenuTrigger(e, el);
}
el.previousElementSibling.classList.add("submenu-btn");
});
openNav.onclick = () => navbar.classList.add('show-navbar');
closeNav.onclick = () => navbar.classList.remove('show-navbar');