Last updated: March 13, 2026
How to Create a CSS Hamburger Menu
The complete guide to building hamburger menus with CSS. Learn icon creation, animations, pure CSS toggles, and mobile navigation layouts with interactive examples.
What You'll Learn
The hamburger menu is the standard pattern for mobile navigation. This guide covers everything from creating the iconic three-line icon to building complete slide-in navigation systems with smooth animations.
Icon Creation
Three divs, pseudo-elements, or SVG - pick your approach for the hamburger icon.
Animations
Smooth transform animations to morph the hamburger into an X close icon.
Toggle Methods
CSS-only checkbox hack, :focus-within, :target, or JavaScript toggle.
Pro tip: The checkbox hack provides a JavaScript-free solution, but for production sites with accessibility requirements, JavaScript gives you more control.
Quick Reference
Choose the right approach for your project based on these key factors.
| Method | Category | Pure CSS? | Animated? | Browser Support |
|---|---|---|---|---|
| Three DivsBest | icon | Yes | No | 100% |
| Pseudo-elements | icon | Yes | No | 99%+ |
| SVG | icon | No | No | 99%+ |
| X AnimationBest | animation | Yes | Yes | 99%+ |
| Rotate | animation | Yes | Yes | 99%+ |
| CheckboxBest | toggle | Yes | Yes | 100% |
| Focus-within | toggle | Yes | Yes | 95%+ |
| Target | toggle | Yes | Yes | 100% |
| Full-screenBest | layout | No | Yes | 99%+ |
| Slide SideBest | layout | No | Yes | 99%+ |
| Slide Down | layout | No | Yes | 99%+ |
| Complete NavBest | complete | No | Yes | 99%+ |
Three DivsBest
Pseudo-elements
SVG
X AnimationBest
Rotate
CheckboxBest
Focus-within
Target
Full-screenBest
Slide SideBest
Slide Down
Complete NavBest
Icon Creation
Different ways to build the hamburger icon
Three Divs/Spans Icon
RecommendedThe classic approach using three separate span or div elements. Easy to understand and customize, perfect for beginners.
Live Preview
HTML + CSS Code
Pros
- +Easy to understand and modify
- +Full control over each line
- +Works in all browsers
- +Simple to animate individually
- +Accessible with proper button element
Cons
- -More HTML markup required
- -Slightly more CSS to write
- -Need to manage three separate elements
When to Use
- *When learning hamburger menu basics
- *When you need individual line control
- *For complex line animations
- *When browser support is critical
Single Element with Pseudo-elements
Creates the hamburger icon using just one element with ::before and ::after pseudo-elements. Cleaner HTML but requires understanding of CSS positioning.
Live Preview
HTML + CSS Code
Pros
- +Minimal HTML - just one element
- +Cleaner markup
- +Easy to toggle with single class
- +Common pattern in CSS libraries
Cons
- -Requires understanding of pseudo-elements
- -Positioning can be tricky
- -Middle line needs special handling for X animation
- -Slightly more complex CSS
When to Use
- *When you want minimal HTML
- *For component-based frameworks
- *When following DRY principles
- *For simple hover effects
SVG Hamburger Icon
Uses an inline SVG for the hamburger icon. Scalable, customizable with CSS, and perfect for icon systems.
Live Preview
HTML + CSS Code
Pros
- +Infinitely scalable without blur
- +Easy color control with currentColor
- +Works with icon systems
- +Can animate individual paths
- +Best for high-DPI displays
Cons
- -More verbose HTML
- -Requires SVG knowledge for customization
- -Slightly larger file size inline
- -Animation requires SVG animation knowledge
When to Use
- *When using an icon system
- *For high-resolution displays
- *When you need precise control over paths
- *For complex icon animations
Icon Animation
Animating the icon on toggle
Transform to X Animation
RecommendedAnimates the hamburger icon into an X (close) icon using CSS transforms. The most common hamburger animation pattern.
Live PreviewClick to toggle
HTML + CSS Code
Pros
- +Smooth, professional animation
- +Clear visual feedback for state
- +Easy to understand code
- +Works with all icon methods
- +Universally recognized pattern
Cons
- -Requires JavaScript for toggle (or checkbox hack)
- -Transform values depend on line spacing
- -May need adjustment for different sizes
When to Use
- *Most mobile navigation implementations
- *When you want clear open/close states
- *For professional, polished UIs
- *When accessibility matters (clear state indication)
Rotate Animation
A more subtle animation that rotates the entire icon or individual lines. Creates a unique visual effect.
Live PreviewClick to toggle
HTML + CSS Code
Pros
- +Unique visual effect
- +Adds personality to the UI
- +Smooth combined animation
- +Works well with playful designs
Cons
- -More complex CSS
- -May be too flashy for some designs
- -Requires more testing across browsers
- -Animation timing is crucial
When to Use
- *Creative or playful websites
- *When you want a unique animation
- *Portfolio sites or personal projects
- *When the default X feels too basic
Toggle Methods
How to show/hide the menu
Checkbox Hack (Pure CSS)
RecommendedUses a hidden checkbox input to toggle the menu state without JavaScript. The classic CSS-only solution.
Live PreviewClick to toggle
HTML + CSS Code
Pros
- +No JavaScript required
- +Works with JS disabled
- +Maintains state on page load
- +Keyboard accessible (space/enter)
- +Perfect for static sites
Cons
- -Requires specific HTML structure
- -Checkbox must be sibling of menu
- -Can't close by clicking outside
- -May have accessibility concerns
When to Use
- *Static sites without JavaScript
- *When JS-free solution is required
- *Simple dropdown menus
- *Learning CSS-only techniques
:focus-within Approach
Uses the :focus-within pseudo-class to show the menu when any child element is focused. Great for keyboard accessibility.
Live PreviewClick to toggle
HTML + CSS Code
Pros
- +Excellent keyboard accessibility
- +No JavaScript needed
- +Semantic button element
- +Natural focus management
- +Works with screen readers
Cons
- -Closes when clicking outside (may be unexpected)
- -Not as common pattern
- -Browser support slightly lower
- -Behavior can be confusing
When to Use
- *When keyboard navigation is priority
- *Accessible dropdown menus
- *When click-outside-to-close is desired
- *Focus-based interaction patterns
:target Approach
Uses URL hash and the :target pseudo-class to show/hide the menu. Works with back button navigation.
Live PreviewClick to toggle
HTML + CSS Code
Pros
- +Works with browser back button
- +No JavaScript required
- +Shareable URL states
- +100% browser support
- +Good for bookmarking open state
Cons
- -Changes URL (may be unwanted)
- -Requires close button or link
- -Affects browser history
- -Not ideal for SPAs
When to Use
- *Simple static websites
- *When URL state is acceptable
- *Learning CSS-only techniques
- *When back button close is desired
Menu Layouts
Where and how the menu appears
Full-screen Overlay Menu
RecommendedA dramatic full-screen overlay that covers the entire viewport. Great for mobile-first designs with immersive navigation.
Live PreviewClick to toggle
HTML + CSS Code
Pros
- +Maximum visual impact
- +Full focus on navigation
- +Works great on mobile
- +Room for large touch targets
- +Can include additional content
Cons
- -Covers all page content
- -Requires JavaScript for toggle
- -May feel heavy for simple sites
- -Need to manage body scroll
When to Use
- *Mobile-first designs
- *When navigation is a key feature
- *Creative or portfolio sites
- *When menu has many items
Slide-in from Side
RecommendedA drawer-style menu that slides in from the left or right side of the screen. The most common mobile navigation pattern.
Live PreviewClick to toggle
HTML + CSS Code
Pros
- +Familiar pattern to users
- +Page content partially visible
- +Click outside to close
- +Room for nested menus
- +Great for apps
Cons
- -Requires JavaScript
- -Need to handle scroll lock
- -More complex z-index management
- -May need touch gestures for mobile
When to Use
- *App-like websites
- *When menu has nested items
- *E-commerce sites
- *Dashboard navigation
Slide-down from Top
The menu slides down from the header area, pushing content down or overlaying it. Natural extension of the header.
Live PreviewClick to toggle
HTML + CSS Code
Pros
- +Natural extension of header
- +Simple implementation
- +Content flow is preserved
- +Works well with sticky headers
- +Familiar accordion pattern
Cons
- -Pushes content down (can be jarring)
- -Limited space for menu items
- -max-height needs adjustment
- -Animation can feel less smooth
When to Use
- *Simple websites with few nav items
- *When sidebar isn't appropriate
- *Content-focused sites
- *When you want minimal disruption
Complete Examples
Production-ready implementations
Pro Tips for Hamburger Menus
1. Always Use a Button Element
For accessibility, wrap your hamburger icon in a button element with aria-label. This ensures keyboard users and screen readers can interact with your menu.
<button class="hamburger"
aria-label="Menu"
aria-expanded="false">
<!-- icon here -->
</button>2. Use Transform for Animations
Transform and opacity are GPU-accelerated, making them perfect for smooth hamburger animations. Avoid animating width or height.
.line {
transition: transform 0.3s ease,
opacity 0.3s ease;
/* GPU accelerated! */
}3. Calculate Transform Values
For the X animation, translateY should equal the gap plus half the line height. With 5px gap and 3px lines: translateY(8px).
/* gap: 5px, line-height: 3px */ /* translate = gap + line-height */ transform: translateY(8px) rotate(45deg);
4. Prevent Body Scroll
When the menu is open, prevent the page from scrolling behind the overlay by adding overflow: hidden to the body.
body.menu-open {
overflow: hidden;
}
/* Remove when closing */Frequently Asked Questions
The easiest way is using three span elements inside a button, styled with flexbox. Each span becomes a line of the hamburger icon. For the menu toggle, the checkbox hack provides a CSS-only solution, or you can use a simple JavaScript onclick to toggle a class.
Use CSS transforms on the three lines: hide the middle line with opacity: 0, then rotate the top line 45 degrees and translate it down, and rotate the bottom line -45 degrees and translate it up. The transform values depend on your line spacing (typically 8px for a 5px gap).
Yes! The checkbox hack is the most reliable CSS-only method. Use a hidden checkbox input, a label as the hamburger button, and the :checked pseudo-class to show/hide the menu. Other options include :target (uses URL hash) and :focus-within (focus-based).
Always use a button element for accessibility. Buttons are keyboard focusable, respond to Enter/Space keys, and are announced properly by screen readers. Add aria-label='Toggle menu' and aria-expanded to indicate state.
The slide-in sidebar is the most popular choice - it's familiar to users from apps, allows click-outside-to-close, and preserves some page context. Full-screen overlays work great for minimal menus, while slide-down works for simple sites with few nav items.
Add overflow: hidden to the body when the menu opens. In JavaScript: document.body.classList.add('nav-open'), then in CSS: body.nav-open { overflow: hidden; }. Remember to remove the class when closing the menu.
The minimum touch target size is 44x44 pixels according to WCAG guidelines. The icon itself can be smaller (24px is common), but the clickable area should be at least 44px. Use padding to increase the touch target if needed.
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
Responsive Navbar
Mobile-friendly navigation
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
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
