Skip to main content

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.

Three DivsBest

Category:icon
Pure CSS:Yes
Animated:No
Browser Support:100%

Pseudo-elements

Category:icon
Pure CSS:Yes
Animated:No
Browser Support:99%+

SVG

Category:icon
Pure CSS:No
Animated:No
Browser Support:99%+

X AnimationBest

Category:animation
Pure CSS:Yes
Animated:Yes
Browser Support:99%+

Rotate

Category:animation
Pure CSS:Yes
Animated:Yes
Browser Support:99%+

CheckboxBest

Category:toggle
Pure CSS:Yes
Animated:Yes
Browser Support:100%

Focus-within

Category:toggle
Pure CSS:Yes
Animated:Yes
Browser Support:95%+

Target

Category:toggle
Pure CSS:Yes
Animated:Yes
Browser Support:100%

Full-screenBest

Category:layout
Pure CSS:No
Animated:Yes
Browser Support:99%+

Slide SideBest

Category:layout
Pure CSS:No
Animated:Yes
Browser Support:99%+

Slide Down

Category:layout
Pure CSS:No
Animated:Yes
Browser Support:99%+

Complete NavBest

Category:complete
Pure CSS:No
Animated:Yes
Browser Support:99%+

Icon Creation

Different ways to build the hamburger icon

1

Three Divs/Spans Icon

Recommended

The classic approach using three separate span or div elements. Easy to understand and customize, perfect for beginners.

Live Preview

Pure CSSStatic

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
2

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

Pure CSSStatic

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
3

SVG Hamburger Icon

Uses an inline SVG for the hamburger icon. Scalable, customizable with CSS, and perfect for icon systems.

Live Preview

Needs JSStatic

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

4

Transform to X Animation

Recommended

Animates the hamburger icon into an X (close) icon using CSS transforms. The most common hamburger animation pattern.

Live PreviewClick to toggle

Pure CSSAnimated

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)
5

Rotate Animation

A more subtle animation that rotates the entire icon or individual lines. Creates a unique visual effect.

Live PreviewClick to toggle

Pure CSSAnimated

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

6

Checkbox Hack (Pure CSS)

Recommended

Uses a hidden checkbox input to toggle the menu state without JavaScript. The classic CSS-only solution.

Live PreviewClick to toggle

Pure CSSAnimated

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
7

: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

Pure CSSAnimated

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
8

:target Approach

Uses URL hash and the :target pseudo-class to show/hide the menu. Works with back button navigation.

Live PreviewClick to toggle

Pure CSSAnimated

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

9

Full-screen Overlay Menu

Recommended

A dramatic full-screen overlay that covers the entire viewport. Great for mobile-first designs with immersive navigation.

Live PreviewClick to toggle

Needs JSAnimated

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
10

Slide-in from Side

Recommended

A 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

Home
About
Contact
Needs JSAnimated

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
11

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

Logo
Home
About
Contact
Needs JSAnimated

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

12

Complete Mobile Navigation

Recommended

A production-ready mobile navigation with hamburger icon, slide-in menu, backdrop, and proper accessibility features.

Live PreviewClick to toggle

Brand
Home
Products
About
Contact
Needs JSAnimated

HTML + CSS Code

Pros

  • +Production-ready code
  • +Proper accessibility (ARIA)
  • +Responsive breakpoint included
  • +Click outside to close
  • +Prevents body scroll when open
  • +Smooth animations

Cons

  • -Requires JavaScript
  • -More code to maintain
  • -May need customization

When to Use

  • *Real-world projects
  • *When accessibility matters
  • *E-commerce and business sites
  • *Any professional website

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

What is the easiest way to create a hamburger menu?

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.

How do I animate a hamburger icon to an X?

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

Can I create a hamburger menu without JavaScript?

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

Should I use a button or a div for the hamburger icon?

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.

What's the best hamburger menu layout for mobile?

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.

How do I prevent body scroll when the menu is open?

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.

What size should a hamburger menu button be?

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.