How to Create a Horizontal Hierarchical Menu Using HTML, CSS and jQuery

Friends,
 
This is Part 2 of the series to create hierarchical menus. You can see the first part “How to Create a Hierarchical Menu Using HTML And CSS” here. This article will explain how to create a multi-level menu using HTML, CSS and a bit of jQuery. So, let's get started.
 
We will use an existing plugin named “jQSimpleMenu” to create this menu. Since we're going to use jQuery in our project, include jQuery in your project in the <head> tag. Also, you will need to include the plugin JS file jqsimplemenu.js and CSS File jqsimplemenu.css.
  1. <script type="text/javascript" src="js/jquery-1.4.4.min.js"></script>  
  2. <script type="text/javascript" src="js/jqsimplemenu.js"></script>  
  3. <link rel="stylesheet" href="css/jqsimplemenu.css" type="text/css" /> 
Now, let us create a HTML structure as below using <ul> and <li> tags. Write the following code in your HTML file where you want to place the menu. The important part here is to assign a class to the “<ul>” tag.
  1. <div>  
  2.     <ul class="menu">  
  3.         <li><a href="#">Menu 1</a></li>  
  4.         <li><a href="#">Menu 2</a>  
  5.             <ul>  
  6.                 <li><a href="#">Menu 2.1</a></li>  
  7.                 <li><a href="#">Menu 2.2</a></li>  
  8.                 <li><a href="#">Menu 2.3</a></li>  
  9.             </ul>  
  10.         </li>  
  11.         <li><a href="#">Menu 3</a>  
  12.             <ul>  
  13.                 <li><a href="#">Menu 3.1</a></li>  
  14.                 <li><a href="#">Menu 3.2</a></li>  
  15.                 <li><a href="#">Menu 3.3</a></li>  
  16.             </ul>  
  17.         </li>  
  18.     </ul>  
  19. </div> 
Once the HTML structure is ready we just need to call the plugin function to convert our HTML into a beautiful looking menu. You can do this as below:
  1. <script type="text/javascript">  
  2.     $(document).ready(function () {  
  3.         $('.menu').jqsimplemenu();  
  4.     });  
  5. </script> 
You're done. The complete HTML file will look something as in the following:
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <script type="text/javascript" src="js/jquery-1.4.4.min.js"></script>  
  5.     <script type="text/javascript" src="js/jqsimplemenu.js"></script>  
  6.     <link rel="stylesheet" href="css/jqsimplemenu.css" type="text/css" media="screen" />  
  7.     <script type="text/javascript">  
  8.         $(document).ready(function () {  
  9.             $('.menu').jqsimplemenu();  
  10.         });  
  11.     </script>  
  12. </head>  
  13. <body>  
  14.     <div>  
  15.         <ul class="menu">  
  16.             <li><a href="#">Menu 1</a></li>  
  17.             <li><a href="#">Menu 2</a>  
  18.                 <ul>  
  19.                     <li><a href="#">Menu 2.1</a></li>  
  20.                     <li><a href="#">Menu 2.2</a></li>  
  21.                     <li><a href="#">Menu 2.3</a></li>  
  22.                 </ul>  
  23.             </li>  
  24.             <li><a href="#">Menu 3</a>  
  25.                 <ul>  
  26.                     <li><a href="#">Menu 3.1</a></li>  
  27.                     <li><a href="#">Menu 3.2</a></li>  
  28.                     <li><a href="#">Menu 3.3</a></li>  
  29.                 </ul>  
  30.             </li>  
  31.         </ul>  
  32.     </div>  
  33. </body>  
  34. </html> 
The final output of the HTML file will look as in the following
 
HTML Output using jQuery
 
Complete Source: Download
 
I hope you like this article! Keep learning and sharing!


Rebin Infotech
Think. Innovate. Grow.