[CSS3] jQuery Dropdown Navigation in WordPress

Today, we will learn how to enhance you WordPress in a whole new way. Multi-Level or Multi-Dimensional navigation menus can offer your theme and users 2 new things. One, add a nice new type of effect to enhance your theme. Two, allow the users to find things more easily. We will start off by making a HTML version, then making it compatible with WordPress.

Beginning the Script

At the beginning of every new project it’s good to have a good basic setup to work with. I prefer to have the basic elements every page needs, head and body, along with importing one stylesheet and the latest jQuery.


		<!-- Title -->

		<!-- Meta -->

		<!-- Javascript -->
		<script src=" jquery-latest.min.js" type="text/javascript"><!--mce:0--></script>

		<!-- Styles -->

		<!-- Content -->

Starting the Navigation

Before we dive into WordPress, in this tutorial or in any case, you want to make a working HTML version first. First, we are going to open up a header div width a unordered list inside of it that will end up being are navigation menu. You can give that unordered list a class or id but i am choosing not to for this tutorial. Next, fill the unordered list with about 4 list items (make sure the text is nested in anchor tags).


		<!-- Title -->

		<!-- Meta -->

		<!-- Javascript -->
		<script src=" jquery-latest.min.js" type="text/javascript"><!--mce:1--></script>

		<!-- Styles -->
<div id="header">
<ul>
	<li><a href="#">list item</a></li>
	<li><a href="#">list item</a></li>
	<li><a href="#">list item</a></li>
	<li><a href="#">list item</a></li>
</ul>
</div>

Styling the Top Level List

Now, let’s style that basic top level list. We want our list items aligned horizontally, and of course centered in the middle of the page. We will also give the header a nice subtitle gradient using CSS3.

/* Basic Reset */
* { margin: 0; padding:0; }
body {
	font: small-caps 14px Tahoma;
	position: relative;
}
a {
	text-decoration: none;
}

/* Header */
#header {
	background: -webkit-gradient(
		linear,
		left top,
		left bottom,
		color-stop(0, rgb(117,18,41)),
		color-stop(1, rgb(79,7,24))
	);
	height: 86px;
}
/* Top Level Menu */
#header ul {
	margin: 0 auto;
	width: 400px;
}

#header ul li {
	display: block;
	float: left;
	margin: 40px 0 0;
	width: 100px;
}

#header ul li a {
	color: #FFF;
	text-shadow: 1px 1px 1px rgba(0, 0, 0, 0.5);
}

Adding the Next Level

Here is where we separate this navigation menu from most; we will add another list of items under each current item so there are submenus when hovered. On the list items you want to have sub menus you will need to open a new unordered list before the parent closing list item, like so:


		<!-- Title -->

		<!-- Meta -->

		<!-- Javascript -->
		<script src=" jquery-latest.min.js" type="text/javascript"><!--mce:2--></script>

		<!-- Styles -->
<div id="header">
<ul>
	<li><a href="#">list item</a></li>
	<li><a href="#">list item</a></li>
	<li><a href="#">list item</a></li>
	<li><a href="#">list item</a></li>
	<li><a href="#">list item</a></li>
	<li><a href="#">list item</a></li>
	<li><a href="#">list item</a></li>
	<li><a href="#">list item</a></li>
	<li><a href="#">list item</a></li>
	<li><a href="#">list item</a></li>
</ul>
</div>

jQuery Animation Time

Now, time for the fun part! Before we style those sub level menus we want to be able to see the content they hold and be able to make them appear on hovering the parent. To do this, we will use jQuery’s .hover() function.

$(function(){

	//Hide SubLevel Menus
	$('#header ul li ul').hide();

	//OnHover Show SubLevel Menus
	$('#header ul li').hover(
		//OnHover
		function(){
			//Hide Other Menus
			$('#header ul li').not($('ul', this)).stop();

			//Add the Arrow
			$('ul li:first-child', this).before(
				'
	<li class="arrow">arrow</li>
'
			);

			//Remove the Border
			$('ul li.arrow', this).css('border-bottom', '0');

			// Show Hoved Menu
			$('ul', this).slideDown();
		},
		//OnOut
		function(){
			// Hide Other Menus
			$('ul', this).slideUp();

			//Remove the Arrow
			$('ul li.arrow', this).remove();
		}
	);

});

Styling the Sub Level Menus

Now, just add this bit of CSS to your CSS file to make your sub level menus look all nice and pretty. Its pretty straightforward.

/* Sub Level Nav */
#header ul li ul {
	background: #F4F4F4;
	border: 0px solid #000;
		-webkit-border-radius: 5px;
		-moz-border-radius: 5px;
	-webkit-box-shadow: 0px 0px 3px rgba(0, 0, 0, 0.5);
	margin: 10px 0 0 -46px;
	padding: 0 10px 0;
	position: relative;
	width: 126px;
}

#header ul li ul li {
	border-bottom: 1px solid #CCC;
	display: block;
	float: none;
	height: 14px;
	padding: 8px 0;
	text-align: center;
	width: 126px;
	margin: 0;
}

#header ul li ul li a {
	color: #620d20;
	text-shadow: none;
}

#header ul li ul li a:hover {
	color: #000;
}

#header ul li ul li:last-child:not(li.arrow) {
	border: 0;
}

/* Arrow */
.arrow {
	background: url(arrow.png) no-repeat;
	border: 0;
	display: none;
	position: absolute;
		top: -10px;
		left: 63px;
	height: 11px;
	width: 20px;
	text-indent: -9999px;
}

Making it Work with WordPress

The Admin Panel

The items for the menu are going to be pages, so when you create your pages the ones you want to be the top level menu items make them have no parent. The items you want under a main item and appear on hover give them the parent of said element. The field to give a page parent is located in the right sidebar during page creation.

The PHP

Now, making this work with WordPress is very easy. All you need to do is include the CSS and JavaScript with your current theme and then do the following with your header.php file.

<div id="header">
<ul>
	<li><a href="&lt;?php bloginfo('url'); ?&gt;">Home</a></li>

 
</ul>
</div>

Final Thoughts

Of course this isn’t a perfect multi-level navigation; it is an awesome start to making your own. This will only look good in WebKit and Mozilla browsers, it’s a feature only meant for them at the current time. If you have IE, switch to a WebKit or Mozilla browser to view the end result. Both the wordpress and html version are located inside the source files, enjoy them and we hope you’ll learn from it.

Themify WordPress Themes

This entry was posted on Monday, March 22nd, 2010 at 9:30 am and is filed under Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Cody Robertson is a 19 year old freelancer. He is currently attending university, but loves to write and create beautiful websites whenever he can. He ranges with a large spectrum of coding languages, all the way from Objective-C to HTML. Follow Cody on Twitter

About the author Published by Cody Robertson

29 Responses »

  1. Thank you! I might use it in my future WordPress website! :)

  2. ok great tutorial , and I’ll leave comment for respect

  3. There is a bug…
    When you pass the dropdown menu item a couple of times, it continues animating…

    If you move quickly 5 times over the button, it will animate 5 times… Is there a fix for it?

  4. Any reason you did not nest your tags properly? I mean going right into another LI without first opening a UL?

    Also, you should probably add a stop before you animate. When you mouse in and out rapidly, you are forced to wait for animation.

  5. @SD and @Matt

    We will try to find a solution for this ASAP.

  6. @SD I will look into it.

    @Matt It has it on my local version. I must have messde up while typing this tutorial. When using the WordPress code

  7. Nice tut.
    Ive found that the arrow element gets in the way of the first link in the list.
    Is there a way to make the arrow li smaller and less obtrusive?

    Thanks.

  8. Found a solution for the arrow blocking the first link in the list.
    Just add z-index: -100; to the .arrow {} css element

  9. Could you please help me with one error im getting with this tutorial.
    Im getting a border on the left hand side of the .arrow element when the list animates up.

    You could please let me know how to remove this.
    My menu can be seen here: http://www.unit23skatepark.co.uk
    Mouse over the team or contact links.

    Thanks in advance.
    Chris.

  10. Tried the z-index fix above but it doesnt seem to do anything. Only way I found to get over the problem is to make the .arrow class more specific so that it overwrites the standard #header ul li ul li styles rather than defaulting to them.

    Change the .arrow selector to #header ul li ul li.arrow. Then on the last line add padding-bottom: 0; and you shouldnt have the arrow obscuring the link.

  11. Awesome tut … haven’t seen a simpler drop-down-jquery-tut anywhere.
    Note: does not work if you use WordPress’s own “jquery.js” from the includes directory.

  12. not working on localhost wordpress theme that is being developed :(

  13. Is there any way to center the submenu, because if you have a menu item with the name of Portfolio, the submenu is not centered.

Trackbacks

  1. You are now listed on FAQPAL
  2. This Weeks Twitter Design News Roundup N.30 - Speckyboy Design Magazine
  3. 45+ Fresh Wordpress Tutorials, Techniques and Hacks - Speckyboy Design Magazine
  4. 45+ Fresh Wordpress Tutorials, Techniques and Hacks · rogdykker
  5. 45+ Fresh Wordpress Tutorials, Techniques and Hacks | EMDMA
  6. 45+ Fresh Wordpress Tutorials, Techniques and Hacks | WebDesign Collection
  7. Best of the Web: First Quarter | Site Design Tips
  8. Best of the Web: First Quarter | Best Web Magazine
  9. Best of the Web: First Quarter | jiggymotto.com
  10. 45+ Fresh Wordpress Tutorials « MoeMir
  11. WordPressハッカーズ
  12. 25 Best Examples of jQuery and CSS Drop-down Menu Tutorials

Leave a Response

We do love friendly, well-constructed and respective comments. We do not love bitchy, stupid and disrespectful comments. Find something wrong in this post?, feel free to send us a note and we will fix the issue asap.

About tuttoaster.com

TutToaster

Tuttoaster is a growing community where enthusiasts and professionals alike can find free tutorials on a wide variety of web-based subjects.

Photoshop, Illustrator, website coding and SEO are just a few of the topics. Not only will you see the finished product, but you’ll also learn the techniques needed to produce the final results – all laid out in an easy-to-use, easy-to-understand and, best of all, free, way.

Recent Comments

  • Cody Robertson on - Creating a Upload...Well, it depends on your current download system. Letting them execute there HTML/JavaScript/PHP fil
  • Pritesh on - Creating a Upload...Great coincidence, On 31 August I managed to create my own upload website. Since I'm new to progra
  • Cody Robertson on - CSS jQuery Dropdown...All the menus should be centered exactly. Please first try and rework your code to resemble the fina