Home arrow HTML arrow Page 3 - Building a Drop-Down Menu with Nested HTML Lists
HTML

Building a Drop-Down Menu with Nested HTML Lists


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.

Author Info:
By: Alejandro Gervasio
Rating: 4 stars4 stars4 stars4 stars4 stars / 21
May 29, 2009
TABLE OF CONTENTS:
  1. · Building a Drop-Down Menu with Nested HTML Lists
  2. · Start building a drop-down menu: the structural markup
  3. · Using CSS styles to turn the menu into a functional user interface
  4. · Making the menu compatible with Internet Explorer 6 and below

print this article
SEARCH DEVARTICLES

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:

/* 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;

}

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.


blog comments powered by Disqus
HTML ARTICLES

- Does HTML5 Need a Main Element?
- Revisiting the HTML5 vs. Native Debate
- HTML5: Not for Phone Apps?
- HTML5 or Native?
- Job Hunting? Freelancer.com Lists This Quart...
- HTML5 in the News
- Report: HTML5 Mobile Performance Lags
- The Top HTML5 Audio Players
- Top HTML5 Video Tutorials
- HTML5: Reasons to Learn and Use It
- More of the Top Tutorials for HTML5 Forms
- MobileAppWizard Releases HTML5 App Builder
- HTML5 Boilerplate: Working with jQuery and M...
- HTML5 Boilerplate Introduction
- New API Platform for HTML5

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2014 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials