[web-tips > new horizontal menu]
tedd's NEW Great CSS Horizontal Drop-Down Menu
The question has been asked numerous times in the css forum, "Is a there way to create a css horizontal drop-down menu in css that will work on most browsers. Well... technically, the short answer is "No" if you are using just css, because IE is problematic in the way it handles css. M$ always has a better idea.
However, above is an example of a horizontal drop-down menu that really works! It even works with all IE (6+) browsers as well every compliant browser I know (i.e., FireFox, Safari, Opera, Mozillia, etc.) and under every OS I am aware.
You see, a smart guy named Peter Nederlof with help from Arnoud Berendsen, Martin Reurings, and Robert Hanson wrote a brilliant piece of javascript that makes this pretty simple. I've included that code with mine and created something very slick and simple for you to install and use.
So, to make this css horizontal menu work for you, follow these two simple steps:
- Download this file to your desktop, unzip the file, read and follow the directions found in the "README" file.
- Prepare and add your html link code to your web page. An example is shown below. Please insert your text and links as needed.
<div id="menuh-container"> <div id="menuh"> <ul> <li><a href="#" class="grandparent">Item 1</a> <ul> <li><a href="#">Sub 1:1</a></li> <li><a href="#" class="parent">Sub 1:2</a> <ul> <li><a href="#">Sub 1:2:1</a></li> <li><a href="#">Sub 1:2:2</a></li> <li><a href="#">Sub 1:2:3</a></li> <li><a href="#">Sub 1:2:4</a></li> </ul> </li> <li><a href="#">Sub 1:3</a></li> <li><a href="#" class="parent">Sub 1:4</a> <ul> <li><a href="#">Sub 1:4:1</a></li> <li><a href="#">Sub 1:4:2</a></li> <li><a href="#">Sub 1:4:3</a></li> <li><a href="#">Sub 1:4:4</a></li> </ul> </li> <li><a href="#" class="parent">Sub 1:5</a> <ul> <li><a href="#">Sub 1:5:1</a></li> <li><a href="#">Sub 1:5:2</a></li> <li><a href="#">Sub 1:5:3</a></li> <li><a href="#">Sub 1:5:</a></li> <li><a href="#">Sub 1:5:5</a></li> </ul> </li> </ul> </li> </ul> <ul> <li><a href="#" >Item 2</a> </ul> ... repeat and alter the list as needed </div> <!-- end the menuh-container div --> </div> <!-- end the menuh div -->
Some important things to consider are:
- This technique works for all browsers EXCEPT IE 5.23 and below -- these are ancient browsers with a less than 1% following. Anyone still using those browsers are probably not worth the effort;
- There is no "sniffer code" as there has been in previous versions. All that has been taken care of in the "tedd" directory (See the README file in the download). Please forgive my naming convention, there are reasons for it -- it makes things simple and it works. In other words, DO NOT RENAME THE "tedd" DIRECTORY;
- The position of the menu is dependant upon the "menu-container" rule in the css document. Change the position, top and left rule values and the placement of the menu will adjust accordingly;
- The "grandparent" and "parent" classes use arrow gifs. If you want to use your own, then create them and place them in the "tedd" directory as "menu-arrow-down.gif" and "menu-arrow.gif". If you don't want to use arrows, then comment out the css code that uses them;
- Besides the navigational layout and textual content, the only things you may want to change are the font, font-size, menu-width and colors of the menu. In this example, the foreground is white and the background is RoyalBlue (#4169E1) for the menu "at rest" and the "mouse-over" state is CornFlowerBlue (#6495ED). As such, the menu-width (currently set at 9em), font and colors are pretty obvious and easy to identify and change.
- If you want to change the size of the text, please use em's and not pixels. That way the menu will hold together (i.e., not break) if the user changes zoom levels. This menu is zoom cooperative. The standard conversion for px to em's is 16:1 (i.e., 16px = 1em);
- If you want to use Images instead of text in this drop-down menu, then look elsewhere for a solution. Or better yet, check out my "Wonderful World of Includes". That demo will show you why you should not be using images for a navigational menu. But if you insist, good luck;
-
The things that you may NOT change in the css are clearly marked
"DO NOT ALTER OR REMOVE" -- there is no reason to change any of these, so be forewarned; - Place the "tedd" directory where your web page can find it. In this example, it is located at the same level as this page. If you move this directory, you must change the path (i.e., href="what/tedd/menu.css" ) accordingly within the stylesheet link within the <head> tags.
- If you have any questions as to how all of this comes together, please take the time to review the source code of this web page and compare it to your own. REALIZE that this page works because everything is where it should be. If yours doesn't work for you, or if you have problems with this technique, then I suggest you follow direction in the README file and take my code "as-is" and move it to your site -- it will work if you follow directions. The "as-is" should look/work like this -> EXAMPLE. Once you get it to work like this, then you can make a copy and work with the copy while making changes. Remember, code has to be exact and even minor changes can make significant differences in performance. So go carefully.
- Also, if you have problems with the code and post a question here, then also post the url where you have placed the example as given in the README file. I don't mean to be rude, but if you are not willing to follow directions, then do not post your questions here;
- For most of you this will not be a problem, but some people have posted problems with my code and asked for my advice. When I investigated, I found that they had purchased a collection of garbage code riddled with non-standard programming and tons of validation errors. Do not report problems here or ask me questions about code that does not validate. Please put your web page through the W3C validation checker before contacting me. The URL for this checker is: W3C. If you ask a question and your code does not validate, then expect my public reply to be pointed. So be warned.
- And lastly, PLEASE, this is very important. I have taken my time to prepare this technique for you -- it will work if you follow directions. If you find this menu useful then please post your experience to this site. If you want to repay me for my efforts (remember I am usually paid for my advice) then you can pay me something for my time -- $5 would be nice. You can make payment to tedd@sperling.com via the PayPal button below.
Please donate $5 below for my development effort -- thank you.
This technique W3C validates under both HTML 4.01 Strict and CSS guidlines.
Enjoy.
tedd...
Comments
Found one annoying bug that shows up in IE7 though...
To replicate:
1. Point at "News"
2. Point at "Global"
3. Exit menu by moving mouse to the left
4. Point at "News" again
Now the outline of the "Global" sub-menu is visible without the menu entries. Very annoying... Works fine in FF3!
Any solution to this behavior? I haven't changed anything and the problem occurs on your site as well.
Regards,
I found a solution to my problem:
Use version 3 of csshover.cth (ccshover3.cth) and the problem is gone!
Found it here: http://www.xs4all.nl/~peterned/csshover.html
Regards,
If someone is going to report an error, it always nice for them to post a solution as well.
Many thanks for your post and I have updated everything accordingly. The menu now works flawlessly in all browsers I have tested it with.
Cheers,
tedd
You have hit upon a problem that exist between browsers and that problem is how they scale elements for viewing. Understand, with browsers everything is relative -- there are no absolutes. Please read my article entitled "Four Things".
As per your inquiry, I did some testing and found that the width of my menu (as measured by me) varies between browsers with respect to pixels (YMMV). For example:
Under Vista:
Browser Pixels
IE7:     649
FireFox: 647
Safari: 644
Opera: 629
Under XP
Browser Pixels
IE6:     649
FireFox: 647
Opera: 629
Now, is there a way to make everything the same? The answer is NO -- in short, the web is not print.
You may mitigate differences by using a common unit of measure. If you use pixels, then use pixels throughout your web site. If you use ems then use those. But do not mix units!
Also keep in mind that even using a single unit of measure does not mean that browsers will treat round-off errors and zoom levels the same way -- there will be differences. There is no way around this problem. Again, web is not print.
Cheers,
tedd
Thanks for your time and effort,
Luke
Check out the vertical menu I offer on this site. Just look down the Web Tips page.
Cheers,
tedd
I'm trying to use the exact menu on this page...but when i hover over a link with a drop down menu, instead of showing the drop down menu vertically as your example above, my drop down comes out horizontally.
What is causing this?
thanks
What's causing this is that you are not following directions.
Please re-read my instructions and follow them. If you do, then you'll know how to post a question reporting a problem.
Cheers,
tedd
Thanks in advance for any info.
I just checked and my menu is working properly in IE6 under Windows XP. Perhaps you have JavaScript turned OFF. You see, it is a JavaScript routine that makes IE6 work like it should.
Incidentally, my stats show that IE is currently holds only 14 percent of the market and that figure is dropping at a rate of one percent per month. So within a year, this M$ blunder won't be holding back technology any longer and will fade into oblivion like their previous blunder (i.e., IE5) did last year -- and good riddance.
Perhaps someday people will learn that M$ is not the technology leader they claim to be. But then again, many people still believe that government is the answer to every problem. Misconceptions have their cost.
Cheers,
tedd
Thanks, Klara.
I apologize!
My problem that I posted earlier was also a case of bad CSS on my behalf. I too have to master it better. Sorry for bothering you and thank you for a nice menu.
Kind regards / Niklas
I'm a spanish programmer and you resolve me some problems
http://donshoaff.com/tedd-index.php
Thanks very much for this awesome resource! I'm a print designer by trade, and my first go at Web design has been pretty frustrating (and fulfilling) thus far.
In any case, I was wondering if there's any way to have the sub-menu items wrap to the width of the containing "grandparent" element—at the moment, the text just seems to flow out on the right hand side (as in the URL I've provided). Am I doing something wrong here?
Thanks again,
NK
Great code, thanks a lot! There is only one small issue I have. If you check out www.pfrc.ca/prenatal.html and hover over the PROGRAMS menu (far left) the menu drops down, but behind the image below (sorry, that is in IE6 only) I tried to set the image z-index to -20 to put it below, but that didn't work.
Do you have any thoughts?
Thanks again,
Cam
I looked at the tedd-index.php script and noticed that the doctype specifies 4.01 Transitional yet the W3 page it points to is the "loose" definition. I've never seen this before. Is this correct?
I'm coding in xhtml strict 1.0. So I'm curious if this will affect the parsing of the css.
Thank you.
will
P.S. Thank you for sharing your code. It is really cool. :-)
I changed the DOCTYPE, but that really doesn't matter much as long as the page validates.
tedd
My problem is that the text on the page shows through the drop down boxes, as if the boxes are a transparent blue. I need the drop down boxes to be a solid color that sits on top of the page text. I have set my text div to a lower z-index (10), and this does not help.
Am I missing something simple?
(sorry - no link to post - can't put online until finished)
Thank you,
Linda
Try doing this -- assign a high z-index value (i.e., 100) to every element that you want to appear above and a low z-index value (i.e., 10) to everything you want to appear below.
At some point you have to pin down what's wrong.
Cheers,
tedd
At the risk of incurring your wrath...
I have used this menu and adapted it along your guidelines. I like it a lot. It works well with Safari and Firefox but I seem to have done something wrong as, when I test it with IE08 (running my Mac in PC mode - which I hate doing anyway) there is no drop down element.
I have verified my html and css with W3C as recommended. The site is hosted by British Telecom who put the site in a '/Public' folder. I have followed your directions and put everything into this folder in the way you directed. I would be quite happy if everyone just used Safari or Firefox but that is not the way of the world...
I have also reviewed the feedbacks and your responses so realise that it is something I've done wrong BUT I don't know what it is.
Please advise.
PS I would be happy to pay for this service - talent needs rewarding.
I have tried to contact you via the email address you gave, but it comes back as not deliverable.
In case you check this site, please try using IE08 on your Mac and review this page. If this page works, then yes you have done something wrong.
I have tried visiting the url you gave as your site, but I see no menu there. If you want me to look at your menu, and can stand my wrath, then post a URL and I'll review your work and comment.
Cheers,
tedd
I'm sorry, but there is just too much spam to review to provide answers. For example, for every honest question, there are over 1000 spams. I am leaving the comments here so that they may help others.
I find my time better spent providing more examples and attending to clients.
tedd