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.
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
forattribute on the label must match the checkbox'sid - The checkbox must come before elements it will control (for the sibling selector to work)
- Three
<span>elements create the hamburger lines
Base CSS (Desktop)
Set up the basic navbar layout using Flexbox. This creates a horizontal navigation for desktop screens.
Key Concepts
display: flexwithjustify-content: space-betweenalign-items: centerfor vertical centeringgapfor spacing between nav links
Best Practices
- Use
remunits for scalability - Add transitions for smooth hover effects
- Remove default list styles
Hamburger Icon Styles
Hide the checkbox and hamburger on desktop. Style the hamburger icon with three horizontal lines.
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.
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
peerandpeer-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
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.
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.
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.
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.
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.
More CSS Tutorials
Copy CSS from Website
Extract styles with 2 clicks
Center a Div (CSS)
Flexbox, Grid, and more
Center a Div (Tailwind)
Utility classes for centering
Convert CSS to Tailwind
CSS to utility classes
Glassmorphism Effect
Frosted glass UI style
Neumorphism Button
Soft UI button design
Sticky Header
Fixed navigation on scroll
CSS Pagination
Page navigation styles
CSS Accordion
Expandable content sections
Dropdown Menu
Hover and click dropdowns
CSS Modal
Popup dialog boxes
Toast Notifications
Animated alert messages
Hamburger Menu
Animated menu icon
Animate Gradients
Moving gradient backgrounds
Skeleton Loader
Loading placeholder UI
Element Screenshot
Capture any element as image
Pick Color from Website
Eyedropper tool comparison
Identify Fonts
Find fonts on any website
Download All Images
Bulk save images from any site
Measure Elements
Page ruler and measurements
Extract Colors from Website
Get any color palette instantly
Dark Mode Toggle
CSS variables and localStorage
Responsive Grid
CSS Grid auto-fit and minmax
Center Text in CSS
text-align, Flexbox, and Grid
Make Text Bold
font-weight values explained
Add Shadow in CSS
box-shadow, text-shadow, drop-shadow
Round Corners in CSS
border-radius and pill shapes
Style File Input
Custom upload buttons
