Easy to Style jQuery Drop Down Menu Tutorial

Written by Kevin Liew on 10 Nov 2009
193,483 Views • Tutorials

Introduction

Alright, this time, we are going to make a drop down menu. The main objective is to make it as simple as possible, with some little jQuery effect and easy to customize/ apply different style on it.

To style it into your own design, you just have to modify the a tag CSS style. You can put background images, hover effect and also change the submenu popup animation.

Before we start, I have had made quite a few of tutorials about menu, the following is the list, I guess you might be interested with some of these :)

As usual, we will start with HTML, CSS and finally Javascript.

Advertisement

1. HTML

This would be the most common way to structure a list menu. It would make sense even if the CSS is disabled.

<ul id="nav">
	<li><a href="#">Parent 01</a></li>
	<li><a href="#" class="selected">Parent 02</a>
		<ul>
			<li><a href="#">Item 01</a></li>
			<li><a href="#" class="selected">Item 02</a></li>
			<li><a href="#">Item 03</a></li>
		</ul>
		<div class="clear"></div>
	</li>
	<li><a href="#">Parent 03</a>
	<ul>
		<li><a href="#">Item 04</a></li>
		<li><a href="#">Item 05</a></li>
		<li><a href="#">Item 06</a></li>
		<li><a href="#">Item 07</a></li>
	</ul>			
		<div class="clear"></div>
	</li>
	<li><a href="#">Parent 04</a></li>
</ul>

<div class="clear"></div>

2. CSS

CSS is quite simple, we have to style two UL List - parent menu and submenu. We put float left for the parent menu so that all the list item would display inline. For the submenu, it's hidden by default, and will only display on mouse over event.

body {font-family:arial; font-size:11px;}
.clear {clear:both}	
/* remove the list style */
#nav {
	margin:0; 
	padding:0; 
	list-style:none;
}	
	
	/* make the LI display inline */
	/* it's position relative so that position absolute */
	/* can be used in submenu */
	#nav li {
		float:left; 
		display:block; 
		width:100px; 
		background:#ccc; 
		position:relative;
		z-index:500; 
		margin:0 1px;
	}
		
	/* this is the parent menu */
	#nav li a {
		display:block; 
		padding:8px 5px 0 5px; 
		font-weight:700;  
		height:23px; 
		text-decoration:none; 
		color:#fff; 
		text-align:center; 
		color:#333;
	}

	#nav li a:hover {
		color:#fff;
	}
	
	/* you can make a different style for default selected value */
	#nav a.selected {
		color:#f00;
	}
	
		/* submenu, it's hidden by default */
		#nav ul {
			position:absolute; 
			left:0; 
			display:none; 
			margin:0 0 0 -1px; 
			padding:0; 
			list-style:none;
		}
		
		#nav ul li {
			width:100px; 
			float:left; 
			border-top:1px solid #fff;
		}
		
		/* display block will make the link fill the whole area of LI */
		#nav ul a {
			display:block;  
			height:15px;
			padding: 8px 5px; 
			color:#666;
		}
		
		#nav ul a:hover {
			text-decoration:underline;	
		}

/* fix ie6 small issue */
/* we should always avoid using hack like this */
/* should put it into separate file : ) */
*html #nav ul {
	margin:0 0 0 -2px;
}

3. Javascript

This could be one of the most simplest jQuery scripts. We are using hover mouse event and just the most basic jQuery animation slideUp and slideDown. :) I love jQuery

$(document).ready(function () {	
	
	$('#nav li').hover(
		function () {
			//show its submenu
			$('ul', this).stop().slideDown(100);

		}, 
		function () {
			//hide its submenu
			$('ul', this).stop().slideUp(100);			
		}
	);
	
});

Conclusion

Like this tutorials? You can express your gratitude by visiting my sponsors on the sidebar, bookmark it and help me to spread this tutorial to our friends! :) Thanks!

Demo Download
Join the discussion

Comments will be moderated and rel="nofollow" will be added to all links. You can wrap your coding with [code][/code] to make use of built-in syntax highlighter.

150 comments
yandos 13 years ago
mm same problem, somtimes the dropdown just keeps opening and closing with no end... any idea someone?
Reply
fra 13 years ago
Wow, fantastic menu..works perfectly! thank you :)
Reply
swami 13 years ago
I have the same issue as @Giovanni
When I use the dropdown menu in a repeating list, sometimes the submenu overlaps with the main menu of the next list, and when this happens, because the z-index of the main list items are 500, you can see through the submenu. What would be the best way to fix this? Thanks.
Reply
Kevin Anderson 13 years ago
Love the Tut, just one question. When I my page loads, the drop down menu is already down, then when I scroll over the nav the Jquery kicks in. Is there a way to get the Jquery to kick in when the page loads and not when I scroll over it.
Reply
pudds 13 years ago
For anyone with the flicker problem: changing the script to: slideUp(0) resolves the problem for me.

As far as I can tell, its a jquery bug related to re-triggering the slideDown before the slideUp is completed.

slideDown(0) and slideUp(0) also works, but if you like the slide effect, just change the Up value.
Reply
Saro 13 years ago
Hey Kevin Anderson, you can easely fix that by adding this css snippet.


#nav li ul{
display:none;
}
Reply
Valikonen 13 years ago
Very cool collection, thanks. If you want to see menus, web site trends, galleries etc, visit http://www.1artmedia.com , you have online demo and free download. Bye!
Reply
MB 13 years ago
Anyway to make the menus pop up rather than drop down? I want to use this at the bottom of a page.
Reply
CK 13 years ago
Is there a way to make this slide up rather than down? Would like to put it at the bottom of the page and have the menus expand up.
Reply
kevin Admin 13 years ago
change slideDown & slideUp to show & hide
Reply
HAJJAJ JAMAL ALI AL HAJJAJI 13 years ago
that is really very good.

thanks alot
Reply
Justin 13 years ago
By far the simplest and most easy to implement drop down menu.

Thanks!
Reply
Jimmy 13 years ago
Really like this menu. But im also using it at the bottom of the page and want it to slide up. I dont seem to figure it out how. You wrote "change slideDown & slideUp to show & hide" i cant get it to work, what can i be doing wrong?
Reply