How to Create a Responsive Navbar

Table of contents
A clean, responsive navbar is essential for any website. Whether you're building a portfolio or a startup landing page, this guide will show you how to create a simple, mobile-friendly navigation bar using just HTML and CSS.
What You'll Learn
How to build a semantic navbar with HTML
Styling it using modern CSS (Flexbox + Media Queries)
Making it responsive for mobile devices
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Responsive Navbar</title> <link rel="stylesheet" href="style.css" /> </head> <body> <nav class="navbar"> <div class="logo">MySite</div> <ul class="nav-links"> <li><a href="#">Home</a></li> <li><a href="#">About</a></li> <li><a href="#">Projects</a></li> <li><a href="#">Contact</a></li> </ul> <div class="menu-icon" id="menu-icon"> ☰ <!-- this is the ☰ icon --> </div> </nav> </body> </html>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', sans-serif;
}
.navbar {
display: flex;
justify-content: space-between;
align-items: center;
background-color: #0d1117;
padding: 1rem 2rem;
}
.logo {
color: #ffffff;
font-size: 1.5rem;
font-weight: bold;
}
.nav-links {
list-style: none;
display: flex;
gap: 1.5rem;
}
.nav-links a {
color: #ffffff;
text-decoration: none;
font-weight: 500;
}
.nav-links a:hover {
color: #58a6ff;
}
@media (max-width: 768px) {
.nav-links {
flex-direction: column;
position: absolute;
top: 70px;
right: 20px;
background-color: #0d1117;
padding: 1rem;
display: none;
}
.nav-links.active {
display: flex;
}
}
.menu-icon {
display: none;
font-size: 2rem;
color: #ffffff;
cursor: pointer;
}
@media (max-width: 768px) {
.nav-links {
flex-direction: column;
position: absolute;
top: 70px;
right: 20px;
background-color: #0d1117;
padding: 1rem;
display: none;
}
.nav-links.active {
display: flex;
}
.menu-icon {
display: block;
}
}
const menuIcon = document.getElementById('menu-icon');
const navLinks = document.querySelector('.nav-links');
menuIcon.addEventListener('click', () => {
navLinks.classList.toggle('active');
});
Final Output
You’ll have a dark-themed responsive navbar that adjusts on small screens. Want to add a toggle menu for mobile? That’s a great next step!
https://codepen.io/24R11D5804-KESARI-SRAVANI/pen/jEEGQNK
Conclusion
This was a quick intro to building a responsive navbar using HTML and CSS. Practice by:
Changing the layout
Adding a logo image
Experimenting with animations
👉 If you liked this tutorial, share it or follow me for more frontend tips!
Subscribe to my newsletter
Read articles from KESARI SRAVANI directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
