Pages Navigation Menu

Dropdown Menus Using CSS

Once video starts, click on video to expand to full screen for best viewing

Dropdown Menus Using CSS

Skill Level: Intermediate

Christian Wilson wrote in with a request this week which I would like to share here (thanks Christian!).

Christian Writes:

How do you make a pull down navigation menu that is all in CSS?

As I said before, this is not a beginners task and might not even be an intermediate one, but I will throw down on it and you can pluck from this what you will.

First challenge I had in answering this question is that it doesn’t lend itself well to the whole "video" thing, so I am going to publish an article, right here in the video section to get you going while I try to think of a way to make this into a video.

There are two methods to adding a navigation bar across the top of a web page. One uses Javascript and "events" to launch an action, and the other relies on the cascading nature of style sheets to define rules that are applied from the top down.

First, the goal… Here’s a sample from my other video site (which uses the CSS to build pull down menu’s). Those of you who visited my original site will remember that the Category "link" had a drop down menu. Since this wasn’t a "blog", I had some freedom in what I did. Here’s a screen shot showing the drop down under the "Category" link in my top navigation bar.

The HTML is pretty straight forward as you can see from below. It just uses nested unordered lists. It’s the CSS that makes it all work.

Please Note: In order to display the actual code (and not have it BUILD the lists), I had to use an image of a screen capture. Here is a text file that contains both the navigation and the CSS.

I told you it was straight forward! In fact, you have seen this many times before. Want proof? Here is that exact same HTML but because this blog doesn’t share that style sheet, well, you can see the difference. Please note: Since it is the exact same HTML, none of these links will work!

Now that we have the HTML written and know our goal, let’s get busy with the CSS. If you aren’t interested in HOW it works, just download the sample file from the link shown above and copy and paste into your web page. Play around. Just keep the sample file separate in case you need to start over.

We will use this section for the discussion so you might consider printing this part out.


/* NAV - top horizontal nav */
#nav,
#nav ul {
padding: 0;
margin: 0;
list-style: none
}
#nav {
font-weight:bold;
height:2.09em;
font: bold 96% arial;
margin: 0 105px 0 40px;
}
#nav li {
position:relative;
background: #999;
float: left;
width: 10em;
display:block;
margin: 0;
border-bottom: 3px solid #666;
border-right: 3px solid #252525;
padding:0
}
#nav a,
#nav a:link,
#nav a:visited,
#nav a:hover,
#nav a:active {
text-decoration:none;
cursor:pointer;
color:#fff;
display: block;
padding: 4px 10px 2px;
}
#nav a:hover {
color:#000
}
#nav li ul {
border-left: 1px solid #c00;
background: #f6f6f6 url("images/featurebox_bg.gif") no-repeat 100% 100%;
width:15.8em;
font-size:90%;
margin-top:3px;
position: absolute;
font-weight:normal;
left: -999em
}
#nav li:hover ul,
#nav li.sfhover ul {
left: 0;
z-index:99999
}
#nav li li {
background:none;
float:none;
border:none;
border: 1px solid #999;
border-top:1px solid #fff;
border-right:none;
border-left:none;
padding-left:0
}
#nav li li.last {
border-bottom:none
}
#nav li li a,
#nav li li a:link,
#nav li li a:visited,
#nav li li a:hover {
color:#000;
padding: 3px 10px 2px;
width:14em;
}
#nav li li a:hover {
color:#fff;
background:#c00;
}
#nav li.active {
background: #c00;
border-bottom: 3px solid #c00
}
#nav li.active ul {
border:none;
background: #c00 url("images/featurebox2_bg.gif") no-repeat 100% 100%
}
#nav li.active a:link,
#nav li.active a:visited,
#nav li.active a:hover,
#nav li.active a:active {
}
#nav li.active a:hover {
color:#000
}
#nav li.active li {
border:none;
border-top: 1px solid #c15c5c;
border-bottom: 1px solid #870000
}
#nav li.active li.last {
border-bottom: none
}
#nav li.active li a:link,
#nav li.active li a:visited,
#nav li.active li a:hover,
#nav li.active li a:active {
color:#fff
}
#nav li.active li a:hover {
background: #666 url("images/sprites.gif") repeat-x 0 99%;
color:#fff
}
#nav li.active li.active a:link,
#nav li.active li.active a:visited,
#nav li.active li.active a:hover,
#nav li.active li.active a:active {
color:#fff;
font-weight:bold;
background: #666 url("images/sprites.gif") repeat-x 0 99%
}
/* hide from IE mac \*/
#nav li {
width:auto
}
/* end hiding from IE5 mac */

Wow, that’s a mouthfull, huh?

Okay, direct your attention to the very first bit, that defines the overall style for the entire navigation bar:

#nav,
#nav ul {
padding: 0;
margin: 0;
list-style: none
}

This set’s the stage for the CSS to work by defining the list-style initially as none. Subsequent tags will override this default. After this section is just a rule for the font weight, size and height of the background area. Pretty standard stuff.

The next rule really get’s busy enabling our drop down.


#nav li {
position:relative;
background: #999;
float: left;
width: 10em;
display:block;
margin: 0;
border-bottom: 3px solid #666;
border-right: 3px solid #252525;
padding:0
}

Notice that it’s position is relative and it floats left. This is the "category" link itself. It’s the very first "li" level. What makes the drop down work is the display:block command. This forces all the unordered list items into a "block" under the Category "link".

To be consistent, the next section defines display:block for the anchor tags as well.

The section that defines the li ul (the unordered list UNDER a list item within an unordered list), sets the position to absolute. That might seem counter intuitive but remember that position relates to the container it is in. Since the ul is within a li, within an ul, the position is relative to that item. (Whew, try saying that three times in a row).


#nav li ul {
border-left: 1px solid #c00;
background: #f6f6f6 url("images/featurebox_bg.gif") no-repeat 100% 100%;
width:15.8em;
font-size:90%;
margin-top:3px;
position: absolute;
font-weight:normal;
left: -999em
}

This rule also refines the background images (replace with your own or remove to just use color), and the default width of the "box" that drops down. (width:15.8em)

The rule that defines the li li section (shown below) turns the float off, and defines the borders.


#nav li li {
background:none;
float:none;
border:none;
border: 1px solid #999;
border-top:1px solid #fff;
border-right:none;
border-left:none;
padding-left:0
}

The rest of the CSS defines colors, font’s background and behaviors when links are hovered and are pretty standard.

My only other suggestion is to download the file, paste into your web page and CSS files and play with it. That’s what I did. I can’t remember the site I came across that used this, but I used the Web Developement add-on for FireFox to display the CSS (which I copied) and the page source (which I copied). I put them both into a "play" page and experimented. It only vaguely resembles the original page I found it on since I made it my own.

Christian, I hope this helps. Now, go play.

Update: Hey Christian,

Thanks for the question. First, you need to realize one thing about CSS and table-less formatting. Containers are everything.

Here’s what I mean.

With traditional tabled format, your table was the primary “object” that you placed content in. While it is pretty simple to do, it made for tougher “reading” when it came to your code. And everything had to fit inside.

With the new CSS method, you just need to build containers (div tags) and place your content there. So, to center your “body content”, created a tag called b_content (or something similar) and set the properties so that it has margins of “auto” and is the width you want.

Create a new tag for the navigation and float it left (ie: c_navigation) and set it’s width. So, div c_navigation is contained in b_content and floats to the left side of that container.

Most of my layout’s use something similar. A main container, and then two or three sub containers.

If this doesn’t answer your question, let me know and I will try to make a video about it, or go into more detail in an article or something.

Update: 7/5/2009

Found a cool online tool (free of course) that will allow you to build your menu for your website interactively, and then download the code for it. You can check that out at Pure CSS Menu

The *Go To Gal* for the *How To Stuff*

Photo of WordPress Blogging Expert Cenay NailorCenay Nailor is an Internet, Affiliate and Networking Marketing success coach with mad technical skills she loves sharing. Visit her other blog and get some technical marketing help. You should also snag a weekly injection of Tech-Based Marketing Tips in the Newsletter she offers, and have it sent right to your inbox.



[[T_F]]Data Leak Prevention - Data Security Solutions - Information Theft Protection, Detection and Prevention Software Productstracefusion_signature=dec2a50de84b2e7994904df9ccb35dd7480785a58589b2328af350fcbe9055df322c3a4f8fa4967207df83192eaf99c9749502043e08badb217d011b0356a294fa31cd5cb66f0836d93adba1454cab261106929272d7b652dc44dd943e5aea5abbae617366f4d0fd2e673cbfd623e163b0d76c7f5df91855a4338afba3f175b67d36c1e9d7985c8145fb893f5f93e8851d85fe70c33b169a1967faa965bc44222fce88c51c3eac063f9baf0f6b867eea2fe7a1982be6bca1847c895773147c4cf6f197e4051dcb033692b46fc3f048c64aa860274ae7a171c1961ad19495373ba0d535ebc25c3f08432616559454db154615d97128a429a2d8edf39772cfdc94a51edba74a3c7370fc9f17835d34f5f14c2e931107944cdd24207d1323fae212cf64d02397327defdac5e545742ebeb01e80b09ad2a6dc3bc5974f79831b364cdc9cb96f67578ed9c26a0eb1a5f77e9d736d56927989bd3cd33364c8f5754c01c19e77cccc536c4947e6e215d421b51196684aa4e65ad39d7cb7172a344dc2002eeab5264aa313e697a3de2e83e66638d61f4c0b79006d53ba4aa92022ee3a0af9298412f71c0762d353d3630eaaadc0db7272722f9f00a6e934589469d879cb2ba145c058432a478356c6085229d4218164d5f9767aebe89ab01f248830d01372c36b9e7279489e7278af[[T_F]]

5 Comments

  1. I need to know how to center this as well as how to make an extended to a third column thanks for all the help thus far?

  2. Hey since your the first person in a year and half who has actually been able to explain with out leaving so much out it is painful to read i am wondering if you can help me with a few other things please shoot me an email

  3. Shot you an email. Contact me anytime.

  4. With the new CSS method, you just need to build containers (div tags) and place your content there. So, to center your “body content”, created a tag called b_content (or something similar) and set the properties so that it has margins of “auto” and is the width you want.

    Create a new tag for the navigation and float it left (ie: c_navigation) and set it’s width. So, div c_navigation is contained in b_content and floats to the left side of that container.

    Cenay, forget the rest of the stuff, this piece right here explains why I have been going wrong in trying to get things to stay where I think I’ve put them! Sheesh, you are a star girl. Thanks

  5. Well, thanks Susan for your vote of confidence. I am happy to have helped you understand something I am so passionate about.

Leave a Reply

Related Resources