Creating a Drop Down Navigation Menu


- Post was last modified on July 23rd, 2009.
post-thumb

Every tutorial I've done on navigation bars or menu's has been the normal one level navigation bar, but when you build a website you may feel the need to create sub categories, for example my blog houses various tutorials which are stored in categories, I have a drop down navigation bar on the main link, which is tutorials which then displays sub catergories and then the specific catergory e.g. Tutorials > XHTML > Basics in that example there are three levels. You can create navigation bar levels using lists, it's the easiest way to create a drop down menu but for styling it can get a bit tricky, but in this tutorial Im going to show you how to form a drop down navigation menu and then style it with CSS

Forming a basic navigation with lists

First off all were actually going to form the drop down menu itself, forming the drop down menu isn't that hard it's styling the lists itself that can get confusing. Were going to start off by creating a basic navigation which is just one level

<ul>
<li><a href="#">Link 1</a></li>
<li><a href="#">Link 2</a></li>
<li><a href="#">Link 3</a></li>
<li><a href="#">Link 4</a></li>
<li><a href="#">Link 5</a></li>
<li><a href="#">Link 6</a></li>
<li><a href="#">Link 7</a></li>
</ul>

This is a basic one level navigation with no styling, but formed by a list (unordered list to be exact) it could be formed with divs but it would get very messy and really it's all about keeping it simple. Lets introduce a couple of drop down menu's, look carefully at how the list is formed with the drop downs added in. At first it looks really odd, but hopefull you'll see why it's formed the way it is.

Introducing drop down menus

<ul>
 <li><a href="#">Home</a></li>
 <li><a href="#">About</a></li>
 <li><a href="#">Contact</a></li>
 <li><a href="#">Articles</a></li>
 <li><a href="#">Scripts</a>
  <ul>
    <li><a href="#">Drop Down Link 1</a></li>
    <li><a href="#">Drop Down Link 2</a></li>
    <li><a href="#">Drop Down Link 3</a></li>
    <li><a href="#">Drop Down Link 4</a></li>
    <li><a href="#">Drop Down Link 5</a></li>
  </ul>
 </li>
  <li><a href="#">Tutorials</a>
   <ul>
    <li><a href="#">Drop Down Link 1</a></li>
    <li><a href="#">Drop Down Link 2</a></li>
    <li><a href="#">Drop Down Link 3</a></li>
    <li><a href="#">Drop Down Link 4</a></li>
    <li><a href="#">Drop Down Link 5</a></li>
  </ul>
 </li>
  <li><a href="#">Links</a></li>
</ul>

This addition has added a drop down level to the scripts and tutorial links. You have to add a whole new list just before the closing </li> tag of the first level link. It's important that your lists are closed correctly otherwise you won't be using valid XHTML and your navigation will just fall apart, quite literally. If you placed this code into a webpage and then viewed it you will have what you call a navigation tree. Which will look like this:

Navigation Tree (Second Level)


Navigation Tree

Adding a third level

Okay so hopefully you understand the whole navigation tree thing and how the drop down menus work. So far we have a two level navigation, but we are not limited to two levels we can add a third level to a second level drop down taking the second level navigation addition we can add a third level to one or more of the second levels:

<ul>
 <li><a href="#">Home</a></li>
 <li><a href="#">About</a></li>
 <li><a href="#">Contact</a></li>
 <li><a href="#">Articles</a></li>
 <li><a href="#">Scripts</a>
  <ul>
    <li><a href="#">Drop Down Link 1</a></li>
    <li><a href="#">Drop Down Link 2</a></li>
    <li><a href="#">Drop Down Link 3</a></li>
    <li><a href="#">Drop Down Link 4</a></li>
    <li><a href="#">Drop Down Link 5</a></li>
  </ul>
 </li>
  <li><a href="#">Tutorials</a>
   <ul>
    <li><a href="#">Drop Down Link 1</a>
        <ul>
         <li><a href="#">Third Level Link 1</a></li>
         <li><a href="#">Third Level Link 2</a></li>
         <li><a href="#">Third Level Link 3</a></li>
        </ul>
      </li>
    <li><a href="#">Drop Down Link 2</a>
        <ul>
         <li><a href="#">Third Level Link 1</a></li>
         <li><a href="#">Third Level Link 2</a></li>
         <li><a href="#">Third Level Link 3</a></li>
       </ul>
     </li>
    <li><a href="#">Drop Down Link 3</a>
        <ul>
         <li><a href="#">Third Level Link 1</a></li>
         <li><a href="#">Third Level Link 2</a></li>
         <li><a href="#">Third Level Link 3</a></li>
       </ul>
     </li>
    <li><a href="#">Drop Down Link 4</a>
        <ul>
         <li><a href="#">Third Level Link 1</a></li>
         <li><a href="#">Third Level Link 2</a></li>
         <li><a href="#">Third Level Link 3</a></li>
       </ul>
     </li>
    <li><a href="#">Drop Down Link 5</a>
        <ul>
         <li><a href="#">Third Level Link 1</a></li>
         <li><a href="#">Third Level Link 2</a></li>
         <li><a href="#">Third Level Link 3</a></li>
       </ul>
      </li>
  </ul>
 </li>
  <li><a href="#">Links</a></li>
</ul>

This now makes each link on the second level of the tutorials link have a third level. The navigation tree changes slightly:

Navigation Tree (Third Level)


Navigation Tree

Okay so I've shown you how to form a drop down menu with various levels but now's time to style the list. This is probably the more harder part to actually do so I'll break it down in steps.

Adding a class to the list

First of all we need to create one class to style the actual navigation bar before we start messing with any lists. I created the class dropdown and added a div around the list code we've been using:

<div class="dropdown">
 <ul>
  <li><a href="#">Home</a></li>
  <li><a href="#">About</a></li>
  <li><a href="#">Contact</a></li>
  <li><a href="#">Articles</a></li>
  <li><a href="#">Scripts</a>
   <ul>
     <li><a href="#">Drop Down Link 1</a></li>
     <li><a href="#">Drop Down Link 2</a></li>
     <li><a href="#">Drop Down Link 3</a></li>
     <li><a href="#">Drop Down Link 4</a></li>
     <li><a href="#">Drop Down Link 5</a></li>
  </ul>
 </li>
  <li><a href="#">Tutorials</a>
   <ul>
    <li><a href="#">Drop Down Link 1</a>
        <ul>
         <li><a href="#">Third Level Link 1</a></li>
         <li><a href="#">Third Level Link 2</a></li>
         <li><a href="#">Third Level Link 3</a></li>
        </ul>
      </li>
    <li><a href="#">Drop Down Link 2</a>
        <ul>
         <li><a href="#">Third Level Link 1</a></li>
         <li><a href="#">Third Level Link 2</a></li>
         <li><a href="#">Third Level Link 3</a></li>
       </ul>
     </li>
    <li><a href="#">Drop Down Link 3</a>
        <ul>
         <li><a href="#">Third Level Link 1</a></li>
         <li><a href="#">Third Level Link 2</a></li>
         <li><a href="#">Third Level Link 3</a></li>
       </ul>
     </li>
    <li><a href="#">Drop Down Link 4</a>
        <ul>
         <li><a href="#">Third Level Link 1</a></li>
         <li><a href="#">Third Level Link 2</a></li>
         <li><a href="#">Third Level Link 3</a></li>
       </ul>
     </li>
    <li><a href="#">Drop Down Link 5</a>
        <ul>
         <li><a href="#">Third Level Link 1</a></li>
         <li><a href="#">Third Level Link 2</a></li>
         <li><a href="#">Third Level Link 3</a></li>
       </ul>
      </li>
  </ul>
 </li>
  <li><a href="#">Links</a></li>
</ul>
</div>

Okay so now we have that class defined we can now begin styling.

Styling the Navigation base

Before anything else we need to style the navigation bar, we don't need to style anything to do with the list we need actually style the class we added to the list code:

/* Navigation Style */

.dropdown {  font-family: arial, sans-serif; position:relative; width:100%; height:50px; border:1px solid #666666; font-size:14px; color:#ffffff; background:#333333; }

Basic List styling

We now need to style the basic list attributes:

/* Basic List Styling (First/Base Level) */

.dropdown ul {padding:0; margin:0; list-style: none;}

.dropdown ul li {float:left; position:relative;}

.dropdown ul li a { border-right:1px solid #666666; padding:17px 17px 17px 17px; display:block; text-decoration:none; color:#000; text-align:center; color:#fff;}

.dropdown ul li a:hover {color:#ffffff; background:#232323;}

This part is important as it is the foundation for the drop downs to work off.

Styling the Drop Down Menus (Second Level)

Now it's time to style the second level drop down menus.


/* Second Level Drop Down Menu */

.dropdown ul li ul {display: none;}

.dropdown ul li:hover ul { font-size:13px; display:block; position:absolute; top:51px; min-width:150px; left:0;}

.dropdown ul li:hover ul li a {display:block; background:#000; color:#ffffff; width:110px; }

.dropdown ul li:hover ul li a:hover {background:#666666; color:#ffffff;}

Removing the :hover parts will break the navigation bar, they are there so the drop down menu only appears when the someones mouse hovers over a drop down link, removing it will menu it will constantly display so don't remove it.

Styling the Drop Down Menus (Third Level)

I said your not limited to just two levels so here's the styling for the third:

/* Third Level Drop Down Menu */

.dropdown ul li:hover ul li ul {display: none;}

.dropdown ul li:hover ul li:hover ul { display:block; position:absolute; left:145px; top:0; }

Explaining all of that

It would be a waste of time for me to just give you the code and say here look, great drop down navigation bar, it's no use really if you don't explain the whole code behind it. Styling the lists is done by signifying which list level you are styling:

.dropdown ul - First Level List
.dropdown ul li - First Level List Item

.dropdown ul li ul - Second Level List
.dropdown ul li ul li - Second Level List Item

.dropdown ul li ul li ul - Third Level List
.dropdown ul li ul li ul li - Third Level List Item

In some cases the third level list item is not needed either because there is no third level, or you don't need to change anything and want it styled just like the second level drop down. Like I said the styling is the tricky part but hopefully from breaking the various levels down into sections has helped you understand in a more clearer way.

Put it all together

Put everything together and we get this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>Drop Down Navigation</title>

<style type="text/css">

/* Navigation Style */

.dropdown { position:relative; font-family: arial, sans-serif; width:100%; height:50px; border:1px solid #666666; font-size:14px; color:#ffffff; background:#333333; }

/* Basic List Styling (First/Base Level) */

.dropdown ul {padding:0; margin:0; list-style: none;}

.dropdown ul li {float:left; position:relative;}

.dropdown ul li a { border-right:1px solid #666666; padding:17px 17px 17px 17px; display:block; text-decoration:none; color:#000; text-align:center; color:#fff;}

.dropdown ul li a:hover {color:#ffffff; background:#232323;}

/* Second Level Drop Down Menu */

.dropdown ul li ul {display: none;}

.dropdown ul li:hover ul { font-size:13px; display:block; position:absolute; top:51px; min-width:150px; left:0;}

.dropdown ul li:hover ul li a {display:block; background:#000; color:#ffffff; width:110px; }

.dropdown ul li:hover ul li a:hover {background:#666666; color:#ffffff;}

/* Third Level Drop Down Menu */

.dropdown ul li:hover ul li ul {display: none;}

.dropdown ul li:hover ul li:hover ul { display:block; position:absolute; left:145px; top:0; }

</style>

</head>

<body>
<div class="dropdown">
<ul>
  <li><a href="#">Home</a></li>
  <li><a href="#">About</a></li>
  <li><a href="#">Contact</a></li>
  <li><a href="#">Articles</a></li>
  <li><a href="#">Scripts</a>
   <ul>
     <li><a href="#">Drop Down Link 1</a></li>
     <li><a href="#">Drop Down Link 2</a></li>
     <li><a href="#">Drop Down Link 3</a></li>
     <li><a href="#">Drop Down Link 4</a></li>
     <li><a href="#">Drop Down Link 5</a></li>
  </ul>
 </li>
  <li><a href="#">Tutorials</a>
   <ul>
    <li><a href="#">Drop Down Link 1</a>
        <ul>
         <li><a href="#">Third Level Link 1</a></li>
         <li><a href="#">Third Level Link 2</a></li>
         <li><a href="#">Third Level Link 3</a></li>
        </ul>
      </li>
    <li><a href="#">Drop Down Link 2</a>
        <ul>
         <li><a href="#">Third Level Link 1</a></li>
         <li><a href="#">Third Level Link 2</a></li>
         <li><a href="#">Third Level Link 3</a></li>
       </ul>
     </li>
    <li><a href="#">Drop Down Link 3</a>
        <ul>
         <li><a href="#">Third Level Link 1</a></li>
         <li><a href="#">Third Level Link 2</a></li>
         <li><a href="#">Third Level Link 3</a></li>
       </ul>
     </li>
    <li><a href="#">Drop Down Link 4</a>
        <ul>
         <li><a href="#">Third Level Link 1</a></li>
         <li><a href="#">Third Level Link 2</a></li>
         <li><a href="#">Third Level Link 3</a></li>
       </ul>
     </li>
    <li><a href="#">Drop Down Link 5</a>
        <ul>
         <li><a href="#">Third Level Link 1</a></li>
         <li><a href="#">Third Level Link 2</a></li>
         <li><a href="#">Third Level Link 3</a></li>
       </ul>
      </li>
  </ul>
 </li>
  <li><a href="#">Links</a></li>
</ul>
</div>
</body>
</html>

Bug Fix:

Thanks to Jamie who emailed me about a certain problem with the navigation bar I have changed the code round a bit to fix a certain bug which when you changed the width nothing would happen to the drop down box sizes, but instead the links would start going onto one line. There was also a problem with when text was different lengths on the same drop down.

Please update your code (If you've used this already) and use the following method to change widths:

min-width:150px; (.dropdown ul li:hover ul) - This is the minimum width for the actual drop down links, you shouldn't change this value to expand the drop down menus anymore.

width:110px; (.dropdown ul li:hover ul li a) - You should now use this width to control the size of the drop down menus. By Default this will make the drop down links align to the center, however you can overide this by using text-aling:left; on the same line the width is set.

Again thanks to Jamie Hudson for submitting this problem to me and advising me on this fix.

Bug Fix 2:

I've had a few emails from people that have had problems with the navigation links being everywhere and completly out of line I found that this is because people have been added a width to the .dropdown ul li line this can not be done as it will make every other list out of line, so instead you can simply change the padding setting on the .dropdown ul li a people have been wanting to change the gap between the links so if this is the case you will need to change these padding parts padding:17px 17px 17px 17px; they will need to be changed equally otherwise your navigation base links won't align.

Bug Fix 3:

Thanks for Steve M, who commeted on the tutorial he provided a fix for IE7 issues where the navigation drop down would keep disappearing to quickly, all you need to do is add a height to the .dropdown ul li:hover ul li a line, this should fix problems with drop downs disappearing too quickly. In IE7

Well thats how to create a drop down menu. It's basic I know, there are many ways to extend the functionality of this navigation bar e.g. jQuery but thats for another tutorial! I hope this was useful!

Preview the Drop down navigation menu

Note: Someone recently sent me there code and reported it wasn't working in IE7. This is because they were using the Strict Doctype, in the example I used the Transitional doctype which does work in IE7 and IE8. But the strict doctype does not work for IE7 (And possiblly IE8 too)

60 Reader Comments, Why not join the party?

  •  
  • James
    August 13th

    Did your e key on your keyboard get stuck or something Dave?! Thanks for your comment. The spacing doesn't help I know but also because the basic method of :hover is used to create the drop downs that probably doesn't help either. I am going to do a version two of this tutorial and improve parts that have been highlighted.

  • Zach
    September 8th

    Hey! Thanks James for all the tutorials. I am brand new to website design, and I have been learning a lot from you. Your effort in putting out these post have been a huge help to me.

    I really like this post and I decided to scratch my old nave bar and try the one you have on here. On your post, "Creating a Drop Down Navigation Menu" I am using your code, but the the sub-menus get covered by other content I have on the page. How do I avoid this?

    Thanks again!
    Zach

  • James
    September 8th

    Hey Zach

    Thanks and im glad I can help you!

    What you can do to avoid other divs and content appearing over your drop down menu's is by applying a z-index to the navigationbar div. e.g.

    .dropdown { font-family: arial, sans-serif; position:relative; width:100%; height:50px; border:1px solid #666666; font-size:14px; color:#ffffff; background:#333333; z-index:9000; }

    This should bring your navigation bar and it's drop downs over the front of any div that is below it.

    Hope that helps!

  • Zach
    September 9th

    Thank you! That worked perfectly!

  • James
    September 9th

    No problem!

  • Beautygalleries
    October 3rd

    Hi James,
    I followed your instruction above and trying to create an add on drop down list to my existing menu just like your drop down link 1 of second level but no luck with it! I can't add anything to my main menu. Would you be able to help me and guide me to create one? Please help!

  • James
    October 3rd

    If you would like to send me your current navigation bar code, then I'll happily take a look and see whats causing you problems :)

  • Beautygalleries
    October 4th

    Hi James, Thanks for your prompt reply and here's my codes:

    Under the template layout I posted :

    Under Page Elements :

    /*- Menu Tabs J--------------------------- */
    #tabsI ul {
    margin:0;
    padding:10px 10px 0 0px;
    list-style:none;
    }
    #tabsI li {
    display:inline;
    margin:0;
    padding:0;
    }
    #tabsI a {
    float:left;
    background:url("http://i306.photobucket.com/albums/nn256/ooiee_bucket/Blog%20Tab/tableftH.gif") no-repeat left top;
    margin:0;
    padding:0 0 0 5px;
    text-decoration:none;
    }
    #tabsI a span {
    float:left;
    display:block;
    background:url("http://i306.photobucket.com/albums/nn256/ooiee_bucket/Blog%20Tab/tabrightH.gif") no-repeat right top;
    padding:5px 15px 4px 6px;
    color:ffffff;
    }
    /* Commented Backslash Hack hides rule from IE5-Mac \*/
    #tabsI a span {float:none;}
    /* End IE5-Mac hack */
    #tabsI a:hover span {
    color:#FFF;
    }
    #tabsI a:hover {
    background-position:0% -42px;
    }
    #tabsI a:hover span {
    background-position:100% -42px;
    }

    Home
    Our Services
    Photo Gallery
    Job Schedule
    Contact Us
    I’m tyring to add another line of drop down sub-menu under the following main menu :
    Job Schedule

    However, after reading through your tutorial, I still can’t get the right code to put underneath and to link to the new sub-menu page that I wanted.

    Hope you’ll be able to help. Many thanks!

  • James
    October 4th

    Thanks! I will look at your code sometime today and find out the problem :)

  • James
    October 4th

    I have emailed you the code, you are welcome to reply to me via email rather than commenting on here if you wish :)

    Hope I've helped!

  • beauty galleries
    October 5th

    Hi James,

    Thanks for your help and it really helps. I've also replied your mail with some questions.

    Do check it out when you have time.

    Take care.

  • James
    October 5th

    Hey Beauty Galleries,

    I will check my emails when im back from college later today.

  • diden
    November 23rd

    Brilliant!!! glad i found you! keep it up..

  • Brandon
    December 21st

    Thanks for this !!! I have been trying to teach myself drop down menu's and would always get stumped. This helped so much !!!! I am in the process of making my own website. I am currently going for my web design certificate and I'm trying to get ahead of my class. Any tips on linking my other pages I created and any advice on getting web space.

  • Jasc
    January 11th

    Thanks for the great Tut!!

    But I have one question, If I want to center my menu, How can I do?

    thanks again.

Leave a Reply





Please Note:

Comments are moderated by both human and automatic resources, to help control the amount of spam comments, as well as unsuitable comments. When you post a comment it should appear instantly, but a comment may be held for moderation if it is deemed suspect for a variety of reasons such as spam, or contains content that may be unsuitable for display. If this is the case you will be notified when you post your comment. Comments are reviewed on a daily basis. For information about comments on James' Blog please click here to read the comments policy.