HTML
  Home arrow HTML arrow Page 4 - Building a Drop-Down Menu with Nested HTML...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
HTML

Building a Drop-Down Menu with Nested HTML Lists
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2009-05-29

    Table of Contents:
  • Building a Drop-Down Menu with Nested HTML Lists
  • Start building a drop-down menu: the structural markup
  • Using CSS styles to turn the menu into a functional user interface
  • Making the menu compatible with Internet Explorer 6 and below

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

       · This final part of the series shows how to use nested HTML lists specifically for...
     

    HTML ARTICLES

    - Comparing Browser Response to Active Client ...
    - Testing Browser Response to Active Client Pa...
    - Active Client Pages: Completing the Code for...
    - ACP and Browsers: Setting up an Example
    - How Browsers Respond to Active Client Pages
    - Completing a Tree with Active Client Pages
    - HTML Form Verification and ACP
    - Building an ACP Tree
    - Completing an ACP 3D HTML Table Image Gallery
    - Building an ACP 3D HTML Table Image Gallery
    - A Multiple Page Image Gallery with Active Cl...
    - Building an Image Gallery with Active Client...
    - Concluding a Menu for All Browsers
    - A Vertical Menu for All Browsers
    - Downloading Long HTML Pages with ACP







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek