Easy to Style jQuery Drop Down Menu Tutorial

Written by Kevin Liew on 10 Nov 2009
199,286 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
pratham 15 years ago
nice tutorials .....thanks
http://www.scrapsforever.com
Reply
ms 15 years ago
how do you set the spacing between the menu and the children menu? I've altered the height to accomodate my design and I've got a gap there.
Reply
chris 15 years ago
i have flicker problem too...bad...
Reply
chris 15 years ago
@MS i actually had to do the same, look for the property in the nav css that has absolute positioning-left, there is not top defined. Create a top property and set a value. Good to go.
Reply
jag123 15 years ago
Kevin: I really like your menu and I am using it for a top navigation bar. However I want the same navigation bar to appear in the footer of the page and it isn't working there. I copied the script exactly. Is there something I need to change to make it work twice on the same page? Thanks.
Reply
mayank 15 years ago
hey i just used your css markup and jquery to get this menu like nav bar on my site..worked like a charm
Reply
kevin Admin 15 years ago
@James: I think you can fix that issue by not putting the script in $(document).read(function() { ...... });

It should fire up straight away.
Reply
kevin Admin 15 years ago
@jag: I think u just have to change this selector to your footer's:

$('#nav li').hover.....
Reply
James Likeness 15 years ago
Hey Kevin, great tutorial! I've got the dropdowns working just like I wanted them to thanks to this. I have a quick question, though. My page seems to be taking a long time to load some element, and because the jQuery is waiting for the document to be ready, it's making the drop down have a delay until it's available. Is there some way to speed that up?
Reply
william 15 years ago
Its very usefull for me.Thx
Reply
Trys Mudford 15 years ago
At last! A simple but effective menu that doesn't have anything overly fancy but works every time. All the other menu tutorials are all too flash. A simple text menu is all thats needed.Thankyou so much!
Reply
Giovanni 15 years ago
Great work! And if you use a class instead of an ID, you can replicate the menu many times in the page.

However, I have only one issue with this setup: since some of the menus are close together, the dropdown menu does not overlap the other ones due to the same z-index in all instances of the menu. Hope I explained the problem somewhat... :) Do you think there's a way to solve it?

thanks
Reply
Betsy 15 years ago
You mentioned that hover-effect backgrounds can be used in this menu.

I've been searching for a tutorial that would teach me how to make a drop-down hover-effect image menu and yours appeared to it fit the bill. The tutorial is simple and clear. Thanks :)

Unfortunately, I can't seem to get my menu to work using the image backgrounds. Although I use "list-style:none" on the ul, the entire horizontal & vertical menu shows up with bullets. Also, the drop-down menu isn't hidden. Here's an example.... http://www.deadlyvintagesweets.com/dindex.htm.

would you mind giving a bit more advice & tell me what I'm doing wrong?
Reply