Welcome to the third part of a three-part series that shows you how to design with nested HTML lists. Today we'll take what we've learned in the previous two parts to a logical conclusion: building a full-fledged drop-down menu. We will even make sure that this menu is compatible with older browsers, such as Internet Explorer 6.
Building a Drop-Down Menu with Nested HTML Lists - Using CSS styles to turn the menu into a functional user interface (Page 3 of 4 )
In the section that you just read, I defined the bare bones structure of the drop-down menu, which as you'll recall, was comprised of some nested HTML lists. Assuming that you already understood how this particular task was accomplished, now it's time to add to the menu's structure the CSS styles that make it work in most modern browsers, except for Internet Explorer 6 and below.
Essentially, the group of CSS styles that turn the nested lists of the menu into a functional user interface are as follows:
/* display drop-down menu (add an 'over' class attribute to list items for IE */
li:hover ul,li.over ul{
display: block;
}
#navbar li li a {
display: block;
font: normal 11px Verdana, Arial, Helvetica, sans-serif;
color: #000;
padding: 5px;
height: 15px;
text-decoration: none;
border-top: 1px solid #ccc;
border-left: 1px solid #ccc;
border-right: 1px solid #ccc;
}
#navbar li li a:hover {
color: #000;
background: #eee;
}
Despite its apparently complexity, the above CSS styles are quite simple to follow, actually. They first style the top items of the menu, then initially hide the corresponding sub items, and finally enhance the visual appearance of the menu's links.
So far, nothing unexpected, right? As you saw before, it's relatively easy to construct a drop-down menu like the one above by combining the functionality given by a few HTML lists, along with some "hover" CSS pseudo classes as well.
In addition, there's a worthwhile point to consider here: in its current incarnation, the menu will function with browsers that support this pseudo class with all the elements of a web page, like Firefox and Opera -- but Internet Explorer 6 and below will ignore this feature, and therefore the menu won't work.
Thus, to solve this small -- yet annoying -- issue, it's necessary to code a simple JavaScript snippet that makes the menu work with Internet Explorer too. This tiny client-side application will be coded in the upcoming section.
To learn how this JavaScript program will be incorporated into this drop-down menu, please visit the following segment.