Skip to page content or skip to Accesskey List.
Search evolt.org
evolt.org login: or register

Work

Main Page Content

How to make a simple CSS dropdown menu

Rated 3.45 (Ratings: 14) (Add your rating)

Log in to add a comment
(20 comments so far)

Want more?

 
Picture of trfc791

Grace Teng

Member info | Full bio

User since: September 15, 2002

Last login: September 15, 2002

Articles written: 1

You've seen those cool Javascript menus that drop down subitems when you move your mouse over them or click on them. One day, in a fit of boredom, I set out to achieve the same effect via CSS. Unfortunately, this only works on browsers that can render hover styles on arbitrary elements, so Netscape and Mozilla users, crow over your IE counterparts for once. It will not work on Opera either. Not tested in Konqueror. Ironically, it looks perfectly fine in Lynx.

Why should I want to have a CSS menu?

There's no real reason why you should use a CSS menu instead of a Javascript menu. In fact, I/you think I'm crazy to have tried in the first place. On the other hand, how many Javascript menus work across all browsers? How will the navigation menu turn out in Lynx? I checked The Javascript Source and the first four dropdown menus I came across were IE only. I would think it is actually simpler to write one in CSS, but the fact that it cannot display in IE negates all the advantages of using CSS. Still, it never hurts to try this. Hopefully, by the time you understand this, IE will have improved their lousy rendering engine.

OK, so how does it work?

The first idea that comes to mind is nested divs. That's what I thought of too. But after surfing around, I came upon Eric Meyer's Pure CSS Menus example. It uses nested lists, which has several advantages over nested divs, both to the end user and the programmer.

The end user, if using Internet Explorer, Opera, or another browser that cannot render hover styles on arbitrary elements, will only be able to see the top level elements which may or may not be links. If they are links, make the page it links to show the items on the submenu as well. If they are not links, tell the user to bash his/her head for not using a better browser or do some browser detection, which will not be covered here. If the user is using a browser that doesn't support CSS, the menu will come out as a bunch of nested lists. Of course, Mozilla and Netscape will display it as a dropdown menu. For the programmer, there is no need to hunt through piles of nested divs any more. The lists and list items clearly show which items are submenu headings and which are navigation items.

Give me the code!

The whole idea is based on these lines of CSS:

li ul {
    display: none;
}
li:hover > ul {
    display: block;
}

If you can't figure out what it means, it just means that a <ul> nested inside an <li> tag should not be displayed, and when the user moves his cursor over the list item, the nested list should appear. The code for the nested list is:

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

Throw in a little formatting and voila! You have your own CSS dropdown menu!

The non-Mozilla-and-Netscape-but-not-Lynx users (well I'm sure you know which group of people I'm referring to) will see only Heading 1, Heading 2 and Heading 3. Make sure the pages that Heading 2 and Heading 3 link to also have links to Subitem 1 - 6, for the sake of those users who cannot see and use the dropdown menu.

Don't stop there!

Unless you want every single list in your page to work this way, the CSS still needs more refining. You don't want your nested to-do list to play hide and seek with you. To prevent this, use classes or IDs. You can give each menu item a class, but that would be ridiculous if you have 100 menu items. Much simpler to use an ID on the base &lt;ul&gt;. Now, the CSS looks like this:

ul#nav li ul {
    display: none;
}
ul#nav li:hover > ul {
    display: block;
}

Subsequently, just add id="nav" on your base &lt;ul&gt;.

Example?

My own trial turned out fine for Mozilla but came out utterly wrecked in IE, which came as no surprise to me. Being my work, it is not worth a plug here. Inigio Surguy has a cool example, but he uses nested divs instead. I've not checked that out in IE, but looking at the source, it is probably wrecked in IE too. I mean, it will look good but will be useless. Be sure to check out his alternate stylesheet if you are using Mozilla or Netscape 6. Also, have a look at Eric Meyer's Pure CSS Menus.

What now?

You might want to try nested lists inside nested lists inside nested lists, or something along those lines. Don't forget to add in some formatting. You should also look at CSS Edge for more interesting things you can do with CSS, and in particular, these CSS popups look good.

A Final Note

I would like Microsoft more if they produced more decent software, especially web browsers. However, please do not regard this as some anti-Microsoft propoganda or something of that sort, because it is not. It just so happens that this menu does not work in what is supposedly the world's most popular browser. No flaming please.

trfc791 is a person often bored to death with school and homework, and can often be found in cyberspace under the name trfc791. But then you knew that already. Unlike most of my friends, I can program (What else would I be doing at evolt?) and program very badly too. If you're here, you probably don't know me because all the people I know who know me would never come to evolt and I suggest you don't continue to know me. My site (free plug :P) can be found at http://trfc791.f2o.org/. When I am on IRC, I am usually on #f2o and on rare occasions, on #evolt.

Opera 7

Submitted by livingdots on January 10, 2003 - 09:37.

Opera 7 (still in beta) does in fact support :hover on arbitrary elements. It is slightly buggy though. For example, the right menu on Eric Meyer's example renders just fine, while the top left menu only works if you move the mouse pointer in from the left hand side over "edge". It's been reported, and I'm sure everything will work properly in the final version. BTW, Opera 7 supports compact boxes properly -- Mozilla doesn't. So as an apparent Mozilla user, you may commence bashing your own head over that one... ^_^

login or register to post comments

Nice idea

Submitted by codepo8 on January 10, 2003 - 09:59.

Don't get me wrong, your CSS idea is good, but...
IMHO I still consider this way of navigation totally senseless and an evil that designers used for far too long.

First of all, if it is going to work without scripting one day, that'll be the day when these finally get a bit more usable.

But they still have 2 big disadvantages:

1) Every bit of navigation needs to be on every page, this adds to pageload and also means a nightmare for aural browsers and accessibility.

2) The space used by the dropdowns needs to be known, if the dropdowns (especially multi level ones) need to much space, you'll have to scroll, and as soon as you scroll, the dropdown loses the focus and disappears.

3) Layer Dropdowns cannot cover multimedia elements or Forms properly in most browsers, this makes the space they will cover forbidden zone for anything but images and text.

4) There is no indicator where in the navigation I am. When I chose point1 - subpoint 7 - sub sub point 6 and load it, I will have to start again drilling down when I want to go to sub sub point 7.

So, basically, thumbs up for trying to do this in CSS, but there is nothing "clever" about this way of navigation, it's bells and whistles.

login or register to post comments

err

Submitted by codepo8 on January 10, 2003 - 10:00.

4 disadvantages, sorry, got carried away there

login or register to post comments

No IE?

Submitted by coinz on January 10, 2003 - 10:05.

Great demo. No offense, but what good is this approach if if doesn't work in IE (a.k.a 80+% of the user population)?

login or register to post comments

The IE Question

Submitted by grubi on January 10, 2003 - 11:06.

THe IE question has a simple answer: write compliant code and eventually people will see so many compliant sites "break" in non-compliant browsers (i.e., IE), so that Microsoft would be pressured into either improving their product or pissing off enough people who will download a different browser altogether.

login or register to post comments

Along the same lines

Submitted by jkd on January 10, 2003 - 12:39.

CSS Trees Basically uses CSS to move the classic tree widget from DHTML to a styled <ul>. Uses a CSS3 property (user-focus), so it is Moz-only, and even then it doesn't entirely work right :-D, but still I think it's a neat glimpse into what is possible in the near future with just CSS.

login or register to post comments

Answering the IE question

Submitted by dshea on January 10, 2003 - 21:46.

grubi - while a nice thought in theory, in practice that will never fly. Being a commercial developer, I absolutely must cater to the majority of the audience as it would be suicide (or at least grounds for dismissal) to do otherwise. I even have to go a step further and take care of that narrowing 4% window of users still on Netscape 4.x.

The IE question has no simple answer, short of a catastrophic bug that caused all versions currently out there to stop working. Microsoft will eventually get CSS2 right (crossing my fingers for 7.0), but until then, neat tricks like this that work for only 5% of the market will remain in the realm of play sites. Support the web standards campaign, use the bleeding edge CSS tricks in your personal work, and bookmark these ideas for the day when it's practical to use them on a wider scale.

login or register to post comments

bah *bashes head*

Submitted by trfc791 on January 11, 2003 - 01:52.

Yes, I am a Mozilla user, but no, I do not recommend actually putting this into use on live sites. I started thinking about this when one day I realised Mozilla could do hover effects on <td> and subsequently divs, lists, etc. almost anything, so I tested it in IE and it didn't work. But I thought that these effects have a lot of potential, so I might as well try something out with it. Good to hear it works on Opera, that's one step closer to getting it to work on all browsers. In fact, I downloaded Opera 7 yesterday (the day this article went live.) But until IE gets it right it is not a plausible navigation option.

In response to codepo8's comment, a possible option is to mix scripting with the CSS so that when you get to say, sub sub point 7, the dropdown remains intact. Of course, the result is not likely to be worth the time spent on it, on top of a working IE version. I did give developing this a shot, but gave up after trying to browser detect browsers like Opera and Konqueror. (Will someone who uses Konqueror and have enough time to waste their time on this article please test if it works in Konqueror?) Plus, think of the more obscure browsers. If the users of those browsers consitute a large part of your audience (suppose your site is for Omniweb users) then this type of navigation is not viable at all.

I just hope it won't be long till IE gets CSS2 right.

login or register to post comments

So last year...

Submitted by mad_hatter on January 14, 2003 - 01:54.

For those of you interested, Chris Poole pulled this off awhile ago, multi-tier nonetheless. He's an up and coming person when it comes to CSS. Check it out

login or register to post comments

OT - Comment fields for Opera

Submitted by shotgun on January 14, 2003 - 07:44.

Why the subject and comment fields at the end of articles are so tight and small when using Opera web browser?. I have read in many parts of evolt about the standards-compliant evolt.org code and even this page DOES NOT validate with W3C Validator for HTML 4.01!!!!

login or register to post comments

Opera's problem?

Submitted by trfc791 on January 15, 2003 - 06:06.

I don't know about you, but I find it all right in Mozilla, but suspiciously squeezy in Opera... I'm not too sure about the evolt code being compliant, but I do know that if you would like to voice your opinion on this sort of issues, this is not the place to do it. Please stay on topic.

login or register to post comments

IE - I have to agree with coinz...

Submitted by tq on January 16, 2003 - 07:00.

In do not think that writing compliant code and seeing sites break in IE will force MS to adopt CSS2 standards. I also think that IE has more than 90% market share now in it's various forms (http://www.thecounter.com/stats/2002/December/browser.php)

login or register to post comments

Dropdown from nested UL with CSS and DOM

Submitted by dahlbyk on January 16, 2003 - 15:01.

The dismal CSS2 support of IE makes CSS-only dropdowns a good way to lose your site's audience. With this in mind, I've been toying with the idea of using nested UL for markup, CSS for formatting, and minimal DOM-compliant JavaScript to create a drop-down menu. In my studies, I found the work of Dave Lindquist. He has implemented a simple version of my vision, which I will post here when it is completed. In the meantime, check out his dropdowns here: Using Lists for DHTML Menus. Comments on the design, or suggestions for my full-featured version are invited.

Cheers ~ K

login or register to post comments

[OT] - Opera's problem

Submitted by shotgun on January 17, 2003 - 07:05.

Sorry to disgust you Grace so much, but I HAVE followed evolt rules about off topic posts by CLEARLY marking it with OT in the subject. I just have to say that for anyone to make claim that his/her website is standards compliant, it should VALIDATE with the standard's authority validator. I won't enter into unconstructive arguments about browser support for standards. BTW, this is not completely off-topic, since it mentions another point in the long, long war on standards-compliance. I have to agree with grubi post in that everyone should write compliant code and eventually, our jobs will be easier. My 2 cents.

login or register to post comments

Try JavaScript

Submitted by shifter on January 24, 2003 - 11:59.

You can easily implement this using some simple JavaScript. Get the main menu item and then on mouseover pop out the visibility of the submenu. Properly coded this method would work in most browsers, except JS disabled.

login or register to post comments

RE: Dropdown from nested UL with CSS and DOM

Submitted by timmy_doulos on February 25, 2003 - 16:54.

>With this in mind, I've been toying with the idea of using nested UL for markup, CSS for formatting, and minimal DOM-compliant JavaScript to create a drop-down menu.
You may want to look at AQTree - (Unobtrusive DHTML, and the power of unordered lists)
Reference is made to the work of Dave Lindquist, but I find this implementation more elegant, as it doesn't require the div's that Dave uses.

login or register to post comments

Suckerfish Dropdowns

Submitted by microknee on January 29, 2004 - 14:38.

Patrick Griffiths and Dan Webb came up with a simple way to get around the IE hover:element problem. Yeah, still falls back on javascript, but still, I think this is a far better of creating and managing dropdown lists and menus than the old-fashioned way. Check out the article: http://www.alistapart.com/articles/dropdowns

login or register to post comments

another css menu that works everywhere

Submitted by iubito on March 14, 2004 - 04:19.

Hi ! Like you I decided to create an good css menu, viewable with all browser. Because before I used an ugly only-js made menu. You can try it at http://iubito.free.fr/prog/menu.php The article is in French but not too hard. I've tested on ie5 / ie6 win 2000 ie5 / win 98 opera 7 / win netscape 7 / win mozilla / win / mac / linux konqueror / linux omniweb / mac IE5 / mac os9 & X safari / mac and it works everywhere :) it's css + a piece of javascript. Give it a try and have a nice day ! :)

login or register to post comments

CSS menus can work on IE

Submitted by JMBattista on April 16, 2004 - 08:40.

you have to include a behavior file, which is .htc it runs some java script that goes through and turns the :hovers into mouse over events, so whenever you move the mouse over it something happens, non-IE browsers ignore this. So it will work with the plain css in mozilla, and the behaviors make it work in IE...

login or register to post comments

On Internet Explorer

Submitted by Hamman on March 2, 2007 - 07:42.

Thanks for this article! I also found a pure CSS drop-down by Stu Nicholls that works from IE 5.5 to 7. It uses lists and CSS hacks.
Here's the link: CSS-hacked dropdown
Hope this helped too.

login or register to post comments

The access keys for this page are: ALT (Control on a Mac) plus:

evolt.orgEvolt.org is an all-volunteer resource for web developers made up of a discussion list, a browser archive, and member-submitted articles. This article is the property of its author, please do not redistribute or use elsewhere without checking with the author.