New Document Template
with Dropdown Menu

I have been looking into adding drop down menus to a web site I am building at present and I have found some examples of coding them using Cascading Style Sheets instead of complex JavaScript which is very browser dependent. I first found it on the Met Offfice web site where I was looking at the source code. The original idea was from Patrick Griffiths and Dan Webb and the original Suckerfish Dropdowns article published in A List Apart proved to be a popular way of implementing lightweight, accessible CSS-based dropdown menus that accommodated Internet Explorer by mimicking the :hover pseudo-class.

Since then they have been considerably refined and involve just a dozen lines of JavaScript to bring compliance to the CSS standards to Internet Explorer, have greater compatibility (they now work in Opera and Safari without a hack in sight) and can have multiple-levels. The latest information can be found at HTML Dog and there is generator for advanced versions at PixoPoint .

I have put the CSS required into a file which can be included on every page where the Dropdowns are needed and likewise the JavaScript the contents of the files follow.

/* This is the barebones original code from Patrick Griffiths and Dan Webb at http://htmldog.com/articles/suckerfish/dropdowns/ . This turns any unordered list with an id of sfnav into a horizontal multilevel dropdown menu - ie where the first <ul> has been replaced by <ul id="nav">.

#nav, #nav ul { /* all lists */
padding: 0;
margin: 0;
list-style: none;
line-height: 1;
}

#nav a {
display: block;
width: 10em;
}

#nav li { /* all list items */
float: left;
width: 10em; /* width needed or else Opera goes nuts */
}

#nav li ul { /* second-level lists */
position: absolute;
background: orange;
width: 10em;
left: -999em; /* using left instead of display to hide menus because display: none isn't read by screen readers */
}

#nav li ul ul { /* third-and-above-level lists */
margin: -1em 0 0 10em;
}

#nav li:hover ul ul, #nav li:hover ul ul ul, #nav li.sfhover ul ul, #nav li.sfhover ul ul ul {
left: -999em;
}

#nav li:hover ul, #nav li li:hover ul, #nav li li li:hover ul, #nav li.sfhover ul, #nav li li.sfhover ul, #nav li li li.sfhover ul { /* lists nested under hovered list items */
left: auto;
}

 

/* This is my current CSS include file (sf.css) with the information to turn any nested unordered list with an id of sfnav into a multiple level horizontal menu - ie where the first <ul> has been replaced by <ul id="sfnav">. This is based on code from Patrick Griffiths and Dan Webb at http://htmldog.com/articles/suckerfish/dropdowns/with modifications based round those at http://pixopoint.com/suckerfish_css/ to allow choices of text and background colours with and without hover. */

#sfnav {
background:#a21f1f ;
font-size:14px;
font-family:arial,sans-serif;
font-weight:bold;
width:100%;
}
#sfnav, #sfnav ul {
float:left;
list-style:none;
line-height:22px;
padding:0;
margin:0;
width:100%;
}
#sfnav a {
display:block;
color:#f2e4c6;
text-decoration:none;
padding:0px 4px;
}
#sfnav li {
float:left;
border-right: 2px solid #f2e4c6;
padding:0;
}
#sfnav ul {
position:absolute;
left:-999em;
height:auto;
width:141px;
font-weight:normal;
margin:0;
line-height:1;

}
#sfnav li li {
width:139px;
font-weight:bold;
border:none;
}
#sfnav li li a {
padding:4px 10px;
width:120px;
font-size:12px;
color:#f2e4c6;
}
#sfnav li ul ul {
margin:-21px 0 0 140px;
}
#sfnav li li:hover {
background:#da0909;
}
#sfnav li ul li:hover a, #sfnav li ul li li:hover a, #sfnav li ul li li li:hover a, #sfnav li ul li li li:hover a {
color:#ffffff;
}
#sfnav li:hover a, #sfnav li.sfhover a {
color:#ffffff;
}
#sfnav li:hover li a, #sfnav li li:hover li a, #sfnav li li li:hover li a, #sfnav li li li li:hover li a {
color:#f2e4c6;
}
#sfnav li:hover ul ul, #sfnav li:hover ul ul ul, #sfnav li:hover ul ul ul ul, #sfnav li.sfhover ul ul, #sfnav li.sfhover ul ul ul, #sfnav li.sfhover ul ul ul ul {
left:-999em;
}
#sfnav li:hover ul, #sfnav li li:hover ul, #sfnav li li li:hover ul, #sfnav li li li li:hover ul, #sfnav li.sfhover ul, #sfnav li li.sfhover ul, #sfnav li li li.sfhover ul, #sfnav li li li li.sfhover ul {
left:auto;
background:#a21f1f;
}
#sfnav li:hover, #sfnav li.sfhover {
background:#da0909;
}

 

// JavaScript include file (sf.js) with the code to mimic the :hover pseudo class for Internet Explorer by Patrick Griffiths and Dan Webb at http://htmldog.com/articles/suckerfish/dropdowns/

sfHover = function() {
var sfEls = document.getElementById("sfnav").getElementsByTagName("LI");
for (var i=0; i<sfEls.length; i++) {
sfEls[i].onmouseover=function() {
this.className+=" sfhover";
}
sfEls[i].onmouseout=function() {
this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
}
}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

The navigation bar at the top provides an example of the use of the two include files and provides a multilevel drop down menu.
Copyright © Peter and Pauline Curtis
Most recent significant revision: 30th May, 2008
Valid HTML 4.01