CSS Menus

This uses just CSS 2 to turn nested lists into a working menu and does not use any JavaScript (except in IE 5.5-6 on windows where I use the proprietary DHTML behaviours, because its CSS handling is not good enough to work this menu without script)

This works in Mozilla 1+, Netscape 7+, Opera 7+, Safari build 60+, OmniWeb 4.5+, Konqueror 3.2+, iCab 3+ and IE 5.5+ on Windows (using DHTML behaviours)

css-dhtml-menu

This produces a styled nested list in IE 4, Netscape 4, OmniWeb 4.2-, iCab 2- and WebTV
This does not function as a menu or a complete list in Netscape 6, Opera 6-, Konqueror 3.0-, Konqueror 3.1 (bug), Internet Explorer on Mac, ICEbrowser, Escape and Clue browser
If you are interested in making it horizontal, try CrazyTB’s menu. It does not work in IE (IE’s CSS sucks, so this is nothing new), but it does work very well in the others.
This requires browsers to be able to understand the > selector, to support :hover over li elements, to be able to correctly position li elements relatively, and ul elements absolutely, to support the display style, and to be able to change display when :hover is detected. Alternatively, a conditional comment and DHTML behaviours make it work in IE 5.5-6. See below for all the CSS used to make it work – substantially smaller than the equivalent JavaScript.
It does not need ANY JavaScript knowledge to set it up. Browsers that do not understand it will simply show a nested list so that the menu could still be used – with JavaScript menus, these browsers would show nothing. With many browsers, the menu will work, even if the user has disabled JavaScript. You can make any nested list into a menu simply by making its class ‘makeMenu’.

To position this menu elsewhere on the page, all you need to do is make ul.makeMenu positioned using CSS positioning, and changing the colours is easy using the stylesheets and the HTC file. To make the menu open to the left instead of to the right, just change the 78px to -78px and 80px to -80px.

<style type=”text/css”>
ul.makeMenu, ul.makeMenu ul {
width: 80px; /* sets the size of the menu blocks */
border: 1px solid #000; /* puts a black border around the menu blocks */
background-color: #8aa; /* makes the menu blocks mint green – a bg-color MUST be included for IE to work properly! */
padding-left: 0px; /* stops the usual indent from ul */
cursor: default; /* gives an arrow cursor */
margin-left: 0px; /* Opera 7 final’s margin and margin-box model cause problems */
}
ul.makeMenu li {
list-style-type: none; /* removes the bullet points */
margin: 0px; /* Opera 7 puts large spacings between li elements */
position: relative; /* makes the menu blocks be positioned relative to their parent menu item
the lack of offset makes these appear normal, but it will make a difference
to the absolutely positioned child blocks */
color: #fff; /* sets the default font colour to white */
}
ul.makeMenu li > ul { /* using the > selector prevents many lesser browsers (and IE – see below) hiding child ULs */
display: none; /* hides child menu blocks – one of the most important declarations */
position: absolute; /* make child blocks hover without leaving space for them */
top: 2px; /* position slightly lower than the parent menu item */
left: 80px; /* this must not be more than the width of the parent block, or the mouse will
have to move off the element to move between blocks, and the menu will close */
}
ul.makeMenu li:hover, ul.makeMenu li.CSStoHighlight {
background-color: #ffa; /* gives the active menu items a yellow background */
color: #000; /* makes the active menu item text black */
}
ul.makeMenu ul.CSStoShow { /* must not be combined with the next rule or IE gets confused */
display: block; /* specially to go with the className changes in the behaviour file */
}
ul.makeMenu li:hover > ul { /* one of the most important declarations – the browser must detect hovering over arbitrary elements
the > targets only the child ul, not any child uls of that child ul */
display: block; /* makes the child block visible – one of the most important declarations */
}
/* and some link styles */
ul.makeMenu li a { color: #fff; display: block; width: 100%; text-decoration: underline; }
ul.makeMenu li a:hover, ul.makeMenu li a.CSStoHighLink { color: #000; }
ul.makeMenu li:hover > a { color: #000; } /* supports links in branch headings – should not be display: block; */
</style>
<!–[if gt IE 5.0]><![if lt IE 7]>
<style type=”text/css”>
/* that IE 5+ conditional comment makes this only visible in IE 5+ */
ul.makeMenu li { /* the behaviour to mimic the li:hover rules in IE 5+ */
behavior: url( IEmen.htc );
}
ul.makeMenu ul { /* copy of above declaration without the > selector, except left position is wrong */
display: none; position: absolute; top: 2px; left: 78px;
}
</style>
<![endif]><![endif]–>

<ul class=”makeMenu”>
<li>>> Lips
<ul>
<li><a href=”cat.html”>Cat</a></li>
<li><a href=”rabbit.html”>Rabbit</a></li>
<li><a href=”dingo.html”>Dingo</a></li>
</ul>
</li>
<li>>> Ears
<ul>
<li>Elephant >>
<ul>
<li><a href=”indian.html”>Indian</a></li>
<li><a href=”african.html”>African</a></li>
</ul>
</li>
<li><a href=”monkey.html”>Monkey</a></li>
<li><a href=”dog.html”>Dog</a></li>
</ul>
</li>
<li>>> Eyes
<ul>
<li><a href=”pig.html”>Pig</a></li>
<li><a href=”bird.html”>Bird</a></li>
<li><a href=”worm.html”>Worm</a></li>
</ul>
</li>
<li><a href=”accessibleLink.html”>>> Noses</a>
<ul>
<li><a href=”bat.html”>Bat</a></li>
<li><a href=”fish.html”>Fish</a></li>
<li><a href=”panther.html”>Panther</a></li>
</ul>
</li>
</ul>

Share this Post:
Digg Google Bookmarks reddit Mixx StumbleUpon Technorati Yahoo! Buzz DesignFloat Delicious BlinkList Furl

No Responses to “CSS Menus”

Leave a Reply:

Name (required):
Mail (will not be published) (required):
Website:
Comment (required):
XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>