Part of our complete guide: CSS Snippets →
UI Component · HTML + CSS + JS
Responsive Navbar
Full navigation bar with mobile hamburger menu and dropdown. Desktop shows horizontal links, mobile shows a toggle menu. Fully responsive.
Last updated: October 2026 · 8 min read
A responsive navbar shows horizontal links on desktop and a hamburger menu on mobile. The hamburger toggles a class that reveals the vertical menu. Desktop dropdowns show on hover; mobile dropdowns expand inline.
What you get: Complete navbar with logo, 4 nav links (one with dropdown), CTA button, and mobile hamburger. Fully responsive with keyboard support and outside-click closing.
Why navbars are the hardest simple component
A navbar looks trivial — a row of links and a logo. But every navbar has to solve six problems simultaneously: it must be sticky without overlapping content, responsive without breaking layout, dropdown-accessible on desktop without clipping, expandable on mobile without trapping the user, keyboard navigable for accessibility, and visually clear without being loud.
The mobile hamburger is where 80% of implementations fail. Common issues: the menu doesn't scroll when it has many items, it doesn't close when you tap a link, it overlaps the page content, or the hamburger icon doesn't change to an X. Each of these is fixable, but only if you've seen them before.
This page shows the complete pattern — desktop with hover dropdowns, mobile with a scrollable menu, plus the 20 lines of JavaScript that make the mobile toggle reliable and accessible. The CSS alone handles 90% of the visual; the JS handles the state management that pure CSS can't.
🎬 Live Navbar
Resize your browser to see the mobile version. Click the hamburger below 768px.
Try resizing your browser window or opening this page on mobile.
Complete code
<nav class="navbar">
<a href="/" class="logo">✨ Acme</a>
<ul class="nav-links" id="navLinks">
<li><a href="/">Home</a></li>
<li><a href="/features">Features</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle">Resources ▾</a>
<ul class="dropdown-menu">
<li><a href="/docs">Documentation</a></li>
<li><a href="/tutorials">Tutorials</a></li>
<li><a href="/blog">Blog</a></li>
</ul>
</li>
<li><a href="/pricing">Pricing</a></li>
<li><a href="/signup" class="cta">Get Started</a></li>
</ul>
<button class="hamburger" id="hamburger" aria-label="Toggle menu">☰</button>
</nav>
<style>
.navbar {
position: sticky;
top: 0;
z-index: 50;
background: #0f172a;
border-bottom: 1px solid #1e293b;
padding: 0 1.5rem;
height: 64px;
display: flex;
align-items: center;
justify-content: space-between;
}
.logo {
color: #fff;
font-weight: 900;
font-size: 1rem;
text-decoration: none;
}
.nav-links {
display: flex;
gap: 1.5rem;
align-items: center;
list-style: none;
margin: 0;
padding: 0;
}
.nav-links a {
color: #cbd5e1;
text-decoration: none;
font-size: 0.85rem;
font-weight: 600;
transition: color 0.2s;
}
.nav-links a:hover {
color: #60a5fa;
}
.cta {
background: #3b82f6;
color: #fff !important;
padding: 0.5rem 1rem;
border-radius: 0.5rem;
}
.cta:hover {
background: #2563eb;
}
/* Dropdown */
.dropdown {
position: relative;
}
.dropdown-menu {
position: absolute;
top: calc(100% + 8px);
left: 0;
min-width: 180px;
background: #1e293b;
border: 1px solid #334155;
border-radius: 0.5rem;
padding: 0.375rem 0;
box-shadow: 0 8px 24px rgba(0,0,0,0.3);
opacity: 0;
visibility: hidden;
transform: translateY(-4px);
transition: all 0.2s ease;
z-index: 20;
list-style: none;
margin: 0;
}
.dropdown:hover .dropdown-menu {
opacity: 1;
visibility: visible;
transform: translateY(0);
}
.dropdown-menu a {
display: block;
padding: 0.5rem 1rem;
color: #cbd5e1;
text-decoration: none;
font-size: 0.82rem;
}
.dropdown-menu a:hover {
background: #334155;
color: #fff;
}
/* Hamburger (hidden on desktop) */
.hamburger {
display: none;
background: none;
border: none;
color: #fff;
font-size: 1.5rem;
cursor: pointer;
padding: 0.5rem;
}
/* Mobile */
@media (max-width: 768px) {
.hamburger {
display: block;
}
.nav-links {
position: absolute;
top: 100%;
left: 0;
right: 0;
background: #0f172a;
flex-direction: column;
align-items: stretch;
gap: 0;
padding: 1rem;
border-bottom: 1px solid #1e293b;
max-height: calc(100vh - 64px);
overflow-y: auto;
-webkit-overflow-scrolling: touch;
display: none;
}
.nav-links.active {
display: flex;
}
.nav-links li {
width: 100%;
}
.nav-links a {
display: block;
padding: 0.75rem 1rem;
font-size: 0.9rem;
}
/* Inline dropdown on mobile */
.dropdown-menu {
position: static;
opacity: 1;
visibility: visible;
transform: none;
box-shadow: none;
background: transparent;
border: none;
padding: 0;
margin: 0;
display: none;
}
.dropdown.open .dropdown-menu {
display: block;
}
}
</style>
<script>
// Mobile hamburger toggle
const hamburger = document.getElementById('hamburger');
const navLinks = document.getElementById('navLinks');
hamburger.addEventListener('click', () => {
navLinks.classList.toggle('active');
const isOpen = navLinks.classList.contains('active');
hamburger.textContent = isOpen ? '✕' : '☰';
hamburger.setAttribute('aria-expanded', isOpen);
});
// Close menu on link click
navLinks.querySelectorAll('a').forEach(link => {
link.addEventListener('click', () => {
navLinks.classList.remove('active');
hamburger.textContent = '☰';
hamburger.setAttribute('aria-expanded', 'false');
});
});
// Close menu on outside click
document.addEventListener('click', (e) => {
if (!navLinks.contains(e.target) && !hamburger.contains(e.target)) {
navLinks.classList.remove('active');
hamburger.textContent = '☰';
hamburger.setAttribute('aria-expanded', 'false');
}
});
// Close menu on Escape
document.addEventListener('keydown', (e) => {
if (e.key === 'Escape' && navLinks.classList.contains('active')) {
navLinks.classList.remove('active');
hamburger.textContent = '☰';
hamburger.setAttribute('aria-expanded', 'false');
}
});
// Mobile dropdown toggle
document.querySelectorAll('.dropdown-toggle').forEach(toggle => {
toggle.addEventListener('click', (e) => {
if (window.innerWidth <= 768) {
e.preventDefault();
toggle.parentElement.classList.toggle('open');
}
});
});
</script>
How the navbar works
position: sticky; top: 0 keeps the navbar at the top of the viewport when scrolling, but unlike position: fixed, it doesn't take the element out of the document flow. This means no need for a spacer div below the navbar — the page content flows naturally.
The desktop navbar is a flex row: logo on the left, links on the right. justify-content: space-between pushes them to the edges. The .nav-links ul is another flex container with gap for spacing. This is the standard pattern — no floats, no absolute positioning for links.
Below 768px, the .nav-links becomes position: absolute; flex-direction: column; display: none. The hamburger button appears. Clicking it toggles the .active class, which sets display: flex. The nav links now render as a vertical menu below the navbar. No horizontal scrolling on mobile — everything stacks.
The critical fix: max-height: calc(100vh - 64px); overflow-y: auto on the mobile menu. Without these, a menu with 10+ items extends past the viewport and can't be scrolled — users get stuck. The -webkit-overflow-scrolling: touch adds iOS momentum scrolling.
Desktop: .dropdown:hover .dropdown-menu shows the menu absolutely positioned below the trigger. Mobile: the menu becomes static (flows inline), and clicking the trigger toggles a class that shows it. The same HTML, two different behaviors driven by media query and JS.
The mobile menu should close when you click anywhere else. The 5-line JS listener checks if the click target is outside the menu and the hamburger. If so, it removes the active class. This is the standard pattern — pure CSS can't do this reliably.
🔬 Edge cases
The problems that break navbars in production.
The mobile menu extends past the viewport with no way to reach the bottom items. Fix: max-height: calc(100vh - 64px); overflow-y: auto on the mobile menu container. This is the #1 mobile navbar bug.
User clicks "Pricing" — page navigates, but the menu is still open. Fix: add a click listener to every link that removes the active class. Or navigate away, which naturally closes it — but for same-page anchors, you need the handler.
If the navbar uses position: fixed, it overlays content — the top of the page is hidden behind it. Fix: use position: sticky instead. It stays in the flow, so no overlap. This is why sticky is the modern standard.
Hover-based dropdowns get stuck on touch devices — first tap opens, but nothing closes it. Fix: use @media (hover: none) to switch to click behavior on touch, or use the <details> pattern which works everywhere.
Keyboard users can tab to the trigger but the dropdown never opens. Fix: add :focus-within to the dropdown container:
.dropdown:hover .dropdown-menu,
.dropdown:focus-within .dropdown-menu {
opacity: 1;
visibility: visible;
}
Screen readers don't know if the menu is open or closed. Fix: update aria-expanded on the hamburger in JS:
hamburger.setAttribute('aria-expanded', isOpen);
The navbar appears under other content (modals, dropdowns from other components). Fix: give the navbar z-index: 50 or higher, and check that no parent creates a stacking context with a lower z-index.
Clicking the hamburger opens the menu, but the icon stays as ☰ instead of ✕. Fix: update the button text on toggle:
hamburger.textContent = isOpen ? '✕' : '☰';
Or use an SVG icon and toggle a class.
⚡ Performance & accessibility
| Aspect | Implementation | Notes |
|---|---|---|
| CSS size | ~3KB | No dependencies |
| JS size | ~20 lines | Vanilla, no framework |
| ARIA | aria-expanded, aria-label | Screen reader support |
| Keyboard | Tab + Escape | Escape closes menu |
Add aria-expanded to the hamburger. Screen readers use this to announce "expanded" or "collapsed." Without it, blind users don't know if the menu is open. It's one line of JS that makes the component accessible.
⚠️ Common mistakes
- Using
position: fixedfor the navbar. Overlaps content. Usestickyinstead. - No
max-heighton mobile menu. Long menus get stuck. Addmax-height: calc(100vh - 64px). - Forgetting
overflow-y: auto. Mobile menu can't scroll without it. - No outside-click closing. Menu stays open forever after opening. Add the 5-line JS listener.
- Missing
aria-expanded. Screen readers don't know the menu state. - Hover dropdowns on mobile. They get stuck. Switch to click behavior at 768px.
- Not closing on link click. User navigates and the menu is still open.
- Dropdown z-index too low. Menu appears under other content. Add
z-index: 20or higher.
❓ Frequently asked questions
How do I make a responsive navbar?
Use a flex container for desktop with logo and nav links. Below 768px, hide the links and show a hamburger button. When the hamburger is clicked, toggle a class that displays the mobile menu as a vertical list.
Do I need JavaScript for a mobile menu?
Pure CSS can toggle a mobile menu using the :target or :checked pseudo-classes. But JavaScript is more reliable for accessibility, focus management, and closing when clicking outside. Use JS for anything user-facing.
How do I add a dropdown to the navbar?
Wrap the trigger and menu in a relative container. Position the menu absolutely below the trigger. Use :hover (desktop) or a click handler (mobile) to show the menu. For mobile, the dropdown should expand inline within the mobile menu.
Why is my mobile menu not scrolling?
The mobile menu container needs max-height (like calc(100vh - 80px)) and overflow-y: auto. Without these, long menus extend past the viewport and can't be scrolled.
How do I make the navbar sticky?
Add position: sticky and top: 0 to the navbar. Unlike position: fixed, sticky keeps the navbar in the document flow, so it doesn't overlap content. It's the modern standard.
What is the best navbar height?
64-80px for desktop, 56-64px for mobile. Anything taller feels heavy and wastes screen space. Anything shorter makes touch targets hard to hit on mobile.
How do I close the menu when clicking outside?
Add a document-level click listener that checks if the click was inside the menu. If not, remove the active class. Also listen for Escape key to close on keyboard.
Should the navbar be different on scroll?
Many modern sites add a background blur or shadow when scrolling. This is optional but improves readability when the navbar overlaps content. Implement with a scroll listener and a class toggle.