Click here to Skip to main content
15,891,253 members
Articles / Web Development / HTML5
Tip/Trick

How to Create a Hierarchical Menu Using HTML And CSS

Rate me:
Please Sign up or sign in to vote.
4.90/5 (13 votes)
30 Apr 2014CPOL 41.6K   21   11
Friends, I am planning to write a series of articles on creating menus in different ways and this is the 1st one in the series. This post will explain how to create a simple 2 level menu using HTML and CSS only. We know there are a lot of ways to create a HTML menu …Read more →

Friends,

I am planning to write a series of articles on creating menus in different ways and this is the 1st one in the series. This post will explain how to create a simple 2 level menu using HTML and CSS only. We know there are a lot of ways to create a HTML menu but at times, we need to use only HTML/CSS in the project to create a nice menu. So, lets get started.

We will create a HTML structure as below using <ul> and <li> tags. Write the below code in your HTML file where you want to place the menu.

<div class="menu">
    <ul>
        <li><a href="#">Menu 1</a></li>
        <li><a href="#">Menu 2</a>
            <ul>
                <li><a href="#">Menu 2.1</a></li>
                <li><a href="#">Menu 2.2</a></li>
                <li><a href="#">Menu 2.3</a></li>
            </ul>
        </li>
        <li><a href="#">Menu 3</a>
            <ul>
                <li><a href="#">Menu 3.1</a></li>
                <li><a href="#">Menu 3.2</a></li>
                <li><a href="#">Menu 3.3</a></li>        
            </ul>
        </li>
    </ul>   
</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 below code in the >head< section.

<style type="text/css">
.menu{
    width: 500px;
    margin: 0px auto;
    font-family: Arial, Helvetica, sans-serif;
    font-weight: bold;
    font-size: 14px;
}
.menu ul li a:link, div ul li a:visited {
    display: block;
    background-color: #f1f1f1;color:#000;
    text-align: center;
    text-decoration: none;
    padding: 4px;
    border-bottom: 1px solid #fff;
    width: 150px;        
}
.menu ul li a:hover{
    background-color: #ccc;
}
.menu ul li ul li a:link, li ul li a:visited {
    display: block;
    background-color: #f1f1f1;
    color: #000;
    text-align: center;
    text-decoration: none;
    padding: 4px;
    border-bottom: 1px solid #fff;
    width: 150px;
}
.menu ul li ul li a:hover {
    background-color: #ccc;
}
.menu ul {
    list-style-type: none;
    margin: 0px;
    padding: 0px;   
}
.menu ul li {
    float: left;
    margin-left: 5px;
}
.menu ul li ul li {
    float: none;
    margin-left: 0px;
}
.menu ul li ul {
    display: none;  
}
.menu li:hover ul{
    display: block; 
}
</style>

Save the HTML file and double click on it to view on the browser. Your menu should look something like the below screesnhot.
html-css-menu

Hope you like this post! Cheers!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder Rebin Infotech
India India
A passionate developer with over 10 years of experience and building my software company code by code. Experience withMS Technologies like .Net | MVC | Xamarin | Sharepoint | MS Project Server and PhP along with open source CMS Systems like Wordpress/DotNetNuke etc.

Love to debug problems and solve them. I love writing articles on my website in my spare time. Please visit my Website for more details and subscribe to get technology related tips/tricks. #SOreadytohelp

Comments and Discussions

 
GeneralMy vote of 5 Pin
Anurag Gandhi30-Apr-14 7:13
professionalAnurag Gandhi30-Apr-14 7:13 
GeneralRe: My vote of 5 Pin
Nitesh Kejriwal1-May-14 1:12
professionalNitesh Kejriwal1-May-14 1:12 
QuestionNot an article Pin
Rage29-Apr-14 21:52
professionalRage29-Apr-14 21:52 
AnswerRe: Not an article Pin
Anurag Gandhi30-Apr-14 7:21
professionalAnurag Gandhi30-Apr-14 7:21 
QuestionCSS scope Pin
cpGlenn25-Apr-14 22:55
cpGlenn25-Apr-14 22:55 
AnswerRe: CSS scope Pin
Nitesh Kejriwal28-Apr-14 23:47
professionalNitesh Kejriwal28-Apr-14 23:47 
AnswerRe: CSS scope Pin
Nitesh Kejriwal29-Apr-14 5:22
professionalNitesh Kejriwal29-Apr-14 5:22 
GeneralRe: CSS scope Pin
cpGlenn29-Apr-14 7:52
cpGlenn29-Apr-14 7:52 
Thanks for the quick response. I see you added an extra <div>... that does improve the situation, but it still depends on the structure and nesting of tags, rather than user specified semantics. While this will now not affect all <ul>, only those in a <div>, that still allows for unintentional triggering of these CSS rules in situations where it might not have been intended. Clearly, <ul> can be used for other things than hierarchical menus.

In my own coding, I would use an attribute class="Nitesh-Luharuka's-Menu-system" (well, maybe something shorter) on the outer <ul> to specify that this particular <ul> block is intended to be a menu, rather than making assumptions based on the structure of the tags... because the structure of the tags alone can be used for other semantic purposes.

The specification of the styling of the contained elements can be based purely on structure, but the outermost item should be based on semantic intention, so a user could have one or more menus in his page, yet not be concerned that other uses of similar structures will be considered menus. I can understand that in demonstration code, there are no other uses, but in educational code, it is good to consider not only how to get something to work, but how to get to work exactly where you want it to work, and only in those places.
GeneralRe: CSS scope Pin
Nitesh Kejriwal29-Apr-14 9:04
professionalNitesh Kejriwal29-Apr-14 9:04 
GeneralRe: CSS scope Pin
cpGlenn29-Apr-14 9:38
cpGlenn29-Apr-14 9:38 
GeneralRe: CSS scope Pin
Nitesh Kejriwal29-Apr-14 15:59
professionalNitesh Kejriwal29-Apr-14 15:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.