A responsive navbar is one of the first things every web developer builds, and one of the easiest to get subtly wrong. In this tutorial we'll make a mobile-first navigation bar that collapses into a hamburger menu on small screens, using plain HTML, CSS, and a few lines of JavaScript.
By the end you'll understand:
- Mobile-first CSS and
min-widthmedia queries - How to toggle a menu with a class
- Basic accessibility with
aria-expandedand a real<button>
Step 1: The HTML
Use semantic elements. The toggle should be a <button>, not a <div> or a link, so keyboard and screen reader users can use it.
<header class="site-header">
<a href="#home" class="logo">MySite</a>
<button
class="menu-toggle"
aria-label="Toggle menu"
aria-expanded="false"
aria-controls="nav-menu"
>
☰
</button>
<nav id="nav-menu" class="nav">
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#about">About</a></li>
<li><a href="#courses">Courses</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
</nav>
</header>
aria-controls links the button to the menu it controls, and aria-expanded tells assistive technology whether the menu is currently open.
Step 2: Mobile-first CSS
We write the small-screen styles first, then enhance for larger screens.
* {
box-sizing: border-box;
}
body {
margin: 0;
font-family: system-ui, sans-serif;
}
.site-header {
position: relative;
display: flex;
justify-content: space-between;
align-items: center;
padding: 1rem 1.5rem;
background: #1f2937;
color: #fff;
}
.logo {
color: #fff;
font-size: 1.25rem;
font-weight: 700;
text-decoration: none;
}
.menu-toggle {
background: none;
border: 1px solid #fff;
border-radius: 4px;
color: #fff;
font-size: 1.25rem;
padding: 0.25rem 0.6rem;
cursor: pointer;
}
/* Mobile: the menu is hidden until toggled */
.nav {
display: none;
position: absolute;
top: 100%;
left: 0;
right: 0;
background: #374151;
}
.nav.open {
display: block;
}
.nav ul {
list-style: none;
margin: 0;
padding: 0;
}
.nav a {
display: block;
padding: 0.9rem 1.5rem;
color: #fff;
text-decoration: none;
}
.nav a:hover,
.nav a:focus-visible {
background: #4b5563;
}
Step 3: Enhance for larger screens
Now we add one media query. On screens 768px or wider, we hide the button and show the menu in a row.
@media (min-width: 768px) {
.menu-toggle {
display: none;
}
.nav {
display: block;
position: static;
background: none;
}
.nav ul {
display: flex;
gap: 0.5rem;
}
.nav a {
padding: 0.5rem 0.9rem;
border-radius: 4px;
}
}
Because we started mobile-first, we only needed to override a few properties instead of undoing a desktop layout.
Step 4: The JavaScript
const toggle = document.querySelector('.menu-toggle');
const nav = document.querySelector('#nav-menu');
toggle.addEventListener('click', () => {
const isOpen = nav.classList.toggle('open');
toggle.setAttribute('aria-expanded', String(isOpen));
});
classList.toggle() returns true if the class was added and false if it was removed, so we can reuse the result to keep aria-expanded accurate.
Bonus: close the menu with the Escape key
document.addEventListener('keydown', (event) => {
if (event.key === 'Escape' && nav.classList.contains('open')) {
nav.classList.remove('open');
toggle.setAttribute('aria-expanded', 'false');
toggle.focus();
}
});
Returning focus to the button after closing is a small detail that makes keyboard navigation much nicer.
Common mistakes to avoid
| Mistake | Better approach |
|---|---|
Using a <div> as the toggle |
Use a <button>
|
Forgetting aria-expanded
|
Update it whenever the menu opens or closes |
Desktop-first CSS with max-width
|
Mobile-first with min-width
|
| Fixed pixel widths | Flexbox and relative units |
| Never testing with a keyboard | Tab through the menu yourself |
Challenges to try next
- Close the menu when a link is clicked.
- Animate the menu with a
max-heightortransformtransition. - Replace
☰with an SVG icon that changes to an X when open. - Add a dropdown submenu (and think about its accessibility).
- Rebuild this as a React component using
useState.
Wrapping up
You now have a responsive, keyboard-friendly navbar in about 60 lines of CSS and 6 lines of JavaScript. The key ideas, mobile-first styles, semantic HTML, and toggling state with a class, apply to almost every component you'll build.
If you want guided practice on these fundamentals, 99eedu in Kolkata offers courses on Responsive HTML, Advanced JavaScript, and React JS. (Disclosure: I work with the 99eedu training team.)
Top comments (0)