Responsive Navigation Bar
What you'll build
A top navigation bar with logo and links that stays horizontal on desktop and collapses into a toggleable hamburger menu below 768px.
Goals
- Use CSS media queries to switch layouts at a breakpoint
- Build a hamburger icon with pure CSS
- Toggle menu visibility with a small JS click handler
- Make the nav accessible with ARIA attributes
Prerequisites
- HTML structure (nav, ul, li, a)
- CSS flexbox
- Basic JavaScript event listeners
Steps
- 1
Write the HTML structure
Create a <nav> containing a logo, a button (hamburger toggle), and a <ul> of links. The button needs aria-expanded for accessibility.
Code<nav class="navbar"> <div class="brand">MySite</div> <button class="hamburger" aria-expanded="false" aria-label="Toggle menu"> <span></span><span></span><span></span> </button> <ul class="nav-links"> <li><a href="/">Home</a></li> <li><a href="/about">About</a></li> <li><a href="/blog">Blog</a></li> <li><a href="/contact">Contact</a></li> </ul> </nav>Expected result
A nav with brand, hamburger button, and four links renders in the browser.
- 2
Style the desktop layout
Use flexbox to put the brand on the left and links on the right. Hide the hamburger button on desktop. Add basic padding and background.
Code.navbar { display: flex; align-items: center; justify-content: space-between; background: #1f2937; padding: 1rem 2rem; color: white; } .nav-links { display: flex; gap: 1.5rem; list-style: none; } .nav-links a { color: white; text-decoration: none; } .hamburger { display: none; }Expected result
On wide screens the nav shows brand left, links right, no hamburger.
- 3
Add the mobile media query
Below 768px, hide the nav-links by default and show the hamburger. When .active is added to nav-links, show them stacked vertically.
Code@media (max-width: 768px) { .hamburger { display: flex; flex-direction: column; gap: 5px; background: none; border: none; cursor: pointer; } .hamburger span { width: 25px; height: 3px; background: white; } .nav-links { display: none; flex-direction: column; position: absolute; top: 100%; left: 0; right: 0; background: #1f2937; padding: 1rem; } .nav-links.active { display: flex; } }Expected result
Resize below 768px: links disappear, hamburger appears.
- 4
Add the JavaScript toggle
Listen for clicks on the hamburger button. Toggle the .active class on .nav-links and flip aria-expanded.
Codeconst hamburger = document.querySelector('.hamburger'); const navLinks = document.querySelector('.nav-links'); hamburger.addEventListener('click', () => { const isOpen = navLinks.classList.toggle('active'); hamburger.setAttribute('aria-expanded', String(isOpen)); });Expected result
Clicking the hamburger on mobile opens and closes the menu.
- 5
Test across breakpoints
Open the page at 1440px (links visible, no hamburger), 768px (hamburger appears), and 390px (menu toggles on click). Verify aria-expanded updates.
Code// DevTools → Toggle device toolbar → test 390px, 768px, 1024px // Console check: console.log(document.querySelector('.hamburger').getAttribute('aria-expanded'));Expected result
Menu works at all three breakpoints; aria-expanded toggles between true/false.
Try it yourself
<nav class="navbar">
<!-- TODO: brand, hamburger, nav-links -->
</nav>
<style>/* TODO: desktop + mobile styles */</style>
<script>/* TODO: toggle handler */</script><nav class="navbar">
<div class="brand">MySite</div>
<button class="hamburger" aria-expanded="false" aria-label="Toggle menu"><span></span><span></span><span></span></button>
<ul class="nav-links"><li><a href="/">Home</a></li><li><a href="/about">About</a></li></ul>
</nav>What you learned
You built the most common responsive pattern in web development — a nav that adapts to screen size — using media queries, flexbox, and a minimal JS toggle.
Related Resources
Related Lessons
- HTML Structure
Semantic nav, ul, li elements.
- CSS Layout
Flexbox and media queries drive the responsive layout.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.