Building a Drop-Down Menu with Nested HTML Lists - Making the menu compatible with Internet Explorer 6 and below
(Page 4 of 4 )
In the previous segment, you learned how to style a group of nested HTML lists to build a simple drop-down menu. Since its functionality relies on assigning a "hover" CSS pseudo class to the menu's items (something that's not supported by Internet Explorer 6), it's necessary to emulate this behavior via JavaScript.
Taking this requirement into account, below I included the complete source code that corresponds to this sample menu, this time incorporating the JavaScript snippet that makes it work correctly with Internet Explorer. Here's the corresponding code sample:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Drop-down menu using nested lists</title>
<style type="text/css">
/* reset body styles */
body{
padding: 0;
margin: 0;
background: #666;
}
/* style unordered list */
ul{
padding: 0;
margin: 0;
list-style: none;
}
/* style menu items */
li.topitem{
float: left;
position: relative;
width: 100px;
padding: 5px;
background: #eee;
font: bold 11px Tahoma, Arial, Helvetica, sans-serif;
color: #000;
border: 1px solid #000;
}
/* position and hide drop-down menu */
li.topitem ul{
display: none;
position: absolute;
top: 22px;
left: 0;
width: 150px;
background: #fff;
padding: 0px;
border-bottom: 1px solid #ccc;
}
li > ul{
top: auto;
left: auto;
}
/* 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;
}
</style>
<script language="javascript">
function initializeDropDownMenu(){
// get 'navbar' element
var navbar=document.getElementById('navbar');
if(!navbar){return};
// get all menu items
var menuitems=navbar.getElementsByTagName('li');
if(!menuitems){return};
for(var i=0;i<menuitems.length;i++){
// assign 'over' class attribute to each menu item on 'mouseover'
menuitems[i].onmouseover=function(){
this.className+=' over';
}
// remove 'over' class attribute to each menu item on 'mouseout'
menuitems[i].onmouseout=function(){
this.className=this.className.replace(' over','');
}
}
}
// initialize drop-down menu when web page is loaded
window.onload=function(){
if(document.all&&document.getElementById&&document.getElementsByTagName){
initializeDropDownMenu();
}
}
</script>
</head>
<body>
<ul id="navbar">
<li class="topitem">Top Item 1
<ul>
<li><a href="#">Sub Item 1</a></li>
<li><a href="#">Sub Item 2</a></li>
<li><a href="#">Sub Item 3</a></li>
<li><a href="#">Sub Item 4</a></li>
<li><a href="#">Sub Item 5</a></li>
</ul>
</li>
<li class="topitem">Top Item 2
<ul>
<li><a href="#">Sub Item 1</a></li>
<li><a href="#">Sub Item 2</a></li>
<li><a href="#">Sub Item 3</a></li>
<li><a href="#">Sub Item 4</a></li>
<li><a href="#">Sub Item 5</a></li>
</ul>
</li>
<li class="topitem">Top Item 3
<ul>
<li><a href="#">Sub Item 1</a></li>
<li><a href="#">Sub Item 2</a></li>
<li><a href="#">Sub Item 3</a></li>
<li><a href="#">Sub Item 4</a></li>
<li><a href="#">Sub Item 5</a></li>
</ul>
</li>
<li class="topitem">Top Item 4
<ul>
<li><a href="#">Sub Item 1</a></li>
<li><a href="#">Sub Item 2</a></li>
<li><a href="#">Sub Item 3</a></li>
<li><a href="#">Sub Item 4</a></li>
<li><a href="#">Sub Item 5</a></li>
</ul>
</li>
<li class="topitem">Top Item 5
<ul>
<li><a href="#">Sub Item 1</a></li>
<li><a href="#">Sub Item 2</a></li>
<li><a href="#">Sub Item 3</a></li>
<li><a href="#">Sub Item 4</a></li>
<li><a href="#">Sub Item 5</a></li>
</ul>
</li>
</ul>
</body>
</html>
As shown above, apart from the menu's markup and its CSS styles, there's an additional JavaScript function, called "initializeDropDown()" menu, which is responsible for emulating the support for "hover" CSS classes for <li> elements in Internet Explorer. In this specific case, the function loops over all the items of the menu and uses an "over" class to display/hide them alternately, in this way implementing the expected behavior when utilizing IE.
Besides, to complement the previous explanation, here's an additional image that shows the menu in two different states:

Here you have it. At this point you should have a good understanding of how nested HTML lists can be used for building standard drop-down menus. Of course, there are many other cases where nesting lists can be utilized for creating other user interfaces, but I think that hierarchical menus are a very illustrative example of the functionality of these web page elements.
Final thoughts
It's hard to believe, but we've come to the end of this series. In this group of articles, I attempted to provide you with a comprehensive introduction to building nested HTML lists, in addition to showing you different situations where you can employ them in a useful fashion.
If you're planning to build your next web site sticking to the standards (and it should be this way), then it's probable that you'll need to deal with nested lists. So go ahead and start using them. You won't be disappointed.
See you in the next web development tutorial!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |