<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Subooa Studios Blog &#187; dropdown</title>
	<atom:link href="http://blog.subooa.com/tag/dropdown/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.subooa.com</link>
	<description>Custom development by Chris Duell aka duellsy</description>
	<lastBuildDate>Wed, 28 Jul 2010 03:57:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Creating a Dropdown Menu with jQuery and CSS</title>
		<link>http://blog.subooa.com/development/html_and_css/creating-a-dropdown-menu-with-jquery-and-css/</link>
		<comments>http://blog.subooa.com/development/html_and_css/creating-a-dropdown-menu-with-jquery-and-css/#comments</comments>
		<pubDate>Mon, 06 Jul 2009 10:59:31 +0000</pubDate>
		<dc:creator>Chris Duell</dc:creator>
				<category><![CDATA[Ajax & Javascript]]></category>
		<category><![CDATA[HTML & CSS]]></category>
		<category><![CDATA[dropdown]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[menu]]></category>

		<guid isPermaLink="false">http://blog.subooa.com/?p=324</guid>
		<description><![CDATA[I often see requests for help on how to create a simple dropdown menu without requiring too much effort, and usually I direct people to an article for 'son of suckerfish', but since jQuery is in most sites I use now I have moved to using a jQuery based dropdown that is much simpler.]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-right: 10px; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.subooa.com%2Fdevelopment%2Fhtml_and_css%2Fcreating-a-dropdown-menu-with-jquery-and-css%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.subooa.com%2Fdevelopment%2Fhtml_and_css%2Fcreating-a-dropdown-menu-with-jquery-and-css%2F&amp;source=duellsy&amp;style=normal&amp;service=bit.ly&amp;hashtags=dropdown,jquery,menu" height="61" width="50" /><br />
			</a>
		</div>
<h3>The raw base</h3>
<p>To start off with, we&#8217;re going to at least need some sort of menu structure to get us going. For this, the best way to structure the menu is using an unordered list (ul), nesting your submenus as you go. Below is the example we&#8217;re going to use in this demo it its raw unstyled format. </p>
<p>(The running demo is here &#8211; <a href="http://blog.subooa.com/example/css_jquery_dropdown.html">http://blog.subooa.com/example/css_jquery_dropdown.html</a>)</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p324code1'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3241"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
</pre></td><td class="code" id="p324code1"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;!</span>DOCTYPE html <span style="color: #000000; font-weight: bold;">PUBLIC</span> <span style="color: #0000ff;">&quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;</span> <span style="color: #0000ff;">&quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>html<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span>head<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>title<span style="color: #339933;">&gt;</span>Creating a dropdown menu with jQuery and CSS<span style="color: #339933;">&lt;/</span>title<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;/</span>head<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;mainmenu&quot;</span><span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span>ul<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>li<span style="color: #339933;">&gt;&lt;</span>a href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;#&quot;</span><span style="color: #339933;">&gt;</span>Menu item <span style="color: #cc66cc;">1</span><span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;</span>ul<span style="color: #339933;">&gt;</span>
				<span style="color: #339933;">&lt;</span>li<span style="color: #339933;">&gt;&lt;</span>a href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;#&quot;</span><span style="color: #339933;">&gt;</span>Submenu item <span style="color: #cc66cc;">1</span><span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;&lt;/</span>li<span style="color: #339933;">&gt;</span>
				<span style="color: #339933;">&lt;</span>li<span style="color: #339933;">&gt;&lt;</span>a href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;#&quot;</span><span style="color: #339933;">&gt;</span>Submenu item <span style="color: #cc66cc;">1</span><span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;&lt;/</span>li<span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;/</span>ul<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;/</span>li<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>li<span style="color: #339933;">&gt;&lt;</span>a href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;#&quot;</span><span style="color: #339933;">&gt;</span>Menu item <span style="color: #cc66cc;">2</span><span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;&lt;/</span>li<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>li<span style="color: #339933;">&gt;&lt;</span>a href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;#&quot;</span><span style="color: #339933;">&gt;</span>Menu item <span style="color: #cc66cc;">3</span><span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;&lt;/</span>li<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>li<span style="color: #339933;">&gt;&lt;</span>a href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;#&quot;</span><span style="color: #339933;">&gt;</span>Menu item <span style="color: #cc66cc;">4</span><span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;</span>ul<span style="color: #339933;">&gt;</span>
				<span style="color: #339933;">&lt;</span>li<span style="color: #339933;">&gt;&lt;</span>a href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;#&quot;</span><span style="color: #339933;">&gt;</span>Submenu item <span style="color:#800080;">4.1</span><span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;&lt;/</span>li<span style="color: #339933;">&gt;</span>
				<span style="color: #339933;">&lt;</span>li<span style="color: #339933;">&gt;&lt;</span>a href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;#&quot;</span><span style="color: #339933;">&gt;</span>Submenu item <span style="color:#800080;">4.2</span><span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;&lt;/</span>li<span style="color: #339933;">&gt;</span>
			<span style="color: #339933;">&lt;/</span>ul<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;/</span>li<span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>li<span style="color: #339933;">&gt;&lt;</span>a href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;#&quot;</span><span style="color: #339933;">&gt;</span>Menu item <span style="color: #cc66cc;">5</span><span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;&lt;/</span>li<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;/</span>ul<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Opening that in a browser is going to be pretty plain, something like this:<br />
<img src="http://blog.subooa.com/wp-content/uploads/2009/07/raw_menu.jpg" alt="raw_menu" title="raw_menu" width="285" height="147" class="alignnone size-full wp-image-325" /></p>
<h3>A little css</h3>
<p>For the sake of this tutorial we&#8217;re only going to give the menu a basic look and not get too fancy. So we&#8217;ll just create a nice green and black menu.</p>
<p>The nav we are going to create is going to be a left to right manu, with the second level popping out below its parent, so we first need to order the items left to right and get the submenu it the right position. (To keep it simple, the style is going to be added to the same file and not extracted into a saperate stylesheet)</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p324code2'); return false;">View Code</a> CSS</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3242"><td class="line_numbers"><pre>6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
</pre></td><td class="code" id="p324code2"><pre class="css" style="font-family:monospace;">	&lt;style type<span style="color: #00AA00;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span><span style="color: #00AA00;">&gt;</span>
&nbsp;
		body<span style="color: #00AA00;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span><span style="color: #00AA00;">;</span>
			<span style="color: #000000; font-weight: bold;">font-family</span><span style="color: #00AA00;">:</span> Arial<span style="color: #00AA00;">,</span> Verdana<span style="color: #00AA00;">,</span> Helvetica<span style="color: #00AA00;">,</span> Sans-Serif<span style="color: #00AA00;">;</span>
		<span style="color: #00AA00;">&#125;</span>
&nbsp;
		<span style="color: #cc00cc;">#mainmenu</span> ul<span style="color: #00AA00;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">100%</span><span style="color: #00AA00;">;</span>
			<span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">30px</span><span style="color: #00AA00;">;</span>
			<span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#393</span><span style="color: #00AA00;">;</span>
			<span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span><span style="color: #00AA00;">;</span>
			<span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span><span style="color: #00AA00;">;</span>
			<span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span> <span style="color: #933;">12px</span><span style="color: #00AA00;">;</span>
		<span style="color: #00AA00;">&#125;</span>
			<span style="color: #cc00cc;">#mainmenu</span> ul li<span style="color: #00AA00;">&#123;</span>
				<span style="color: #000000; font-weight: bold;">float</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span>
				<span style="color: #000000; font-weight: bold;">list-style</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span><span style="color: #00AA00;">;</span>
				<span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span><span style="color: #00AA00;">;</span>
				<span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">relative</span><span style="color: #00AA00;">;</span>
				<span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">30px</span><span style="color: #00AA00;">;</span>
				<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">100px</span><span style="color: #00AA00;">;</span>
			<span style="color: #00AA00;">&#125;</span>
				<span style="color: #cc00cc;">#mainmenu</span> ul li a<span style="color: #00AA00;">&#123;</span>
					<span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">block</span><span style="color: #00AA00;">;</span>
					<span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span> <span style="color: #933;">10px</span><span style="color: #00AA00;">;</span>
					<span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">30px</span><span style="color: #00AA00;">;</span>
					<span style="color: #000000; font-weight: bold;">line-height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">30px</span><span style="color: #00AA00;">;</span>
					<span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#FFF</span><span style="color: #00AA00;">;</span>
					<span style="color: #000000; font-weight: bold;">text-decoration</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span><span style="color: #00AA00;">;</span>
					<span style="color: #000000; font-weight: bold;">border-right</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#363</span><span style="color: #00AA00;">;</span>
					<span style="color: #000000; font-weight: bold;">border-left</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#3c3</span><span style="color: #00AA00;">;</span>
				<span style="color: #00AA00;">&#125;</span>
&nbsp;
				<span style="color: #cc00cc;">#mainmenu</span> ul li ul<span style="color: #00AA00;">&#123;</span>
					<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">170px</span><span style="color: #00AA00;">;</span>
					<span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">absolute</span><span style="color: #00AA00;">;</span>
					<span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span><span style="color: #00AA00;">;</span>
					<span style="color: #000000; font-weight: bold;">float</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span>
					<span style="color: #000000; font-weight: bold;">clear</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span>
				<span style="color: #00AA00;">&#125;</span>
					<span style="color: #cc00cc;">#mainmenu</span> ul li ul li<span style="color: #00AA00;">&#123;</span>
						<span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#000</span><span style="color: #00AA00;">;</span>
					<span style="color: #00AA00;">&#125;</span>
						<span style="color: #cc00cc;">#mainmenu</span> ul li<span style="color: #6666ff;">.hover</span> ul li a<span style="color: #00AA00;">&#123;</span>
							<span style="color: #000000; font-weight: bold;">border-width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span><span style="color: #00AA00;">;</span>
							<span style="color: #000000; font-weight: bold;">border-bottom</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#CCC</span><span style="color: #00AA00;">;</span>
							<span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#000</span><span style="color: #00AA00;">;</span>
							<span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">29px</span><span style="color: #00AA00;">;</span>
							<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">150px</span><span style="color: #00AA00;">;</span>
						<span style="color: #00AA00;">&#125;</span>	
	&lt;/style<span style="color: #00AA00;">&gt;</span></pre></td></tr></table></div>

<p><img src="http://blog.subooa.com/wp-content/uploads/2009/07/style_no_js.jpg" alt="style_no_js" title="style_no_js" width="600" height="103" class="alignnone size-full wp-image-326" /></p>
<h3>Hiding the submenu</h3>
<p>Now that the menu is working and displaying as we want it to, now we need to hide the submenu by default and setup the style for the hover state.</p>
<p>Change the left value for the &#8220;#mainmenu ul li ul&#8221; class to be off the screen somewhere (we&#8217;re using -12000px here) and add a new hover class for the list item, which moves the ul back to where we just had it when the user hovers over the main menu item.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p324code3'); return false;">View Code</a> CSS</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3243"><td class="line_numbers"><pre>40
41
42
43
44
45
46
47
48
49
50
</pre></td><td class="code" id="p324code3"><pre class="css" style="font-family:monospace;">				<span style="color: #cc00cc;">#mainmenu</span> ul li ul<span style="color: #00AA00;">&#123;</span>
					<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">170px</span><span style="color: #00AA00;">;</span>
					<span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">absolute</span><span style="color: #00AA00;">;</span>
					<span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">:</span> <span style="color: #933;">-12000px</span><span style="color: #00AA00;">;</span>
					<span style="color: #000000; font-weight: bold;">float</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span>
					<span style="color: #000000; font-weight: bold;">clear</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span>
				<span style="color: #00AA00;">&#125;</span>
&nbsp;
					<span style="color: #cc00cc;">#mainmenu</span> ul li<span style="color: #6666ff;">.hover</span> ul<span style="color: #00AA00;">&#123;</span>
						<span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span><span style="color: #00AA00;">;</span>
					<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<h3>Adding the jQuery event&#8230; the fun part</h3>
<p>Up to this point, the file could run as a standalone HTML file, and with a little extra CSS could actually work in a lot of browsers, but there are many out there that don&#8217;t support using the &#8220;:hover&#8221; on list items, so we need to make use of some javascript to tell the browser to react when the user hovers over the main menu items. So first step here is to actually load the jQuery javascript:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p324code4'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3244"><td class="line_numbers"><pre>63
64
65
66
</pre></td><td class="code" id="p324code4"><pre class="javascript" style="font-family:monospace;">	<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js&quot;</span> type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
		jQuery.<span style="color: #660066;">noConflict</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>(The no conflict line has been added in cases where people may already be calling a javascript file that makes use of $, it prevents us from running into any javascript code conflicts)</p>
<p>And now here is where the magic happens, using jQuery you can &#8216;chain&#8217; function calls one after the other without having to do each function call on an element on its own line which cuts down the file size and keeps things neat and together.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p324code5'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3245"><td class="line_numbers"><pre>64
65
66
67
68
69
70
71
72
73
74
75
</pre></td><td class="code" id="p324code5"><pre class="javascript" style="font-family:monospace;">	<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
		jQuery.<span style="color: #660066;">noConflict</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		jQuery<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#mainmenu'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'li'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">mouseenter</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				jQuery<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'hover'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">mouseleave</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				jQuery<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">removeClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'hover'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Finally, just to top things off we need to add some hover colors to the menu items.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p324code6'); return false;">View Code</a> CSS</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3246"><td class="line_numbers"><pre>63
64
65
66
67
68
69
</pre></td><td class="code" id="p324code6"><pre class="css" style="font-family:monospace;">&nbsp;
				<span style="color: #cc00cc;">#mainmenu</span> ul li<span style="color: #6666ff;">.hover</span> a<span style="color: #00AA00;">&#123;</span>
					<span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#3c3</span><span style="color: #00AA00;">;</span>
				<span style="color: #00AA00;">&#125;</span>
				<span style="color: #cc00cc;">#mainmenu</span> ul li ul li<span style="color: #6666ff;">.hover</span> a<span style="color: #00AA00;">&#123;</span>
					<span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#333</span><span style="color: #00AA00;">;</span>
				<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p><img src="http://blog.subooa.com/wp-content/uploads/2009/07/final_dropdown_menu.jpg" alt="final_dropdown_menu" title="final_dropdown_menu" width="600" height="103" class="alignnone size-full wp-image-327" /></p>
<p>And that&#8217;s it, a fully functional CSS dropdown using jQuery. In a later article, I&#8217;ll cover adding a slight animation and also adding multiple levels of dropdowns.</p>
<h3>Demo and full code</h3>
<p>Here&#8217;s the running demo &#8211; <a href="http://blog.subooa.com/example/css_jquery_dropdown.html">http://blog.subooa.com/example/css_jquery_dropdown.html</a><br />
In case you got a little lost along the way, you can download the full code here to play with &#8211; <a href="http://blog.subooa.com/example/css_jquery_dropdown.zip">css_jquery_dropdown.zip</a></p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-bg-enjoy">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://blog.subooa.com/development/html_and_css/creating-a-dropdown-menu-with-jquery-and-css/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://blog.subooa.com/development/html_and_css/creating-a-dropdown-menu-with-jquery-and-css/&amp;title=Creating+a+Dropdown+Menu+with+jQuery+and+CSS" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://blog.subooa.com/development/html_and_css/creating-a-dropdown-menu-with-jquery-and-css/&amp;title=Creating+a+Dropdown+Menu+with+jQuery+and+CSS" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-diigo">
			<a href="http://www.diigo.com/post?url=http://blog.subooa.com/development/html_and_css/creating-a-dropdown-menu-with-jquery-and-css/&amp;title=Creating+a+Dropdown+Menu+with+jQuery+and+CSS&amp;desc=I%20often%20see%20requests%20for%20help%20on%20how%20to%20create%20a%20simple%20dropdown%20menu%20without%20requiring%20too%20much%20effort%2C%20and%20usually%20I%20direct%20people%20to%20an%20article%20for%20%27son%20of%20suckerfish%27%2C%20but%20since%20jQuery%20is%20in%20most%20sites%20I%20use%20now%20I%20have%20moved%20to%20using%20a%20jQuery%20based%20dropdown%20that%20is%20much%20simpler." rel="nofollow" class="external" title="Post this on Diigo">Post this on Diigo</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.google.com/buzz/post?url=http://blog.subooa.com/development/html_and_css/creating-a-dropdown-menu-with-jquery-and-css/&amp;imageurl=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://blog.subooa.com/development/html_and_css/creating-a-dropdown-menu-with-jquery-and-css/&amp;title=Creating+a+Dropdown+Menu+with+jQuery+and+CSS" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://blog.subooa.com/development/html_and_css/creating-a-dropdown-menu-with-jquery-and-css/&amp;title=Creating+a+Dropdown+Menu+with+jQuery+and+CSS" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-technorati">
			<a href="http://technorati.com/faves?add=http://blog.subooa.com/development/html_and_css/creating-a-dropdown-menu-with-jquery-and-css/" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=Creating+a+Dropdown+Menu+with+jQuery+and+CSS+-+http://bit.ly/2hGvd9&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

<script type="text/javascript">var wordpress_toolbar_urls = ["http:\/\/api.tweetmeme.com\/share?url=http%3A%2F%2Fblog.subooa.com%2Fdevelopment%2Fhtml_and_css%2Fcreating-a-dropdown-menu-with-jquery-and-css%2F","http:\/\/twitter.com\/home?status=Creating+a+Dropdown+Menu+with+jQuery+and+CSS+-+http:\/\/bit.ly\/2hGvd9&amp;source=shareaholic"];var wordpress_toolbar_url = "";var wordpress_toolbar_oinw = "oinw";var wordpress_toolbar_hash = "aHR0cDovL2Jsb2cuc3Vib29hLmNvbS9kZXZlbG9wbWVudC9odG1sX2FuZF9jc3MvY3JlYXRpbmctYS1kcm9wZG93bi1tZW51LXdpdGgtanF1ZXJ5LWFuZC1jc3MvPHdwdGI%2BQ3JlYXRpbmcgYSBEcm9wZG93biBNZW51IHdpdGggalF1ZXJ5IGFuZCBDU1M8d3B0Yj5odHRwOi8vYmxvZy5zdWJvb2EuY29tPHdwdGI%2BU3Vib29hIFN0dWRpb3MgQmxvZw%3D%3D";</script>]]></content:encoded>
			<wfw:commentRss>http://blog.subooa.com/development/html_and_css/creating-a-dropdown-menu-with-jquery-and-css/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
