Create The Fanciest Dropdown Menu You Ever Saw

By | 153 comments | Web Development | Tags: , , , , , , , , , ,

 

Good evening web designers. I hope you have all had a great weekend, and are ready to start the working week tomorrow. I have something very special to share with you today. Before I begin, I want to remind you of a free website builder called Wix, using which you can create beautiful, professional websites. Now let’s continue…back in June when I wrote the post 21 Beautiful And Creative Navigation Menus, we had a comment from Brian Cray, in which he shared his menu with us. After speaking with Brian, he agreed to write a tutorial for us on how he created  his website navigation menu.

Recently he has changed his website design and the layout, but this tutorial is sweet, and one amazing navigation! Here is a bit about Brian…

About Brian…
Brian Cray writes a blog that helps web 2.0 professionals build better websites. Among other things, he is known for his development of Nearby Tweets and PXtoEM.com. Feel free to follow him on Twitter.

Be sure to check out some of the services that Brian provides, and if you have any questions for Brian feel free to drop in a comment and im sure he will reply.

What will you be building?

See the working demo

final

Contents…
  1. Making a basic dropdown menu that works
    1. The basic HTML
    2. The basic CSS
    3. The basic jQuery
  2. Making the basic menu look fancy
    1. The images
    2. The fancy HTML: What we’ve changed
    3. The fancy CSS: What we’ve added
    4. The fancy jQuery: What we’ve changed
  3. The final result with files

* * * * *

1. Making a basic dropdown that works

Before I begin – for those of you who want an easy way out of this tutorial, you can create a free  flash website with Wix that will look like a very professional website. Now for those who are still interested in going through with the tutorial, I will continue. Web designers and developers find themselves creating dropdown menus over and over. I’ve drilled dropdown menu production to a science. Seriously. Here’s the benefits of Brian Cray’s basic dropdown menu code:

  • Can be put anywhere on site in a matter of seconds
  • Forces no styling on the menu, but adapts to ANY styling
  • Can handle multiple dropdown levels gracefully
  • Covers variable-width tabs and dropdown menus
  • Semantic – only two classes needed on an otherwise plain UL

Here’s what the basic dropdown looks like

Let’s go over my dropdown menu code first as it provides the basis for the fanciest dropdown menu you ever saw.

The basic HTML

You’ve seen this kind of thing a million times. But take notice of the two UL classes: tabs and dropdown.

[html]<ul class=”tabs”>
<li><a href=”#”>Dropdown 1</a>
<ul class=”dropdown”>
<li><a href=”#”>Menu item 1</a>
<ul class=”dropdown”>
<li><a href=”#”>Submenu item 1</a></li>
<li><a href=”#”>Submenu item 1</a></li>
<li><a href=”#”>Submenu item 1</a></li>
</ul>
</li>
<li><a href=”#”>Menu item 2</a></li>
<li><a href=”#”>Menu item 3</a></li>
<li><a href=”#”>Menu item 4</a></li>
<li><a href=”#”>Menu item 5</a></li>
<li><a href=”#”>Menu item 6</a></li>
</ul>
</li>
<li><a href=”#”>Dropdown 2</a>
<ul class=”dropdown”>
<li><a href=”#”>Menu item 1</a></li>
<li><a href=”#”>Menu item 2</a></li>
<li><a href=”#”>Menu item 3</a></li>
<li><a href=”#”>Menu item 4</a></li>
<li><a href=”#”>Menu item 5</a></li>
<li><a href=”#”>Menu item 6</a></li>
</ul>
</li>
<li><a href=”#”>Dropdown 3</a>
<ul class=”dropdown”>
<li><a href=”#”>Menu item 1</a></li>
<li><a href=”#”>Menu item 2</a></li>
<li><a href=”#”>Menu item 3</a></li>
<li><a href=”#”>Menu item 4</a></li>
<li><a href=”#”>Menu item 5</a></li>
<li><a href=”#”>Menu item 6</a></li>
</ul>
</li>
</ul>[/html]

The tabs class makes the first UL a horizontal tab bar. The dropdown class makes the other UL dropdown menus. Simple, right?

The basic CSS

This CSS sets and resets the necessary styles so you don’t have to worry about your menu inheriting a bunch of crap that will make the menu look unexpected.

[css]/* tabs
*************************/

ul.tabs
{
display: table;
margin: 0;
padding: 0;
list-style: none;
position: relative;
}

ul.tabs li
{
margin: 0;
padding: 0;
list-style: none;
display: table-cell;
float: left;
position: relative;
}

ul.tabs a
{
position: relative;
display: block;
}

/* dropdowns
*************************/

ul.dropdown
{
margin: 0;
padding: 0;
display: block;
position: absolute;
z-index: 999;
top: 100%;
width: 250px;
display: none;
left: 0;
}

ul.dropdown ul.dropdown
{
top: 0;
left: 95%;
}

ul.dropdown li
{
margin: 0;
padding: 0;
float: none;
position: relative;
list-style: none;
display: block;
}

ul.dropdown li a
{
display: block;
}[/css]

The basic jQuery

The jQuery behind this is genius. It will pick up ANY dropdown class on the page and turn it into a working dropdown menu.

[js]$(function () {
$(‘.dropdown’).each(function () {
$(this).parent().eq(0).hover(function () {
$(‘.dropdown:eq(0)’, this).show();
}, function () {
$(‘.dropdown:eq(0)’, this).hide();
});
});
});[/js]

And that’s all you need to add to each project to create dropdown menus in a flash! Now lets take this concept a step farther and REALLY light up the show.

2. Making the basic menu look fancy

Here’s what the fancy menu looks like that we’ll create below

The first thing to do to make something fancy is to add colors and images.

The images

download

The fancy HTML: what we’ve changed

  • Wrapped everything in a #menu div
  • Added h4 elements to add another layer to the menu’s organization
  • Added a “hasmore” class to the menu items containing dropdown menus
  • Added a “last” class to the last menu item in dropdown menus
  • Added a <span> element around the main menu items

[html]<div id=”menu”>
<ul class=”tabs”>
<li><h4><a href=”#”>In the blog »</a></h4></li>
<li class=”hasmore”><a href=”#”><span>Recent</span></a>
<ul class=”dropdown”>
<li><a href=”#”>Menu item 1</a></li>
<li><a href=”#”>Menu item 2</a></li>
<li><a href=”#”>Menu item 3</a></li>
<li><a href=”#”>Menu item 4</a></li>
<li><a href=”#”>Menu item 5</a></li>
<li class=”last”><a href=”#”>Menu item 6</a></li>
</ul>
</li>
<li class=”hasmore”><a href=”#”><span>Topics</span></a>
<ul class=”dropdown”>
<li><a href=”#”>Topic 1</a></li>
<li><a href=”#”>Topic 2</a></li>
<li><a href=”#”>Topic 3</a></li>
<li class=”last”><a href=”#”>Topic 4</a></li>
</ul>
</li>
<li><a href=”#”><span><strong><img src=”images/feed-icon-14×14.png” width=”14″ height=”14″ alt=”RSS”> Subscribe to RSS</strong></span></a></li>
<li><h4><a href=”#”>Elsewhere »</a></h4></li>
<li><a href=”#”><span>About</span></a></li>
<li class=”hasmore”><a href=”/about/#networks”><span>Networks</span></a>
<ul class=”dropdown”>
<li><a href=”#”>Twitter</a></li>
<li><a href=”#”>posterous</a></li>
<li><a href=”#”>SpeakerSite</a></li>
<li><a href=”#”>LinkedIn</a></li>
<li class=”last”><a href=”#”>See more…</a></li>
</ul>
</li>
<li><a href=”#”><span>Bookmarks</span></a></li>
</ul>
</div>[/html]

The fancy CSS: what we’ve added

  • Added the * {margin:0; padding:0} simple reset hack (I know, there are better resets)
  • Styled to body
  • Removed underline from links
  • Styled the menu heavily

[css]/* hack reset (for production, use Yahoo! reset CSS)
*************************/

*
{
margin: 0;
padding: 0;
}

/* body
*************************/

body
{
font: 14px/21px Georgia, serif;
color: #370707;
background: #e3d79b url(images/mainbg.jpg) left 40px repeat-x;
}

/* links
*************************/

a:link, a:visited, a:hover, a:active
{
text-decoration: none;
}

/* inline elements
*************************/

strong
{
font-weight: bold;
}

/* menu-specifc
*************************/

#menu
{
position: fixed;
z-index: 5;
top: 0;
left: 0;
width: 100%;
height: 40px;
line-height: 40px;
background: #ffb35a url(images/topbg.gif) repeat-x;
border-bottom: 1px solid #000;
}

#menu ul
{
margin: 0 auto;
}

#menu ul li.hasmore
{
background: url(images/drophighlight.png) no-repeat center 27px;
}

#menu ul li h4
{
margin: 0;
}

#menu ul li h4 a
{
font-size: 14px;
color: #000;
font-weight: bold;
padding: 0 15px;
}

#menu ul li a
{
color: #9b2021;
padding-left: 4px;
}

#menu ul li a img
{
vertical-align: middle;
}

#menu ul li a:hover
{
background: url(images/topselectionleft.png) top left no-repeat;
}

#menu ul li a span
{
display: block;
padding: 0 15px 0 11px;
}

#menu ul li a:hover span
{
background: url(images/topselectionright.png) top right;
}

#menu ul.dropdown
{
padding: 10px;
background-image: url(images/dropdown.png);
overflow:hidden;
border-bottom: 1px solid #dc902f;
width: 290px;
}

#menu ul.dropdown li a
{
border-bottom: 1px solid #e5cd8e;
line-height: 30px;
overflow: hidden;
height: 30px;
}

#menu ul.dropdown li.last a
{
border-bottom-width: 0;
}

#menu ul.dropdown li a:hover
{
background: url(images/menuarrow.png) no-repeat left center;
}

#menu ul li h4 a:hover
{
background-image: none;
}[/css]

The fancy jQuery: what we’ve changed

  • Used the hoverIntent jQuery plugin by Brian Cherne instead of jQuery’s default hover to prevent unintended dropdown menus
  • Used the easing jQuery plugin by GSGD to add a visual effect to dropdown menu items
  • Changed the default dropdown menu show() and hide() functions to slideDown() and fadeOut() to add some pizzazz (without being annoying)
  • Preloaded the images necessary to make the menu happen

[js]$(function () {

$(‘.dropdown’).each(function () {
$(this).parent().eq(0).hoverIntent({
timeout: 100,
over: function () {
var current = $(‘.dropdown:eq(0)’, this);
current.slideDown(100);
},
out: function () {
var current = $(‘.dropdown:eq(0)’, this);
current.fadeOut(200);
}
});
});

$(‘.dropdown a’).hover(function () {
$(this).stop(true).animate({paddingLeft: ’35px’}, {speed: 100, easing: ‘easeOutBack’});
}, function () {
$(this).stop(true).animate({paddingLeft: ’0′}, {speed: 100, easing: ‘easeOutBounce’});
});

pic1 = new Image(310, 672);
pic1.src = “images/dropdown.png”;

pic2 = new Image(4, 40);
pic2.src = “images/dropselectionleft.png”;

pic3 = new Image(394, 40);
pic3.src = “images/dropselectionright.png”;

});[/js]

3. The final result with files

Congratulations! You’ve walked through creating the fanciest menu ever! By the way, if you feel frustrated with designing, or just want an easy way out, you can always create a free professional website with Wix.

Here are the files this menu uses:

Ultimate Designer Toolkit: 60,000+ Premium Design Items, Only $49!

READY TO TAKE WEB DESIGN SERIOUSLY?

Today, I am giving away Web Design Riches, the ultimate guide on how to run a profitable web design business. Enter your name and email below to get instant access.

Comments

153 Comments

  1. Chris (1 year ago)

    Hi, Its a great menu. I was wondering how it might be possible to make the menu title hover image remain when the drop down is displayed. Currently it only displays when you hover over the title. When you move the cursor onto the drop down, the title is no longer hi-lighted.

    Also, I’m concerned about the compatibility with safari, chrome etc. Does anyone here know how to fix those problems?

    • Jim (1 year ago)

      I am also interested in the hover. I am working on it now.

  2. conquerer_ni (1 year ago)

    Thank for the great tutorial.

    But it doesn’t seem to be working in IE 7.
    Can anybody please help me fix the issue or help me move in the right direction to get it fixed?

    I have installed debugbar and that doesn’t seems to even recognize the javasciprt file.

  3. tuck (1 year ago)

    Very Nice. But when I take the div id=menu and put that in a table cell. The image that display when the mouse hover over the links, does not align to the top of the table cell. Any figure out why is that?

  4. website design (1 year ago)

    thank you for yet again another great tutorial

  5. afors (1 year ago)

    very nice tutorial.

  6. Manju (1 year ago)

    Thanks for great tutorial

  7. Joe Bloggs (1 year ago)

    Nice, but what if you don’t want the menu at the very top of your website? Seems a bit inflexible.

  8. Joe Bloggs (1 year ago)

    Ignore my previous comment, I just removed position:fixed and was able to place it anywhere.

  9. manish singh (1 year ago)

    this is really awesome

  10. Greg (1 year ago)

    Works nicely for me on ie7 except that nested drop downs are a bit of a pain. When one moves one’s mouse from drop down level to nested drop down level the nested drop down level disappears too easily so that selecting an element in the nested drop down level is very difficult. Any workarounds for this?

  11. Website translator (1 year ago)

    The menu looks great, I will have to try it out even if some of the comments indicate that there might be potential problems with it.

  12. baby (1 year ago)

    thank you for the tutorials i think going to learn about website creation from you from now on

  13. Allan (1 year ago)

    Why does this script popup an active X warning? Is there anyway to get rid of that. Is that because of the settings on my browser? Will many people also have that problem? Thanks.

  14. Shruti (1 year ago)

    Hello I am making a site for a non profit organisation, just wanted to know if I can use the code u have given as is or even after me modifying it to fit in my needs.

    Awaiting your response.

    • Web Design (1 year ago)

      Hi Shruti,

      Feel free to modify it as you need : )

  15. Jonas (1 year ago)

    Doesn´t work.
    Subitem 1:3 apperas outside Imtem 3:1 to the right on FIREFOX when “moving the mouse slowly” on the drop down 1:2

  16. Sarah (1 year ago)

    Lovely! But I ran into something bizarre.
    IE (naturally) on 7 and 8, the drop downs don’t want to function when there is a .swf embedded on the page (using tag generated by Flash publish settings).

  17. College advice (1 year ago)

    The magics in the CSS! Thanks guys

  18. Cody (1 year ago)

    Same problem. This does not work with my jquery content slider. Drop down functionality quits. Solutions?? Desperate here…

  19. Sponsor a Child (1 year ago)

    Excellent resource. Thank you!

  20. vinodh (1 year ago)

    Hi,
    thank you for this. I am a serverside programmer.
    stuck with menu in jquery. Your tutorial solved my problem.
    regards
    vinodh

  21. deea (1 year ago)

    How could I use the symbol of that showing hand if I have only html/css on the website and a horizontal submenu, where and how should I put that photo ? Thank you.

  22. Scotty G (11 months ago)

    Will this make submenus open to the left if you are against the right side of the screen???

  23. Sarah (11 months ago)

    Hi, all! Does anyone know if this menu will function nicely on a Blackberry device?

  24. Sarah (11 months ago)

    Hi, me again!

    I noticed on mobile (specifically iPhone) that the menus will not close once they are open. Even if you click outside the menu or click back on the top-level item — they stay open!

    Does anyone know of a workaround on this?

    Sarah

  25. boshra (11 months ago)

    hi webdesgindev
    do you know that i really love you?
    i guess i read your site times a day. you r amazing. I send lots of positive energy for you each time i read your articles.
    tnxxxxxxxx

  26. hd desktop wallpapers (10 months ago)

    Hi there, this is really good tutorial for drop down. thanks for sharing it.

  27. Webdesign Twente (10 months ago)

    thanks for sharing this with us ! keep up the good work :)

  28. microsilic (10 months ago)

    great works and thanks

  29. John Kortis (9 months ago)

    Nice Menu, whenI move the css to it’s own subdir, it stops working

  30. mohit (9 months ago)

    very nice dropdown & nice ezeye tutorial Thanks

  31. Pg1creative (9 months ago)

    Great tut! It has helped me out of a hole.

    I do have a couple of questions though.

    I have found a way to align the drop down text to the left of the drop down box by adding {text-align: left} to the CSS but on hover over the text jumps to the middle. Any ideas how to stop this from happening?

    Also I need the menu to align to the right of my site. This means on some screens the drop down goes off the screen. Is there a way to move the drop down to the left of the main link rather than the right?

    Cheers

  32. Jacqueline Green (8 months ago)

    Hi there!

    I am having a hard time because I am using the slidesJS on the same page as this dropdown menu and so far, it will only give me one or the other, depending on how I have my external links listed. Is there anything that I can do about it? Here is a link to a discussion I started that has all my coding and everything! Thanks so much in advance to your help!

    —> https://groups.google.com/forum/?fromgroups=#!topic/slidesjs/B6PTFRWYDyw

    ps. I love this dropdown menu. ::)

  33. Breanna (8 months ago)

    Hmm, it didn’t work for me. -sad face-

  34. Danielle (6 months ago)

    Being fairly new to Javascript…..any idea why this wouldn’t be working in IE8?

  35. Ali (6 months ago)

    Very nice menu. Thanks for tutorial.

  36. vikas (5 months ago)

    Thankyou…very much..:)

  37. Women Abayas (3 months ago)

    Thanks for this great list, These are my all time favorites. 

  38. Michael (2 months ago)

    this works great, and I’ve been able to tweak it with CSS, but when I try to separate the jQuery1.3.4 from the other code, it doesn’t seem to work. Can you get it to play nice with jQuery 1.9?

  39. Web telorDesign (4 weeks ago)

    Good tutorial! Sarah asked:

    “I noticed on mobile (specifically iPhone) that the menus will not close once they are open. Even if you click outside the menu or click back on the top-level item — they stay open!”

    I have the same problem, any solution?

    Thank you,

  40. Rasmus (2 weeks ago)

    I was wondering the jQuery cod, i have never used it before and i don’t know, where to add it? In the HTML? Css? or a new?