Hamburger Menu CSS (CSS-Only) 10+ Examples + Code

Luke Embrey Avatar

Follow on Twitter

On today’s menu are CSS hamburgers. A responsive way to display an off-canvas menu using only HTML and CSS.

A CSS hamburger menu is a great way to improve navigation without cluttering a screen. Think of it as the epitome of responsive design but with endless possibilities.

Keep reading to discover hamburger menu examples you can create using CSS, including slide-outs, screen overlays, and cool animations. Let’s get started!

How do you make a hamburger menu in CSS?

You can make a hamburger menu in CSS by toggling a hidden checkbox and using the :checked selector to show and hide an off-canvas navigation panel.

  • Add the HTML structure (toggle + nav).
  • Style the hamburger icon (three bars).
  • Hide the menu by default.
  • Reveal it on toggle (:checked).
  • Animate and add reduced

    4. Full-screen Hamburger Menu

    Considering opening the menu element in full-screen? Then you’ll love this example. A CSS-only solution to display a burger menu and open it on a full-screen layer.

    This kind of full-screen menu can come in handy when our menu has many items, sub-menus, or extra information.

    -motion support.

Copy the HTML/CSS snippet and swap in your nav links.

Step 1, Add the HTML (copy-paste)

<header class="site-header">
  <a class="brand" href="#">Brand</a>

  <label class="hamburger" for="nav-toggle">
    <span class="sr-only">Open menu</span>
    <span class="bar"></span>
    <span class="bar"></span>
    <span class="bar"></span>
  </label>

  <input class="nav-toggle" id="nav-toggle" type="checkbox" />

  <div class="overlay"></div>

  <nav class="offcanvas" aria-label="Primary">
    <ul>
      <li><a href="#">Home</a></li>
      <li><a href="#">About</a></li>
      <li><a href="#">Contact</a></li>
    </ul>
  </nav>
</header>Code language: HTML, XML (xml)

Step 2, Add the base CSS (layout + hidden menu)

:root{
  --header-h: 64px;
  --panel-w: min(80vw, 320px);
  --bar-w: 28px;
  --bar-h: 3px;
  --bar-gap: 6px;
  --dur: .25s;
  --bg: #111;
  --fg: #fff;
}

*{ box-sizing: border-box; }
body{ margin:0; font-family: system-ui, -apple-system, Segoe UI, Roboto, Arial, sans-serif; }

.sr-only{
  position:absolute; width:1px; height:1px; padding:0; margin:-1px;
  overflow:hidden; clip:rect(0,0,0,0); white-space:nowrap; border:0;
}

.site-header{
  position: relative;
  height: var(--header-h);
  padding: 0 16px;
  display:flex;
  align-items:center;
  justify-content: space-between;
  background: var(--bg);
  color: var(--fg);
}

.brand{ color: var(--fg); text-decoration: none; font-weight: 700; }

.nav-toggle{
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.offcanvas{
  position: fixed;
  top: 0;
  left: 0;
  width: var(--panel-w);
  height: 100dvh;
  padding: 80px 16px 16px;
  background: #1b1b1b;
  transform: translateX(-105%);
  transition: transform var(--dur) ease;
  z-index: 1001;
}

.offcanvas ul{ list-style:none; padding:0; margin:0; display:grid; gap: 12px; }
.offcanvas a{ color: var(--fg); text-decoration:none; font-size: 18px; }

.overlay{
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.45);
  opacity: 0;
  visibility: hidden;
  transition: opacity var(--dur) ease, visibility 0s linear var(--dur);
  z-index: 1000;
}Code language: CSS (css)

Step 3, Add the toggle CSS (checked selector)

.nav-toggle:checked ~ .offcanvas{
  transform: translateX(0);
}

.nav-toggle:checked ~ .overlay{
  opacity: 1;
  visibility: visible;
  transition: opacity var(--dur) ease;
}

/* Click overlay to close, by associating it to the checkbox */
.overlay{ pointer-events: none; }
.nav-toggle:checked ~ .overlay{ pointer-events: auto; }Code language: CSS (css)

Step 4, Add focus styles and keyboard support

Accessibility note: the checkbox plus label pattern is CSS-only, but it is not as strong as a real <button> that can update aria-expanded. Add visible focus styles and consider a “Menu” label.

.hamburger{
  width: 44px;
  height: 44px;
  display: grid;
  place-content: center;
  gap: var(--bar-gap);
  cursor: pointer;
  border-radius: 10px;
}

.hamburger:focus-visible{
  outline: 2px solid #7dd3fc;
  outline-offset: 2px;
}

/* Basic 3-bar icon */
.bar{
  width: var(--bar-w);
  height: var(--bar-h);
  background: var(--fg);
  border-radius: 999px;
  transition: transform var(--dur) ease, opacity var(--dur) ease;
}Code language: CSS (css)

How do you animate a CSS hamburger menu into an X?

Rotate the top and bottom bars and fade the middle bar using transform, opacity, and transition.

/* Turn hamburger into X when open */
.nav-toggle:checked ~ .hamburger .bar:nth-child(2){ opacity: 0; }
.nav-toggle:checked ~ .hamburger .bar:nth-child(1){
  transform: translateY(calc(var(--bar-gap) + var(--bar-h))) rotate(45deg);
}
.nav-toggle:checked ~ .hamburger .bar:nth-child(3){
  transform: translateY(calc(-1 * (var(--bar-gap) + var(--bar-h)))) rotate(-45deg);
}

/* Reduced motion */
@media (prefers-reduced-motion: reduce){
  .offcanvas, .overlay, .bar{ transition: none !important; }
}Code language: CSS (css)

What Is A Hamburger Menu?

A hamburger menu is a three-line button that toggles a hidden navigation panel, often on small screens. In CSS, it usually means styling the icon and controlling the open state with :checked, a class, or an attribute like aria-expanded.

What is a Hamburger Menu?

What are the pros and cons of a hamburger menu?

Pros

  • Reduces visual clutter. It condenses navigation options and keeps small headers readable.
  • Works well for responsive layouts. A hamburger button helps when your nav does not fit on mobile widths.
  • Keeps focus on content. Fewer visible links can reduce distraction on content-heavy pages.
  • Familiar pattern. Many users recognize it, especially in mobile apps.

Cons

  • Reduced discoverability. Add a “Menu” label or show key links on desktop.
  • Hidden options. Keep top tasks visible outside the menu.
  • Extra interaction. Keep animations quick and add a clear close state.

How do you make the hamburger menu responsive?

Show the hamburger on small screens and switch to a horizontal nav when links stop fitting.

/* Example: show off-canvas on small screens, inline nav on larger */
@media (min-width: 900px){
  .hamburger, .nav-toggle, .overlay{ display:none; }
  .offcanvas{
    position: static;
    transform: none;
    height: auto;
    width: auto;
    padding: 0;
    background: transparent;
  }
  .offcanvas ul{ display:flex; gap: 20px; }
}Code language: CSS (css)

Should you use CSS-only or JavaScript for a hamburger menu?

CSS-only menus are quick, but minimal JavaScript is better for accessibility because it can update aria-expanded, close on Escape, and manage focus.

CSS-only checkbox pattern (when it is OK)

Use CSS-only for demos, prototypes, or simple sites. Limitations: no automatic aria-expanded, no easy “close on link click”, and tricky “click outside to close” behavior.

Use a real button and toggle a class. Keep aria-expanded in sync and support Escape to close.

<button class="hamburger" id="menuBtn" aria-controls="siteNav" aria-expanded="false">Menu</button>
<nav id="siteNav" class="offcanvas">...</nav>

<script>
const btn = document.getElementById('menuBtn');
const nav = document.getElementById('siteNav');

btn.addEventListener('click', () => {
  const open = btn.getAttribute('aria-expanded') === 'true';
  btn.setAttribute('aria-expanded', String(!open));
  nav.classList.toggle('is-open', !open);
});

document.addEventListener('keydown', (e) => {
  if (e.key === 'Escape'){
    btn.setAttribute('aria-expanded', 'false');
    nav.classList.remove('is-open');
    btn.focus();
  }
});
</script>Code language: HTML, XML (xml)

Hamburger menu CSS variations (slide-in, dropdown, overlay)

Most hamburger menus share the same toggle. Variations come from positioning and transitions: dropdown, off-canvas, or full-screen overlay.

Off-canvas slide-in

Slide the nav in from a side and add an overlay. Keep the panel above the overlay with z-index.

Best for short navs. Place it under the header and animate max-height or clip-path.

Full-screen overlay

Use for many items. Set the nav to position: fixed with inset: 0 and center links.

What are the most common hamburger menu bugs?

Most issues come from stacking context, scrolling, and focus.

  • Menu appears behind content. Use position: fixed and raise z-index. Parents with transform or filter can trap children.
  • Page scrolls behind the open menu. With JS, toggle body{overflow:hidden}. Pure CSS cannot reliably scroll-lock across browsers.
  • 100vh jumps on mobile. Prefer 100dvh, or use top:0; bottom:0; with position: fixed.
  • Links are not clickable. The overlay may be on top. Keep the panel above the overlay and avoid incorrect pointer-events.

When is a hamburger menu a bad idea?

A hamburger menu is a bad idea when primary navigation is critical to conversion. If you only have a few top-level links, show them inline on desktop and use a hamburger only at small widths.

Hamburger Menu Examples to “Take-Away”

Here are a few examples you can reuse or use for inspiration.

1 Responsive CSS Hamburger Menu – CSS only

It’s quite common to have burger menus to replace standard horizontal menus on small viewports. This way, the menu becomes completely responsive and provides the best experience depending on the viewport size.

If that’s what you are looking for, this example will do exactly that, and with only CSS. To test it out, open the codepen in a new window and resize the result panel.

In responsive mode, the hamburger menu will open the list of items one after the other in a vertical column coming from the top. Quite a standard behavior for mobile devices.

2. Simple Centred Hamburger Menu

This one is very simple but effective; it only uses HTML and CSS to pull off a hamburger menu with some cool animations.

The hamburger icon itself, when clicked, transforms into a cross and works as the close button. The menu slides into view and displays in the centre with its navigation links.

If you like sliding menu designs and cool animations with many options, you may be interested in fullPage.js – A library that allows you to build full-width, full-screen web pages that are scrollable. It is even available for WordPress with Elementor and Gutenberg plugins and a WordPress theme.

3. Left Sliding Responsive Hamburger Menu

If you are looking for a more complete example of how a CSS hamburger menu can be useful, this CodePen renders an example website to showcase the use of the CSS hamburger menu.

It only uses pure HTML and CSS, so it is easy to learn from and understand what is happening. The menu icon is animated and transforms into a cross when the menu is open.

The menu itself slides out from the slide and overlays the main website. As this design is responsive, it will automatically hide the header menu and make the burger menu available once the screen width decreases.

If you are also interested in menus and not only on the hamburger elemenet, check out these examples of great side menus for your webpage!

Alternatives to a Hamburger Menu

If you don’t like the cons of a hamburger menu, here are some alternatives:

  • Tabbed Menus. Display all features and functionalities on the screen. A good example is the Instagram app, which allows you to switch from one page to another with a single click.
  • Labeled Menu Button. Instead of the Hamburger button, simply put a “Menu” label, which is hard to miss. 
  • Floating Menu. It is placed in a fixed position on the screen, and users can access it from any page.
  • Tab Bar With More Options. This is where primary navigation options are displayed on the screen. Users can click “More” or “Options” to unlock hidden features.
  • Swiping Feature. This option allows users to swipe to access different features. It is mainly used in dating and social media sites. For instance, swiping left on TikTok displays the person’s profile. However, this works best when accompanied by screen icons. 
  • Collapsing Menu. As the name suggests, the menu opens and collapses progressively. This technique is common in media websites and blogs.

FAQ

Can you make a hamburger menu with CSS only?

Yes. A CSS-only hamburger menu usually uses a hidden checkbox and a label to toggle menu visibility with the :checked selector. It works without JavaScript, but it cannot update aria-expanded automatically.

What is the best size for a hamburger icon?

A common size is 24 to 32px wide with 2 to 4px bar thickness and 4 to 6px spacing. The tap target should be at least 44 by 44px.

How do I stop the page from scrolling when the menu is open?

With JavaScript, toggle a class on body and use overflow: hidden;. Pure CSS cannot reliably lock body scroll across browsers for all layouts.

Why is my hamburger menu behind other elements?

Usually a z-index or stacking context problem. Give the menu and overlay position: fixed or absolute and a higher z-index. Parents with transform, filter, or opacity can create new stacking contexts.

Takeaway

We’ve seen a lot of different designs for CSS hamburger menus, some traditional, some a little different. Hopefully, you have found something you like from our examples and found inspiration to use one on your next website.

Find the perfect combination for your hamburger menu by using one of these amazing JavaScript menus.

The CSS hamburger menu has a wide range of uses: from responsive design, interactive experiences with floating menu icons, and providing you with more space with an off-canvas menu.

Overall, CSS responsive hamburger menus are a great way to make your website layout responsive and scale down to smaller screens on mobile devices. It is an easy way to make your header navigation section responsive and adaptable to different screen sizes.

More articles which you may find interesting.

Hamburger Menu: Is it Always the “Tastiest” Option?

No, the hamburger menu isn’t always the best option, and it could need some improvements. If the features added to the menu aren’t important to your users, then you don’t need it. But if you’re looking for increased functionality, then yes, it’s a great alternative.

Sites with numerous animations don’t require the Hamburger Menu. Also, there is no point in including it if all the important features will be hidden. However, let’s consider real estate or gaming websites and apps. 

In such cases, this additional navigation helps to prevent visitors from being overwhelmed with numerous options and information. Imagine visiting the Minecraft website on your phone without the hidden menu. This means everything from games to shop, community, support, and accounts is displayed simultaneously, which is chaotic. 

5. Hamburger menu animations

If you are looking for different animations for your hamburger menu icon, you have to take a look at these ones.

It uses a single line of JavaScript (or jQuery) to set the state of the burger. The rest is all pure CSS.

6. Snappy Sliding Hamburger Menu

A very snappy and slick CSS hamburger menu that only uses HTML and CSS to pull this off.

The menu itself quickly slides out from the left and does not take up the whole screen, just the left side. The menu items have a nice hover effect as well. And if this effect is not fancy enough for you, you can create a better hover effect by getting inspiration from these CSS Button hover effects.

Good to work from if you are looking to add this to an existing website or you only want the basic structure.

7. Top Left Animated Hamburger Menu

Most CSS hamburger menus either slide out from the left and right or take up the whole screen. But this one is a bit different because it only uses the top left corner inside a bubble-shaped menu.

Very unique compared to the traditional hamburger menu design, this example could easily be changed to edit the colours or add an effective shadow on the background.

This one does use JavaScript, but it is only very minimal: basically just to toggle the CSS classes to change the menu status, open or closed. Nothing complicated. No libraries or dependencies to rely on, just pure JavaScript that is very basic.

8. Simple Left-Sliding Hamburger Menu Overlay

The animation is smooth and doesn’t feel tacky. The menu slides out from the left and sits on top of any main content behind it.

Doesn’t require any JavaScript, just works purely based on HTML and CSS, easy to work from or adapt to your liking.

The menu itself will be easy to edit and add your own content, simply write your own HTML elements inside, and the menu will slide out.

The hamburger menu icon also has a smooth open and close animation that only uses CSS.

9. Animated Full Screen Hamburger Menu

Featuring a floating CSS hamburger menu icon inside a circular background, once clicked, the menu uses a curricular opening animation.

The animation is very smooth and opens from the point of the menu icon itself.

Taking up the full screen would be great for busy navigation menus that require a lot of space with images, icons, and text.

Only uses pure HTML and CSS to pull this off.

10. Full Screen Morphing Hamburger Menu

A fun animated CSS hamburger menu that morphs outwards from the top right corner of the screen into a full-screen menu.

Only using HTML and CSS, the structure is simple to follow and make edits to add your own content and navigation links/style.

11. Multi-Depth Hamburger Menu

Sliding out from the left side of the screen, this menu design is more suited for complex navigation.

It has a lovely sliding animation, but the menu itself uses a very well-structured item list that can go multiple depths, which is useful for inner categories.

It uses HTML and CSS, which are generated from SCSS.

Was this page helpful?