Help - Search - Members - Calendar
Full Version: Pure Css Drop Down Menu
Zymic Webmaster Forums > Web Design & Development > Client Side Scripting > HTML and CSS
xire
Check out the action here.

Comments?

Since the website MAY be subject the digg effect, I'll mirror the post + code here.



QUOTE
After playing with some javascript menus today, I decided to see if I could pull off a drop down menu without any javascript. I've seen it done before, so I thought I'd give it a shot. It actually ended up being pretty simple, so I thought I'd share the how-to.

I uploaded a demo of what I mean. If you take a look at the source, the basic elements of the menu are ul and li tags.

The first thing to do is differentiate the parent menu items from the menu items that only appear after you hover over the parent item. I did this with classes.

CODE
<div id="menu"> <ul id="item1"> <li class="top">menu item</li> <li class="item"><a href="#">menu item 1</a></li> <li class="item"><a href="#">menu item 2</a></li> <li class="item"><a href="#">menu item 3</a></li> </ul> </div>


You'l see the parent item is marked with top, while the rest are marked item. Now I can get to hiding .item and letting .top continue to show. So I'll add this to the style sheet.

CODE
#menu ul .item{display:none;}


And when I hover over the top of the unordered list, I want all list items with class item to appear again. Here's what I'll add to the style sheet.

CODE
#menu ul:hover .item{display:block;}


You could choose to use display:block or display:inline. The last important piece is the position of the menu.

CODE
#menu{position:absolute;}


I wouldn't say this is necessary, but if you'd like the drop down to appear on top of the content below, it is. Otherwise, your CSS menu will just displace everything underneath it.

You can dig through more of the source from the demo if you'd like to see the styling of the menu. Most important of all, this is all web standards compliant, and keeps the HTML of the page beautiful. I've tested it successfully in Camino 1.5.1, Firefox 2.0.0.9, and Safari 3.0.3. If you'd like test it anywhere and see how it fairs, go for it.
EMOruffino
actually someone was asking me about this... nice find!
Nexus
Drop-down menu FTW. Definitely a lot better looking to the one I've used with a free template.
xire
Since the website MAY be subject the digg effect, I've mirrored the post + code above. Enjoy.
Colin
Nice man...I gotta think of a way to use it tongue.gif
MrTouz
this is really nice, really really nice and useful ! thanks !
MrTouz
well not working in IE6... since IE6 doesnt understand BLOCK... i found an other way to do it, but MUST use javascript...

http://tutorials.alsacreations.com/deroulant/

very nice tho
EMOruffino
personally, thinking about it, i would not want to do this with pure CSS... i would rather base it off mootools....

actually im going to start using mootools more often
MrTouz
mootools ? no idea what it is... but an other way would be flash, im sure that navigation must not be that hard...
Cmain
I was actually messing about with some CSS a few days ago and stumbled upon doing a menu like this myself. I then looked around and it seemed like it was quite popular.
psam
Really nice biggrin.gif , i was really needing this.
EMOruffino
QUOTE(MrTouz @ Dec 1 2007, 01:53 PM) *
mootools ? no idea what it is...

a tear has come to my eye...

http://mootools.net/
IamShipon1988
Its a very nice method. What I'm confused about it the validation part. Did anyone test it? Did it validate on W3? What I see right away is in the UL tag, what is item1? It's not defined in the CSS. When you use random tags it becomes an issue.
MrTouz
its just to name the different sections of the menu

he doesnt use the ID so for his code its kinda of useless but what you can do :

div#menu item1 {
and put your CSS
}

which will give each of your items a specific CSS i guess smile.gif

but i don't see any use for him to name the items
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2012 Invision Power Services, Inc.