TechBuzzPro

Bootstrap Hamburger Menu Examples: 7 Free Patterns

· 6 min read · Apps & Browsers

In this article
  1. How Bootstrap's Hamburger Menu Works
  2. 1. Basic Off-Canvas Slide-In
  3. 2. Full-Screen Overlay Menu
  4. 3. Slide-Out with Backdrop
  5. 4. Hamburger with Animated Icon
  6. 5. Hamburger Menu with Dropdowns
  7. 6. Hamburger Menu with Search Bar
  8. 7. Hamburger Menu with User Account Options
  9. How to Choose the Right Pattern
  10. Common Problems and How to Fix Them
  11. Frequently Asked Questions

This article presents seven free Bootstrap hamburger menu patterns you can adapt for your own projects, ranging from a basic off-canvas slide-in to a full-screen overlay. Each pattern includes the HTML, CSS, and JavaScript needed to implement it, along with notes on when to use it and how to customize it. You'll also learn how these menus work, how to choose the right one, and common pitfalls to avoid.

How Bootstrap's Hamburger Menu Works

Bootstrap's navbar component includes a built-in hamburger toggle that appears on small screens (below the lg breakpoint by default). The toggle button uses the data-bs-toggle="collapse" and data-bs-target="#navbarNav" attributes to control the collapsible <div> that contains the menu links. When clicked, Bootstrap's JavaScript adds the show class to the target, expanding it vertically. The default behavior is a simple drop-down that pushes the content below it down. To create more advanced patterns like off-canvas or overlay menus, you override the default styles and sometimes add a bit of custom JavaScript.

1. Basic Off-Canvas Slide-In

The off-canvas pattern hides the menu off-screen and slides it in from the left or right when the hamburger is clicked. This is a popular choice for mobile apps and sites with many navigation items.

  • HTML: Use a standard navbar with a custom button and a <div> that will act as the off-canvas container.
  • CSS: Position the container fixed, set left: -250px (or right), and use a transition to bring it to left: 0 when a class like open is added.
  • JavaScript: Toggle the open class on the container when the button is clicked. You can also add an overlay that closes the menu when clicked.

2. Full-Screen Overlay Menu

This pattern covers the entire screen with the navigation menu, often with large, centered links and a close button. It's a bold choice that works well for portfolios or creative sites.

  • HTML: A full-width fixed container that is hidden by default (using visibility: hidden and opacity: 0).
  • CSS: When the open class is added, set visibility: visible and opacity: 1. Add a transition for a smooth fade-in effect.
  • JavaScript: Toggle the open class on the overlay. Ensure the hamburger button is hidden when the overlay is open, and show a close button instead.

3. Slide-Out with Backdrop

This is a variation of the off-canvas pattern that includes a semi-transparent backdrop over the page content. The backdrop helps focus attention on the menu and provides a clear way to close it.

  • HTML: Add a <div> for the backdrop, positioned fixed with a dark background and low opacity.
  • CSS: The backdrop is hidden by default and fades in when the menu opens. The menu itself slides in from the side, overlapping the backdrop.
  • JavaScript: Toggle both the menu and backdrop classes. Clicking the backdrop should close the menu.

4. Hamburger with Animated Icon

Instead of the default Bootstrap hamburger icon (three lines), you can animate the icon to transform into an "X" when the menu is open. This is a small but polished touch that improves user feedback.

  • HTML: Replace the default <span class="navbar-toggler-icon"> with three custom <span> elements.
  • CSS: Style the spans as lines, and when the button has the open class, rotate and translate them to form an X.
  • JavaScript: Toggle the open class on the button when clicked.

5. Hamburger Menu with Dropdowns

This pattern combines the hamburger with Bootstrap's dropdown component, allowing you to have sub-menus within the mobile menu. It's useful for sites with a complex hierarchy.

  • HTML: Use the standard navbar with a collapse, and inside it place .nav-item.dropdown elements.
  • CSS: Ensure the dropdown menu is styled to fit within the off-canvas or overlay container, often with a slightly indented style.
  • JavaScript: Bootstrap's built-in dropdown functionality works, but you may need to adjust the click behavior to prevent the menu from closing when a dropdown is opened.

Integrating a search bar into the hamburger menu is a common requirement. This pattern places a search input at the top or bottom of the menu, making it easy for users to find content.

  • HTML: Add a <form> with an input and a submit button inside the collapsible area.
  • CSS: Style the search bar to match the menu's design, often full-width with a margin.
  • JavaScript: No extra JavaScript is needed for the search itself; the menu's open/close behavior remains the same.

7. Hamburger Menu with User Account Options

For e-commerce or membership sites, the hamburger menu often includes login/logout links, a user avatar, or a mini profile section. This pattern personalizes the menu and gives quick access to account features.

  • HTML: Include an avatar image or icon, the user's name, and links to their profile, settings, and logout.
  • CSS: Style the account section to stand out, perhaps with a subtle background or border.
  • JavaScript: No extra JavaScript is required beyond the menu toggle.

How to Choose the Right Pattern

Selecting the right hamburger menu pattern depends on your site's content and user needs. If you have a simple site with a few links, a basic collapse is sufficient. For a site with many links or a desire for a more immersive mobile experience, consider an off-canvas or overlay menu. If your users frequently search, include a search bar. Always test the menu on real devices to ensure it's easy to use and doesn't feel clunky.

Common Problems and How to Fix Them

Here are some frequent issues when implementing custom hamburger menus:

  • Menu doesn't close when a link is clicked: Add a click event listener to each link that removes the open class.
  • Page scrolls behind the menu: Add overflow: hidden to the <body> when the menu is open.
  • Dropdowns don't work inside the off-canvas: Ensure the off-canvas container has a high enough z-index and that Bootstrap's JavaScript is loaded after the custom script.
  • Hamburger icon doesn't animate: Check that your CSS transitions are applied to the right elements and that the open class is toggled correctly.

Frequently Asked Questions

Do I need Bootstrap's JavaScript for these patterns?

Yes, Bootstrap's JavaScript is required for the basic collapse functionality. For custom off-canvas or overlay patterns, you'll also need a small custom script to toggle the open class.

Can I use these patterns with Bootstrap 5?

Yes, these patterns work with Bootstrap 5. The main differences from Bootstrap 4 are the use of data-bs-* attributes and the removal of jQuery as a dependency.

How do I make the hamburger menu appear on desktop?

By default, the hamburger is hidden on large screens. To force it to show, you can change the navbar's navbar-expand-lg class to navbar-expand-sm (or navbar-expand-md), or remove it entirely to always show the hamburger.

What is the best way to style the hamburger icon?

You can use the default Bootstrap icon, or create a custom one with CSS or an SVG. For animation, it's easiest to style three spans and toggle a class.

Are these patterns accessible?

Bootstrap's navbar is generally accessible, but you should ensure your custom patterns include aria-expanded and aria-controls attributes on the toggle button, and use semantic HTML for navigation.

How do I make the menu slide from the right?

Simply change the CSS positioning from left to right in the off-canvas pattern. For example, set right: -250px and transition to right: 0.