Advertisement
  1. Web Design
  2. HTML/CSS
  3. CSS

Making a Sliding Side Navigation Menu for Responsive Designs

Scroll to top

In this tutorial, you will be creating an expandable side navigation menu with JavaScript and CSS. The final product will appear as shown below:

1. Create the Markup

To get started, let's add some markup for our side menu:

1
<div id="sideNavigation" class="sidenav">
2
  <a href="#" class="close-btn">&times;</a>
3
  <a href="#">About</a>
4
  <a href="#">Features</a>
5
  <a href="#">Contact Us</a>
6
</div>
7
 
8
<nav class="topnav">
9
  <a href="#" class="ham-icon">
10
    <svg width="30" height="30" id="icoOpen">
11
        <path d="M0,5 30,5" stroke="#000" stroke-width="5"/>
12
        <path d="M0,14 30,14" stroke="#000" stroke-width="5"/>
13
        <path d="M0,23 30,23" stroke="#000" stroke-width="5"/>
14
    </svg>
15
  </a>
16
</nav>
17
 
18
<section id="main">
19
  <h1>This Side Navigation Menu Looks Good!</h1>
20
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
21
</section>

Here you can see we created a side menu div with the class sidenav. Next, we added the actual top bar navigation via a <nav> tag, and we are using an SVG for our side menu icon.

Remember to put all of your website's content inside the div id="main" container so that it will slide to the right.

2. Code the JavaScript

Next, let's add the JavaScript which will listen for click events on the hamburger icon with the class ham-icon and the close button that appears once the side navigation menu has slid onto the screen.

1
document.querySelector("a.ham-icon").addEventListener("click", function(event){
2
  document.getElementById("sideNavigation").style.width = "250px";
3
  document.getElementById("main").style.marginLeft = "250px";
4
});
5
6
document.querySelector("a.close-btn").addEventListener("click", function(event){
7
  document.getElementById("sideNavigation").style.width = "0";
8
  document.getElementById("main").style.marginLeft = "0";
9
});

A click on the hamburger icon is supposed to show the side navigation. We do this by setting the width of our navigation to 250px and at the same time adding a left margin of 250px to the main website content.

A click on the close button is supposed to hide the side navigation. We do this by changing the width of our navigation back to 0 while setting the left margin of the main website content to 0.

3. Style With CSS

Finally, we will need to style our page with some CSS for the side menu and our links. The CSS will properly place all the webpage elements where we intend them to be. We will also apply some simple animations with the help of the transition property. Let's take a look at the CSS one part at a time.

1
.sidenav {
2
  height: 100%;
3
  width: 0;
4
  position: fixed;
5
  z-index: 1;
6
  top: 0;
7
  left: 0;
8
  background-color: #111;
9
  overflow-x: hidden;
10
  padding-top: 4rem;
11
  transition: 0.4s cubic-bezier(0.18, 0.89, 0.32, 1.28);
12
  font-family: "Bebas Neue";
13
}
14
15
.sidenav a {
16
  padding: 0.5rem 1rem;
17
  text-decoration: none;
18
  color: #bdbdbd;
19
  display: block;
20
  transition: 0.4s;
21
  white-space: nowrap;
22
  font-size: 2rem;
23
}
24
25
.sidenav a:hover {
26
  color: white;
27
  background: #9e9e9e;
28
}

We set the height of the side navigation to 100% and its initial width to 0 to keep it hidden. However, the contents of the side navigation only stay hidden if the value of overflow-x property is set to hidden.

The transition property makes sure that the change in the width of the side navigation isn't sudden and the easing function makes it a little bouncy.

For the links inside the side navigation, we have set the value of the white-space property to nowrap so that the menu text doesn't spill over multiple lines.

1
.sidenav .close-btn {
2
  position: absolute;
3
  top: -1rem;
4
  right: 1rem;
5
  font-size: 5rem;
6
}
7
8
.sidenav .close-btn:hover {
9
  background: initial;
10
  transform: scale(1.2);
11
}

The CSS above styles our close button separately from other links in the side navigation. We have applied absolute positioning to the close button and prevent its background from turning light gray on hover. It also scales up 20% in size when users hover over it.

Now, the following CSS will make sure that the shift in position of the main content is synchronized with the navigation menu by using the same transition duration and the same easing function. Setting the value of overflow-x property to hidden makes sure that no horizontal scrollbar appears due to the content shift.

1
#main {
2
  transition: margin-left 0.4s cubic-bezier(0.18, 0.89, 0.32, 1.28);
3
  padding: 20px;
4
  width: 100%;
5
}
6
7
body {
8
 overflow-x: hidden; 
9
}

We can also add a little rotational motion to the hamburger icon with the help of the following CSS. It applies a rotation of 180 degrees to the hamburger icon over a period of 0.5 seconds.

1
a svg {
2
  transition: all 0.5s ease;
3
}
4
a svg:hover {
5
  transform: rotate(180deg);
6
}

Finally, let's make the navigation menu responsive by adjusting the spacing and the size of links with the following CSS. It makes sure that the menu doesn't go out of bounds on screens with smaller vertical space.

1
@media screen and (max-height: 480px) {
2
  .sidenav {
3
    padding-top: 3rem;
4
  }
5
  .sidenav a {
6
    font-size: 1.5rem;
7
  }
8
}

Your navigation menu should be like the following CodePen demo at this point.

4. Removing the Slide

To make the menu appear with no slide animation, simply make changes to the CSS property transition, as shown in an abbreviated form below:

1
.sidenav {
2
    transition: 0s; 
3
}
4
5
#main {
6
    transition: margin-left 0s;
7
}

This will make the change appear instantly as there is no delay specified in the transition. The default we used is 0.5s.

Conclusion

Creating a side menu only takes a few lines of code and does not need to use many resources. Making the code responsive to work with different device screen resolutions is merely a case of modifying the CSS by adding media queries for specific cases.

To take this further, you may wish to style your menu with a CSS framework such as Bootstrap or Bulma.

This post has been updated with contributions from Monty Shokeen. Monty is a full-stack developer who also loves to write tutorials and to learn about new JavaScript libraries.

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Web Design tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.