CSS Snippets

Copy. Paste. Ship.

Home › Code Snippets › CSS › Navbar

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

How the navbar works

1. Sticky positioning, not fixed

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.

2. Flex layout for desktop

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.

3. The mobile breakpoint swaps layout

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.

4. The mobile menu needs a scroll container

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.

5. Dropdowns: hover on desktop, click on mobile

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.

6. Outside-click closing

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.

Menu doesn't scroll on mobile

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.

Menu stays open after clicking a link

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.

Sticky navbar overlaps content

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.

Dropdown menu doesn't close on touch

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.

No keyboard support

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;
}
No aria-expanded state

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);
Z-index conflicts

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.

Hamburger icon doesn't change to X

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~3KBNo dependencies
JS size~20 linesVanilla, no framework
ARIAaria-expanded, aria-labelScreen reader support
KeyboardTab + EscapeEscape 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

❓ 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.

🔗 Related snippets

🌐 External resources

← All CSS Snippets