2 Feb 2023

Building a Hamburger Menu with HTML, CSS, and JavaScript

The hamburger menu is a popular design element used in mobile and responsive web design. It is a button with three horizontal lines that open and close a menu when clicked. The menu typically contains a list of navigation links, and is used to provide a clean and concise way to access site content on smaller screens. In this tutorial, we will go through the steps of building a hamburger menu using HTML, CSS, and JavaScript.

Step 1: HTML Structure

First, let's set up the HTML structure for our menu. We'll create a container element that will hold our button and menu elements, and we'll give it a class of "menu-container".

<div class="menu-container">
  <button class="menu-toggle">
    <span class="menu-toggle-bar"></span>
    <span class="menu-toggle-bar"></span>
    <span class="menu-toggle-bar"></span>
  </button>
  <nav class="menu">
    <ul>
      <li><a href="#">Home</a></li>
      <li><a href="#">About</a></li>
      <li><a href="#">Contact</a></li>
    </ul>
  </nav>
</div>

In this HTML, we have a button element with a class of "menu-toggle" that will act as our hamburger button. Inside the button, we have three span elements, each with a class of "menu-toggle-bar", which will form our hamburger icon. Next, we have a nav element with a class of "menu", which will contain our navigation links. Finally, we have an unordered list (ul) element with a list of list items (li), each containing a link (a) to a different page on our site.

Step 2: CSS Styles

Next, let's style our menu with CSS. We'll start by hiding the menu by default, and then adding styles to our button and menu.

.menu-container {
  position: relative;
}

.menu {
  position: absolute;
  top: 100%;
  right: 0;
  background-color: #fff;
  padding: 20px;
  display: none;
}

.menu-toggle {
  display: block;
  padding: 10px;
  cursor: pointer;
}

.menu-toggle-bar {
  height: 3px;
  width: 20px;
  background-color: #000;
  margin: 6px 0;
  transition: all 0.3s ease;
  display: block;
}

In this CSS, we have set the position of the "menu-container" to "relative" so that we can position the "menu" element absolutely within it. The "menu" element is positioned at the top of the "menu-container" with a top of 100% and a right of 0. We have also set its background color to white and added some padding for styling. The "menu" element is hidden by default with the "display: none" property.

The "menu-toggle" button has a display of "block", with padding and a cursor of "pointer" for styling purposes. The "menu-toggle-bar" elements are styled with a height of 3px, width of 20px, background color of black, and a margin of 6px 0. We have also added a transition of 0.3 seconds for ease of animation when opening and closing the menu.

Step 3: JavaScript Functionality

Finally, let's add the JavaScript to open and close our JavaScript functionality to open and close our menu. We'll start by adding an event listener to the "menu-toggle" button that listens for a click and toggles the "menu" element.

const toggleBtn = document.querySelector('.menu-toggle');
const menu = document.querySelector('.menu');

toggleBtn.addEventListener('click', () => {
  menu.classList.toggle('active');
});

In this JavaScript, we are using the querySelector method to select the "menu-toggle" button and "menu" element and store them in variables. We then add an event listener to the "menu-toggle" button that listens for a click event and toggles the "active" class on the "menu" element.

We'll also need to add some styles for the "active" class in our CSS.

.menu.active {
  display: block;
}

This CSS adds a display property of "block" to the "menu" element when it has the "active" class, which will make the menu visible.

And that's it! With these HTML, CSS, and JavaScript elements, we have created a functional hamburger menu that can be easily incorporated into any website. You can also enhance the functionality and styling of the menu as per your requirements.

In conclusion, building a hamburger menu is a simple and effective way to create a responsive navigation for smaller screens. By using HTML, CSS, and JavaScript, you can create a clean and user-friendly interface for your users to access the content on your website.