How to implement a perfect multi-level navigation bar

A question I often receive is what's the best way to implement a multi-level navigation bar for a website. In some cases, some of my readers ask to me a little help and send me "very original" solutions but not so suitable in terms of semantic and unobtrusive approach to coding. For my experience, I always suggest to use simple and standard methods. So in this tutorial I want to illustrate you how to implement a perfect multi-level navigation bar using HTML, CSS and some lines of unobtrusive JavaScript code with jQuery to show and hide sub sections. The result is the following:

You can quickly reuse the code I provided in this tutorial into your web projects, customizing the CSS file and changing the links. You can take a look at the demo here or download the source code here.

Navigation bar structure
Here is the standard box model of a typical navigation bar:

...and here is the standard HTML structure you would have to use to implement it

<div id="nav"> <!-- nav container -->
<li>item <!-- main item -->
<ul> <!-- item submenu -->
<li>sub item</li>

How you can see the code is composed by some nested unordered list. For example, in this tutorial I used the following code (main navigation items highlighted in bold):

<div id="nav">

<li><a href="#">Web Design</a>
<ul class="submenu">
<li><a href="">Woork</a></li>
<li><a href="">DZone</a></li>

<li><a href="#">Tech News</a>
<ul class="submenu">
<li><a href="">Mashable</a></li>
<li><a href="">CNET</a></li>


You can add new main items or sub items simply adding new <li> tags with an <ul> tag within.

CSS Code
Now take a look at the CSS code. I prepared a very basic style you can customize how you prefer in your projects. Here is the code for the container of the navigation bar (<div id="#nav">):

padding:0 10px;
#nav ul,
#nav ul li {
#nav ul li{

Here is the code for navigation links. #nav ul li {...} it's the code that defines main items (in this tutorial for example they are Web Design, Tech News, Social Network). #nav ul li ul li {...} it's the code that defines sub items for each main item.

#nav ul li a:link,
#nav ul li a:visited{
padding:0 20px 0 6px;
#nav ul li a:hover{
#nav ul li ul li{
#nav ul li ul li a:link,
#nav ul li ul li a:visited{
padding:0 10px;
border-bottom:solid 1px #DEDEDE;
#nav ul li ul li a:hover{

Here is the code for sub menu sections:

.submenu {
position: absolute;
width: 160px;
background: #FFF;
padding: 10px;
border: solid 1px #2E4B88;
border-top: none;
display: none;
line-height: 26px;
z-index: 1000;

jQuery Code
Now take a look at this simple jQuery code that shows and hides sub sections using mouseover and mouseleave jQuery events. The code is very simple and works independently from the number of main items contained into the navigation bar. You have to copy the following code into the <head> tag of your page. Remember first to include a link to jQuery using the following code:

<script type="text/javascript" src="jquery.js"></script>

Than add this code:

<script type="text/javascript">
function nav(){
$('div#nav ul li').mouseover(function() {

$('div#nav ul li').mouseleave(function() {
$('div#nav ul li ul').hide();

$('div#nav ul li ul').mouseleave(function() {
$('div#nav ul li ul').hide();;

$(document).ready(function() {

You can also add this code in an external JS file and import it into the page that use this script. That's all! Take a look at the demo here or download the source code here.

Leave a comment for your suggestions, thanks!

Elegant ScrollPanes with jQuery and CSS3Perfect pagination style using CSSHow to implement a launching soon page with PHP and jQuerySuper simple way to work with Twitter API (PHP + CSS)


Post a Comment

Copyright © 2011 Technology Management | Themes by Home Design Architecture.
personal, fashion, travel, loan, insurance, health, real estate, home, marketing, personal, fashion, travel, loan, insurance, health, real estate, home, marketing,