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 - 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">
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.