You are currently viewing Hamburger Menu Using HTML,CSS and JavaScript

Hamburger Menu Using HTML,CSS and JavaScript

Hamburger Menu Using HTML,CSS and JavaScript

Hamburger menu has become an std icon for Navigation, it has become so popular the even most mainstream platforms and apps use them nowadays.

It was named in this manner because of its resemblance to the Hamburger.

Hamburger Menu Using HTML,CSS and JavaScript
Hamburger Menu Using HTML,CSS and JavaScript


Why use Hamburger Menu?

The hamburger menu gives the Website a neater and cleaner way to navigate or by organizing content. Also, Designers have been using the Hamburger menu for so long that it has become a standard itself.

How to Create Hamburger-menu:

There are multiple ways to create Hamburger Menu.
Today we’ll be creating a Hamburger Menu with the help of HTML, CSS & JavaScript.
Without further a do, Let’s get right into it.

1.Writing HTML code:

<nav class="navbar">
<div class="title">CWR</div>
<div class="ham" >
<span class="bar1"></span>
<span class="bar2"></span>
<span class="bar3"></span>
<ul class="nav-sub">
<li class="list-item"><a href="#" class="nav-link">Home</a></li>
<li class="list-item"><a href="#" class="nav-link">Blogs</a></li>
<li class="list-item"><a href="#" class="nav-link">About me</a></li>

In the above code snippet at the top, we’ve declared <nav> tag with the class navbar which will hold all the elements of our header. Let’s split the header into 3 parts for an easier understanding.

Title – that will hold our site name.
Ham- which will contain the config for the hamburger menu.
Nav-sub- which will have the links of the navigation.
Today we’ll only focus on how to create the hamburger menu. In the HTML part, we only have to define a div element (which will contain the hamburger menu) and 3 span elements (each representing the iconic line for the hamburger menu).

Notes App Using Javascript Code

There is all HTML code for the Hamburger Menu javascript. Now, you can see output without CSS, then we write CSS for our Hamburger Menu javascript.


Hamburger Menu Using HTML,CSS and JavaScript
Hamburger Menu Using HTML,CSS and JavaScript

2. Writing CSS :

Let’s start by defining the CSS properties of the NavBar And The Title.

/*Properties for Nav-bar*/
padding:0px 20px;

Now let’s move on to our list of navbar.

.list-item {

And lastly, it’s time to define the properties of our hamburger menu.

In the .ham class selector we have set display to none as we don’t want the hamburger icon appear until the screen has certain conditions.(we will be defining them a bit later)

/*Properties for Hamburger*/
margin:5px auto;
transition: 0.4s;

But we haven’t made this whole thing responsive yet as you can notice in the clip below.

Created Header Using Html Code

For it to appear properly on the small devices, we need to further declare media queries properties.

In the media query, I have set the max-width to 768px .You can go ahead and make another media query for the tablets ratio. If you want to learn more about media query you can visit here

@media only screen and (max-width:768px){
border-radius:0px 0px 0px 20px;
transition:transform 0.3s ease-in;
.change .bar1 {
transform: rotate(45deg) translate(8px,3px);
.change .bar2 {
opacity: 0;
.change .bar3 {
transform: rotate(-45deg) translate(8px,-3px);

After applying the media query properties the navbar should appear as follows on smaller devices.


Hamburger Menu Using HTML,CSS and JavaScript
Hamburger Menu Using HTML,CSS and JavaScript


3. Writing JS Code:

To finish our Hamburger menu and make all the elements function as we want them to,
let’s add write javascript code for it.

In the code below I have created two bindings named hamburger and navsub.

hamburger binding is used to toggle the icon from 3 lines to X and the navsub is used the making the navbar  appear and disappear when the icon is clicked.

const hamburger = document.querySelector(".ham");
const navsub = document.querySelector(".nav-sub");
hamburger.addEventListener('click', () => {

After this it should work as following clip:

50+ Frontend Project With Code

You Can Test below how this whole project work


We have successfully created our Hamburger menu along with a navbar. Although there are multiple ways to apply the hamburger menu instead of using that span tags. If you have any queries please ask in the comments.

Let me know in the comments below which way do you prefer for the Hamburger menu.
Written by: @Om Bandiwan

This Post Has 0 Comments

  1. J

    for me the hamburger menu does not open the list once clicked

  2. Anonymous

    sir this is helpful
    thanks sir

  3. Anonymous

    same here; does not open when clicking the the tab.
    the inspector shows an error : Uncaught TypeError: Cannot read properties of null (reading 'addEventListener')

  4. Anonymous

    very helpful bahut bahut dhanyavad dil se

  5. Anonymous


  6. Anonymous


Leave a Reply