How to Create a Horizontal Hierarchical Menu Using HTML And CSS

Introduction

 
I am planning to write a series of articles on creating menus in various ways and this is the first one in the series. This article will explain how to create a simple 2-level menu using HTML and CSS only.
 
We know there are many ways to create an HTML menu but at times, we need to use only HTML/CSS in the project to create a nice menu. So, let's get started.
 
We will 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.
  1. <div class="menu">  
  2.     <ul>  
  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> 
Now, we have the structure ready. Definitely, the current menu does not look good and we will now apply some CSS to make it look beautiful. Write the following code in the <head> section.
  1. <style type="text/css">  
  2.     .menu  
  3.     {  
  4.         width500px;  
  5.         margin0px auto;  
  6.         font-familyArialHelveticasans-serif;  
  7.         font-weightbold;  
  8.         font-size14px;  
  9.     }  
  10.     .menu ul li a:link, div ul li a:visited  
  11.     {  
  12.         displayblock;  
  13.         background-color#f1f1f1;  
  14.         color#000;  
  15.         text-aligncenter;  
  16.         text-decorationnone;  
  17.         padding4px;  
  18.         border-bottom1px solid #fff;  
  19.         width150px;  
  20.     }  
  21.     .menu ul li a:hover  
  22.     {  
  23.         background-color#ccc;  
  24.     }  
  25.     .menu ul li ul li a:link, li ul li a:visited  
  26.     {  
  27.         displayblock;  
  28.         background-color#f1f1f1;  
  29.         color#000;  
  30.         text-aligncenter;  
  31.         text-decorationnone;  
  32.         padding4px;  
  33.         border-bottom1px solid #fff;  
  34.         width150px;  
  35.     }  
  36.     .menu ul li ul li a:hover  
  37.     {  
  38.         background-color#ccc;  
  39.     }  
  40.     .menu ul  
  41.     {  
  42.         list-style-typenone;  
  43.         margin0px;  
  44.         padding0px;  
  45.     }  
  46.     .menu ul li  
  47.     {  
  48.         floatleft;  
  49.         margin-left5px;  
  50.     }  
  51.     .menu ul li ul li  
  52.     {  
  53.         floatnone;  
  54.         margin-left0px;  
  55.     }  
  56.     .menu ul li ul  
  57.     {  
  58.         displaynone;  
  59.     }  
  60.     .menu li:hover ul  
  61.     {  
  62.         displayblock;  
  63.     }  
  64. </style> 
Save the HTML file and double-click on it to view it in the browser. Your menu should look something like the following screenshot.
 
menu
 
You can read other parts of this series below:
I hope you like this article! Cheers!