Register now or login here to start promoting your blog and your favourite articles.
Easy to Style jQuery Drop Down Menu Tutorial
10 Nov 2009 - 27 Comments
Drop down menu is always the most famous solution in navigation menu. This tutorial will show us how to create one and also to keep it as simple as possible so that it can be styled easily.
Author: kevin | Source: queness
Goin Nutty
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.

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

Copyright & Usage

The effects and techniques demonstrated in tutorials on Queness can be used in whatever manner you wish without attribution. You cannot copy whole tutorials (unless permission is given), either in English or translated to another language.

Share This Post to Support Me! :)


Comments

NBA Jerseys on 24 Feb 2010 says:
http://www.nbajs.com NBA Jerseys
Kartlos Tchavelachvili on 18 Feb 2010 says:
Sweet! Thanks :)
nagarajan on 16 Feb 2010 says:
nice tutorial, now i learned how dropdown menus are working thanks lot
Matt on 4 Feb 2010 says:
Hi Kevin, I'm using FF3/IE7/8 and I got the same flickering problem when hovering a drop down menu, it happends on your demonstration as well, simply hover a LI and go away quickly and come back.
Can you please find a fix. WOuld be highy appreciated!
CsS RoCkeR on 26 Jan 2010 says:
plz send me online jquery.js script link.
than i am copy & past in head.
kevin on 26 Jan 2010 says:
Hi Hector... u need to created a CSS class that highlight the parent item. And also you will need to use server side language (php,asp) to add that class to the parent.. depend on which page user is visiting... hope it make sense...
Hector on 26 Jan 2010 says:
Hello
Your tutorial is really good, but I have one troble. I succed to make the submenu items to chanche colors but I want the parents change color and satay as I explore the submenu.
Sorry for my English.
chau
Wah on 25 Jan 2010 says:
Hello Kevin,i have the same problem like Andy,also try your bug fix for Agilius,but still have the problem.When you quick hover to the button it will flicking no stop,do you have any bug fix for this?thanks!
kevin on 23 Jan 2010 says:
Hi Andy, I can't seem to regenerate the bug u mentioned above... can i know what browser are you using?
Andy on 22 Jan 2010 says:
This is pretty good, but it still has a major bug. If you scroll off the menu while it's closing and then quickly scroll back on the closing portion of the menu, it flickers non-stop until you mouse off. I can see that being a pretty visible issue, especially in cases where you have two menu items that have a number of subcategories. Any ideas of how to fix that?

Leave a comment

Subscribe RSS Subscribe RSS, Keep yourself updated with queness latest posts!
Pixel Crayons Really learn it! Digging into Wordpress

Buy wholesale computers directly from China at DHgate.com

Discover the tools to build your e-Commerce Site with Netfirms

  •  
  •  
  •  
  •  
  •  

Community News

Recent Comments

Random Posts


View all posts and news Back to top

About the Author

A web designer and developer who is passionate and keen on contributing to the web development industry. Feel free to say hi to me, or follow me on twitter.

Kevin Liew

Partner

  • Web and Designers
  • CSS Style
  • PV.M Garage Blogzine - (Italian)
  • TutsValley
  • Designrfix
  • CoolVibe
  • Web Developer Juice
  • Denbagus
  • Web Hosting Secret Revealed
  • PSD to HTML Conversion
  • BlueHost