Easy to Style jQuery Drop Down Menu Tutorial

Demonstration Download

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).slideDown(100);

		}, 
		function () {
			//hide its submenu
			$('ul', this).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!

Demonstration Download

AdvertisementOur self paced 642-374 training courses and high quality brain dump provide you definite guarantee for passing the real 640-802 dumps exam. If you get through 640-802 exams, you will find other HP0-S25.

Show Some Love, Spread This Post!

96 comments

Marvio Rocha Wed, 4th January 2012 Very nice, i would like see a tutorial with wordpress menu dropdown.

Thank you
Reply
Amy Tue, 27th December 2011 Thank you for sharing.....saved me stress!
Reply
lalit Mon, 12th December 2011 NIce...I like it
Reply
Pandora Mon, 31st October 2011 Hi,
Thanks for this - this is a great script. I do have one question: I'm trying to turn the menu into a completely vertical one, meaning that both levels are aligned vertically (and level 2 opens on top of level 1). This works great on every browser except, of course, IE, where the 2nd level opens under the 1st. I assume this is a z-index issue, but I haven't been able to fix it. Do you have any ideas? Is this even possible? Thanks!
Reply
Arun Sun, 9th October 2011 Sir cud u please explain
y dat it doesnt work on aspx page...?
Please help..
Reply
Kevin Liew Thu, 13th October 2011 Not quite sure Arun, as server side language shouldn't affect the script. You need to make sure you have all the scripts in place and no conflict with other scripts.
Michael Wed, 5th October 2011 Ah, the demo works in these browsers so I need to get to the bottom of why it does not work for me.
Reply
Michael Wed, 5th October 2011 Works on both Firefox and Safari but NOT on Chrome or IE 9
Reply
parag Fri, 30th September 2011 the menu works perfect in mozilla ie8. but in ie7 the menu hides behind any division put below it. please help
Reply
George Mon, 29th August 2011 The best drop down menu.
Reply
Jeff Mon, 1st August 2011 Thanks, It's awesome. but I was wondering if it's possible to make it multilevel? I mean another <ul></ul> within the first one.
Reply
Kevin Liew Mon, 1st August 2011 Hi Jeff, unfortunately, it's only limits to 2 level. If you want a more robust drop down menu, try superfish:
http://users.tpg.com.au/j_birch/plugins/superfish/
Rich Fri, 29th July 2011 For me, I was getting some strange behaviour because the slide up / down functions were getting called too often if you rapidly moved the mouse over the menu items. This code solves that: http://pastebin.com/DzamVHfq
Reply
tim Thu, 7th July 2011 Greate article. Can you explain the theory behind why the sublist hides when you unhover from the parent?
Reply
Kevin Liew Sun, 17th July 2011 change this $('ul', this).slideDown(100);
to $('#nav ul', this).slideDown(100);

same thing for the next one.
Tony Måseskjær Sun, 26th June 2011 At http://www.webdevout.net/test?02K - why do the dropdown menus flicker 1px to the left once you hover off the main links?

Thanks in advance!

Tony
Reply
Mani Tue, 21st June 2011 Thanks for this awesome tutorial loved it ( life saver ). I need help with a glitch, when you hover over the menu
and then you go down to the drop down menu, then hover out and quickly hover back in it blinks like crazy.
I know an end user might not do that exactly but perhaps by accident they might end up seeing something horrible.
Reply
Kevin Liew Wed, 22nd June 2011 I can't replicate your problem :/.. could be the gap between top nav and sub menu, if yes, you might have to reduce it.
Richard Bonn Sat, 18th June 2011 Thank you for this easy to understand tutorial about jQuery. All of the tutorials in this series are so helpful. I especially like that you include HTML, CSS and Javascript.
Reply
Lacey Brown Wed, 8th June 2011 I enjoyed doing this tutorial. Thank you.
Reply
Dave Fri, 20th May 2011 Hi
This is great but I want the parent menu item to stay 'selected' when you hover over one of its submenu items. At the moment when you rollover the submenu items the parent item has returned to its old colour. Any ideas?
Reply
dave de Thu, 26th May 2011 Exactly! me too!

i have been searching for a lonnnnggg time to figure out how to do this from scratch
Kevin Liew Sun, 29th May 2011 There is one way to do it, put a class for all the submenu... and then:

$('ul.submenu').hover(

function () {
$(this).parent().addClass('active');
},
function () {
$(this).parent().removeClass('active');
}

);

of course, you will need to style the active class... I haven't tested it, hopefully it will work.
Dave Wed, 1st June 2011 thanks so much for putting this together. I've put the code in with the rest of the jquery code.
bit stuck as to how I add a class for all the submenu...? i know its in the css but is it a new class or part of the #nav ul class already there...? if you could put an example i would be truly grateful :)
Mike Tue, 14th June 2011 Hi Dave, I was having the same problem as you and I asked my brother about it and he told me to do this and it worked :)
css
ul.submenu {
}
.active{
color:#fc0 !important; /*
}

javascript
$(document).ready(function () {

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

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

function () {
$(this).parent().find('a').first().addClass('active');
},
function () {
$(this).parent().find('a').first().removeClass('active');
}

);

});

html
<ul id="nav">
<li><a href="#">Item1</a>
<ul class="submenu">
<li><a href="#">Item1.1<a/></li>
</ul>
</li>
</ul>

I hope this helps :)
steve Tue, 10th May 2011 Brilliant, nice and simple ! Just used it on my new flght case website. www.3d-flghtcase.co.uk
Reply
Thomas Duperre Tue, 10th May 2011 Need assistance. How can I, using this script, create a centered nav bar with submenus showing items both on top and on the the bottom?
Reply
Apple Tue, 3rd May 2011 thanks bro it really worked...........
Reply

Leave a comment

Have something to say? Drop a comment! No HTML tags are allowed in the comment textfield.

Advertisement