[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:

  1. Download this file to your desktop, unzip the file, read and follow the directions found in the "README" file.
  2. 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:

  1. 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;
  2. 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;
  3. 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;
  4. 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;
  5. 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.
  6. 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);
  7. 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;
  8. 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;
  9. 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.
  10. 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.
  11. 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;
  12. 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.
  13. 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
PL   Tuesday, June 30, 2009 07:21
 
Great job!!

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,
PL   Tuesday, June 30, 2009 07:46
 
Hello again!

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,
tedd   Tuesday, June 30, 2009 08:47
 
PL:

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
DH   Tuesday, June 30, 2009 15:56
 
Hello ... I would like to use this Multi-level menu in a fixed width web page. Everything works great except the final width of the menu. The Firefox 3 presentation is slightly wider tha the IE7 presentation. I examined the demo version on this site and the same presentation occured. Is there a way to fix this?? Thanks DH
tedd   Wednesday, July 1, 2009 10:27
 
DH:

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
Luke   Thursday, July 2, 2009 18:37
 
What part of the css code decides whether the drop down menu is vertical or horizontal? Currently my drop down menu is horizontal right under the main menu, which i like just fine. But if i wanted to make it vertical, how would i do that?

Thanks for your time and effort,
Luke
tedd   Thursday, July 2, 2009 21:19
 
Luke:

Check out the vertical menu I offer on this site. Just look down the Web Tips page.

Cheers,

tedd
Luke   Thursday, July 2, 2009 21:37
 
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
tedd   Thursday, July 2, 2009 22:11
 
Luke:

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
Luke   Thursday, July 2, 2009 22:38
 
What line of the css code will make the drop down menu vertical or horizontal
wsfn   Monday, July 6, 2009 08:06
 
Morning Tedd. I'm looking at this page and cannot see your sample working. I can tab to each tab, but no drop-down on either enter or mouse and click. I can elicit a tab color change on click, but no further menu topics appear. Is my US Fed controlled machine too bound up to see it properly? IE6.0 of course!

Thanks in advance for any info.
tedd   Tuesday, July 7, 2009 07:53
 
Good morning wsfn:

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
Klara   Thursday, July 9, 2009 17:41
 
I would just like to thank you for sharing this new version. It fixed my problem with the menu opening vertically and thus far I have not had any major problems with IE7, IE8, or Firefox.
Thanks, Klara.
Niklas Waller   Monday, July 20, 2009 02:34
 
Hi Tedd,
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
Benito   Monday, August 10, 2009 18:06
 
Thank's for your work.

I'm a spanish programmer and you resolve me some problems
Neil   Tuesday, September 22, 2009 01:26
 
Thank You Very Much, You Are A Good Gan !
Don   Tuesday, October 6, 2009 13:06
 
how does this menu seem with images in the menu?
http://donshoaff.com/tedd-index.php
NK   Thursday, October 8, 2009 15:38
 
Hi Tedd,

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
Cam Lillico   Thursday, October 8, 2009 19:45
 
Hi Tedd,

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
will   Thursday, October 15, 2009 11:10
 
Hello, Ted,

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. :-)
tedd   Tuesday, October 20, 2009 12:12
 
will:

I changed the DOCTYPE, but that really doesn't matter much as long as the page validates.

tedd
Linda   Tuesday, October 20, 2009 17:54
 
Hi,

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
tedd   Tuesday, October 20, 2009 18:08
 
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
Ian Booker   Tuesday, December 22, 2009 07:00
 
Hello Ted - and Merry christmas!

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.
tedd   Sunday, December 27, 2009 18:02
 
Ian:

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
matthew   Tuesday, August 3, 2010 14:00
 
I am using your CSS Menu, and love it. But the site that I am working on has an admin menu, which I want to appear when an admin is logged in. I have the "#menuh ul" and the "#menuh li" width both set to 250px, and I have the main "#menuh" set to 1000px. I am using PHP to determine if a site admin is logged in. If there is, PHP inserts the admin menu to the predefined location, otherwise, PHP removes the fake tag I used to define that location. I then have PHP automatically insert a Javascript at a different predefined location, that says: "document.getElementById('menuh').style.width = $width;", $width being set to 4 when an admin is logged in, 3 when not. The above string is defined in my PHP, so the Javascript never actually sees "$width", but instead a digit. Here is the problem: I had to change my DOCTYPE because it was set to QUIRKs in IE for some reason (I changed it to strict). Once I changed the DOCTYPE, the “#menuh” refuses adjust itself. I tried changing the “#menuh” to 750px, and then the admin menu overflows to a new row! What should I do?
matthew   Thursday, August 5, 2010 08:20
 
I figured it out! Sorry for the trouble.

"Post a Comment" has been deactivated.

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