Skip to main content

Last updated: March 13, 2026

How to Create a Responsive Navbar with CSS

Build a mobile-friendly navigation bar with a hamburger menu using pure CSS - no JavaScript required! Learn the checkbox hack technique.

What We'll Build

A fully responsive navbar that transforms into a hamburger menu on mobile devices.

Switch to mobile view to see the hamburger menu

The CSS Checkbox Hack Explained

The key to building a JavaScript-free responsive navbar is the checkbox hack. We use a hidden checkbox input and the :checked CSS pseudo-class to toggle visibility of the mobile menu.

1. Hidden Checkbox

A checkbox input is placed in the HTML but hidden with CSS. It maintains state (checked/unchecked) without JavaScript.

2. Label as Trigger

A label element is styled as the hamburger icon. Clicking it toggles the checkbox because of the for attribute.

3. Sibling Selector

Using the ~ sibling selector, we target elements after the checkbox and change their styles when checked.

1

HTML Structure

Start with the semantic HTML structure. The key elements are: the hidden checkbox, the hamburger label, and the navigation links.

Important Notes:

  • The for attribute on the label must match the checkbox's id
  • The checkbox must come before elements it will control (for the sibling selector to work)
  • Three <span> elements create the hamburger lines
2

Base CSS (Desktop)

Set up the basic navbar layout using Flexbox. This creates a horizontal navigation for desktop screens.

Key Concepts

  • display: flex with justify-content: space-between
  • align-items: center for vertical centering
  • gap for spacing between nav links

Best Practices

  • Use rem units for scalability
  • Add transitions for smooth hover effects
  • Remove default list styles
3

Hamburger Icon Styles

Hide the checkbox and hamburger on desktop. Style the hamburger icon with three horizontal lines.

4

Mobile Styles (Media Query)

Use a media query to show the hamburger and transform the nav links into a slide-out menu on mobile.

The Magic Line

.nav-toggle:checked ~ .nav-links

This selector targets .nav-links when it's a sibling of a checked .nav-toggle. The ~ is the general sibling combinator.

5

Hamburger Animation

Animate the hamburger icon to form an X when clicked. This provides visual feedback that the menu is open.

Line 1: Rotate 45deg + move down

Line 2: Fade out (opacity: 0)

Line 3: Rotate -45deg + move up

Complete Code

Here's the complete HTML and CSS code combined. Copy this and you're ready to go!

HTML

CSS

Tailwind CSS Version

If you prefer Tailwind CSS, here's the same navbar using utility classes. Note that the checkbox hack works slightly differently in Tailwind due to the peer modifier limitations.

Tailwind Tips

  • Use peer and peer-checked: modifiers for the checkbox hack
  • The checkbox must be a sibling (not nested) for peer to work
  • For complex navbars, consider using JavaScript with Tailwind for better control

Pro Tips

1. Add a backdrop overlay

Add a semi-transparent overlay behind the mobile menu to focus attention and allow closing by clicking outside.

.nav-toggle:checked ~ .backdrop {
  display: block;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
}

2. Accessibility matters

Add aria labels and ensure keyboard navigation works properly for screen readers.

<label
  for="nav-toggle"
  class="hamburger"
  aria-label="Toggle navigation"
  role="button"
>
  ...
</label>

3. Prevent body scroll

When the mobile menu is open, you may want to prevent the body from scrolling. This requires a small JavaScript addition.

// Optional: prevent scroll
checkbox.addEventListener('change', () => {
  document.body.style.overflow =
    checkbox.checked ? 'hidden' : '';
});

4. Consider sticky positioning

For a sticky navbar that stays visible while scrolling, use position: sticky with a z-index.

.navbar {
  position: sticky;
  top: 0;
  z-index: 1000;
}

Frequently Asked Questions

How do I make a navbar without JavaScript?

You can create a fully functional responsive navbar using the CSS checkbox hack. By using a hidden checkbox input and the :checked pseudo-class, you can toggle the mobile menu visibility without any JavaScript. The checkbox is visually hidden but clickable through a label element styled as a hamburger icon.

What's the best breakpoint for mobile navbar?

The most common breakpoint for switching to a mobile navbar is 768px (tablet width). However, the best breakpoint depends on your navbar content. If you have many links, you might need to switch at 1024px. A good rule is to switch when your links start to feel cramped or overflow. Test your navbar at various widths to find the right breakpoint.

How do I animate the hamburger icon?

The hamburger icon animation is achieved by targeting the three span elements inside the hamburger when the checkbox is checked. The top line rotates 45 degrees and moves down, the middle line fades out (opacity: 0), and the bottom line rotates -45 degrees and moves up. Using CSS transform and transition properties creates a smooth X animation.

Should I use position fixed or sticky for navbar?

Use position: fixed if you want the navbar to always stay at the top of the viewport, even when scrolling. Use position: sticky if you want the navbar to scroll with the page initially but stick to the top once it reaches the viewport edge. Fixed is more common for main navigation, while sticky is useful for secondary headers or in-page navigation.

How do I add a dropdown menu?

For CSS-only dropdowns, nest a ul inside the li that should have a dropdown. Hide it with opacity: 0 and visibility: hidden, then show it on :hover or :focus-within. For mobile, you can use another checkbox hack or simply show all items in a flat list. For complex dropdowns with many levels, JavaScript provides better accessibility and control.

Explore More Navbar Styles

Browse our collection of ready-to-use navbar designs for inspiration.