Multilevel Toggle Responsive Navigation Menu

Introduction

This multilevel toggle responsive navigation is similar to the nested accordions on the smaller screen. When the user clicks or touches the parent on the menu bar, then the dropdown submenu will be opened and if he touches the sub menu then the submenu's submenu will be opened. There will be one toggle button that will be clickable.
 
Now let's start with designing a HTML layout.

This article will add prefixfree.min.js to the layout that automatically adds a prefix to CSS properties to help keep the prefix free and the style sheet cleaner.
 
Now add the following lines of code for the navigation menu:
  1. <!doctype html>  
  2. <html lang="en">  
  3. <head>  
  4.  <meta charset="UTF-8">  
  5.  <title>Multilevel Toggle Responsive Navigation Menu</title>  
  6.  <link rel="stylesheet" href="style.css">  
  7.  <script src="jquery.js"></script>  
  8.  <script src="prefixfree.min.js"></script>  
  9.  <meta name="viewport" content="width=device-width, initial-scale=1.0">   
  10. </head>  
  11. <body>  
  12.  <nav><div class="menu-btn"><img src="btn-hamburger.png" alt=""></div>  
  13.  <ul class="menu">  
  14.   <li><a href="#">Item 1</a>  
  15.    <ul>  
  16.     <li><a href="#">Sub Item 1.1</a></li>  
  17.     <li><a href="#">Sub Item 1.2</a>  
  18.      <ul>  
  19.       <li><a href="#">Sub Item 1.2.1</a></li>  
  20.       <li><a href="#">Sub Item 1.2.2</a>  
  21.        <ul>  
  22.        <li><a href="#">Sub Item 1.2.2.1</a></li>  
  23.        <li><a href="#">Sub Item 1.2.2.2</a>  
  24.        <ul>  
  25.         <li><a href="#">Sub Item 1.2.2.2.1</a></li>  
  26.         <li><a href="#">Sub Item 1.2.2.2.2</a></li>  
  27.        </ul>  
  28.        </li>  
  29.       </ul>  
  30.       </li>  
  31.      </ul>  
  32.     </li>  
  33.     <li><a href="#">Sub Item 1.3</a></li>  
  34.    </ul>  
  35.   </li>  
  36.   <li><a href="#">Item 2</a>  
  37.    <ul>  
  38.     <li><a href="#">Sub Item 2.1</a>  
  39.      <ul>  
  40.       <li><a href="#">Sub Item 2.1.1</a></li>  
  41.       <li><a href="#">Sub Item 2.1.2</a></li>  
  42.      </ul>  
  43.     </li>  
  44.     <li><a href="#">Sub Item 2.2</a></li>  
  45.    </ul>  
  46.   </li>  
  47.   <li><a href="#">Item 3</a></li>  
  48.   <li><a href="#">Item 4</a></li>  
  49.  </ul>  
  50.  </nav>  
  51.  <div class="container">  
  52.  <!--content here-->  
  53.  </div>  
  54. </body>  
  55. </html>  
By using the preceding code your output page will be as follows:



Now we are adding some styles to our navigation bar. The code is as follows:
  1. body, ul, li,a {    
  2.     margin0;    
  3.     padding0;    
  4.     list-stylenone;    
  5. }    
  6. .menu{    
  7.     background: rgba(44621000.99);    
  8. }    
  9. .menu a{    
  10.     displayblock;    
  11.     text-decorationnone;    
  12.     padding20px 30px;    
  13.     color#fff;    
  14.     border1px solid #000000;    
  15. }    
  16. .menu li{    
  17.     positionrelative;    
  18. }    
  19. .menu li ul{    
  20.     displayblock;    
  21.     background: rgba(255000.12);    
  22.     max-height0;    
  23.     overflowhidden;    
  24.     transition: all 0.3s ease;    
  25. }    
  26. nav{    
  27.     transition: max-height 0.3s;    
  28.     overflowhidden;    
  29. }    
  30. .menu-btn{    
  31.     background#232d3b;    
  32.     text-aligncenter;    
  33.     cursors-resize;    
  34. }    
  35. .menu-btn img{    
  36.     vertical-alignmiddle;    
  37.     height50px;    
  38. }    
  39. .menu-open,    
  40. .menu .open > ul{    
  41.     max-height2000px;    
  42. }    
  43. .menu .open > .more img{    
  44.     transform: rotate(-90deg);    
  45. }    
  46. .more img{    
  47.     height55px;    
  48.     transition: transform 0.3s;    
  49. }    
  50. .more{    
  51.     background: rgba(0000.38);    
  52.     floatright;    
  53.     min-width10%;    
  54.     cursormove;    

Using the preceding CSS code your page will look such as in the following:

As you can see that this toggle menu is just for the smaller screen or we can say for a phone or tablet screen only. So for a bigger screen (Laptop, Desktop and so on) I am writing some media query as shown below and including this media query with this same CSS file.
 
Media query:
  1. @media all and (min-width900px){    
  2.    nav{    
  3.        max-height100%;    
  4.        overflowvisible;    
  5.    }    
  6.    .menu-btn{    
  7.        displaynone;    
  8.    }    
  9.    .menu{    
  10.        text-aligncenter;    
  11.    }    
  12.    .menu a{    
  13.        width:9em;    
  14.        background#232d3b;    
  15.    }    
  16.    .menu a:hover{    
  17.        background-color#099;    
  18.    }    
  19.    .menu > li{    
  20.        display: inline-block;    
  21.        margin0 2px;    
  22.    }    
  23.    .menu li ul{    
  24.        left: -9999px;    
  25.        position:absolute;    
  26.        overflowvisible;    
  27.        -webkit-transition: all 0.1s ease;    
  28.    }    
  29.    .menu > li > ul ul{    
  30.        top: 0;    
  31.    }    
  32.    .menu li:hover > ul{    
  33.        left:100%;     
  34.    }    
  35.    .menu > li:hover > ul{    
  36.        left: 0;    
  37.    }    
  38.    .menu .open > ul{    
  39.        left:100%;     
  40.    }    
  41.    .menu > li.open > ul{    
  42.        left: 0;    
  43.    }    

The preceding styles or media query overrides the previous style when the screen is larger than 900 pixels. When the screen is larger than 900 pixels then the output will be as follows:

menu
 
Now I am adding some jQuery functions to add the following functionality to our menu.
  1. Add a .parent class to make the list item having a sub-menu easier that will hold a list inside the parent.

  2. Add a div with the class .more holding a hamburger.png image that will be used for the toggle button.

  3. When the .more button is clicked its parent list item should expand to its original height using the .open class.

  4. The .menu-btn class that will be responsible between the hidden and visible states of toggle.
Add the following lines of code to the Head Section of the HTML page.
  1. <script>  
  2.     $(document).ready(function(){  
  3.        $(".menu li a").each(function() { 

  4.            if ($(this).next().length > 0) { 

  5.                $(this).addClass("parent");  
  6.            };  
  7.        })  
  8.        var menux = $('.menu li a.parent');  
  9.        $( '<div class="more"><img src="btn-hamburger.png" alt=""></div>' ).insertBefore(menux);  
  10.        $('.more').click(function(){  
  11.             $(this).parent('li').toggleClass('open');  
  12.        });  
  13.        $('.menu-btn').click(function(){  
  14.           $('nav').toggleClass('menu-open');  
  15.       });  
  16.    });  
  17. </script>  
By using the preceding code the output will be:

main menu
 
And for a small screen:
 
Now add the following lines of code more to the nav rule set. This line will the allow toggle menu to be hidden by default on smaller screens.
  1. nav{  
  2.  max-height50px;  
  3.  transition: max-height 0.3s;  
  4.  overflowhidden;  
  5.  
Final Output

Up Next
    Ebook Download
    View all
    Learn
    View all
    sourabhsomani.com