Code: cssMenus

Update (04/15/2009)
I’ve completely overhauled this technique. It now uses jQuery for the IE stuff, and the CSS has been slimmed down tremendously. Be aware that this new version only works on pages with a valid DOCTYPE.

This technique is based on the fantastic Suckerfish Dropdowns.

Here’s the meat:

Menu Examples

On to the potatoes:

HTML
This can’t get any simpler. Just make a valid nested unordered list with a root id of “navmenu-h” (for horizontal) or “navmenu-v” (for vertical) and you are all set. The CSS and JavaScript rely on the names “navmenu-h” or “navmenu-v”. Here’s an example of a valid nested unordered list:

  1. <ul id="navmenu-h">
  2. <li><a href="#">Root nav item</a>
  3. <ul>
  4. <li><a href="#">Sub nav item</a></li>
  5. </ul>
  6. </li>
  7. </ul>

You can make this as simple or as complicated as you like. The CSS will take care of the prettiness.

CSS
This is the most complicated part of the entire technique. Way too complicated to list out in detail. Just open the above link, click on the View CSS link, and have a gander. I tried to add as many detailed comments as I could so that making edits would be as easy as possible.

What’s with the JavaScript?

Sadly, Internet Explorer 6 and below does not recognize the psuedo class :hover on any element except the <a> tag. To get around IE’s crappulence we have to add a bit of JavaScript for the poor souls that are still blind to the future.

The script looks at the html of the page and, on a mouseover event, assigns a iehover class to <li> tags that have a nested <ul> tag. If you viewed the example CSS it probably makes much more sense now.

This is how I call my script:

  1. <script language="JavaScript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js" type="text/javascript"></script>
  2. <!--[if gte IE 5.5]>
  3. <script language="JavaScript" type="text/JavaScript">
  4. $(document).ready(function(){
  5. $("#navmenu-h li,#navmenu-v li").hover(
  6. function() { $(this).addClass("iehover"); },
  7. function() { $(this).removeClass("iehover"); }
  8. );});
  9. </script>
  10. <![endif]-->

Two things you’ll notice: 1. There’s a link to jQuery from Google’s AJAX API site. Since I’m using jQuery to dynamically add the iehover class, we need to include the actual jQuery library. In my opinion, the easiest way to do this is to just use Google’s hosted service. 2. I’m using Microsoft IE conditional comments. By writing out the code like this it prevents browsers other than IE 5.5 or higher from loading the script and also gives us an extra hook to fix things like IE’s z-index bugs.

The bad news

That’s right… this technique does not work in every browser and I’ve only had the opportunity to test it in a select few.

  • Firefox 1.x+ : any OS (works!!!)
  • IE 5.5+ : Windows (works!!!)
  • IE Mac : any version, OS 9-X (probably doesn’t work)
  • Safari 2.0+ : OS X (works!!!)
  • Safari 1.x : OS X (probably doesn’t work)
  • Opera 7+ : any OS (works!!!)
  • Opera 1.x-6.x : any OS (probably doesn’t work)

Make Flash obey

Thanks go out to Brian on how to get around the flash “rendering on top of everything” issue… just add the following <param> if you are using this menu system near a flash object and IE is giving you problems:

  1. <param name="wmode" value="transparent" />

Working examples

Menu Examples

Enjoy! and remember, this technique is not my original idea. The modified code is here for all to use and share. I do not take any credit for the creation of this method.


Recent Blog

  • No More

RSS

©1992-2014 qrayg.com | all rights reserved.