Style Sheets
  Home arrow Style Sheets arrow Page 3 - Building Clean Drop-Down Menus with CSS
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? 
STYLE SHEETS

Building Clean Drop-Down Menus with CSS
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 2
    2008-10-15

    Table of Contents:
  • Building Clean Drop-Down Menus with CSS
  • Building the drop-down menu's structural markup
  • Adding some useful CSS styles to make the menu work
  • Seeing the completed drop-down menu in action

  • 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 Clean Drop-Down Menus with CSS - Adding some useful CSS styles to make the menu work


    (Page 3 of 4 )

    According to the concepts deployed in the previous section, you'll realize that the current incarnation of this navigational menu is completely useless. However, this condition is about to change, since I plan to aggregate some simple CSS styles into it, which hopefully will make the menu work at least partially (more about this in a moment).

    Below I included the respective declarations of the aforementioned CSS styles, which, as I explained earlier, are responsible for displaying the drop-down menus when the mouse is positioned over a determined top-level item:


    /* reset body styles */

    body{

    padding: 0;

    margin: 0;

    background: #fff;

    }

    /* style unordered list */

    ul{

    padding: 0;

    margin: 0;

    list-style: none;

    }

    /* style menu items */

    li{

    float: left;

    position: relative;

    width: 10em;

    }

    /* position and hide drop-down menu */

    li ul{

    display: none;

    position: absolute;

    top: 1em;

    left: 0;

    }

    li > ul{

    top: auto;

    left: auto;

    }

    /* display drop-down menu */

    li:hover ul{

    display: block;

    }


    As you can see, the above CSS declarations are indeed very easy to follow, since their primary goal is first to style the list that comprises the top-level elements of the menu in question, and second to hide/show the respective menu items when a user places the mouse over one of these elements.

    The magic happens due to the following CSS declaration:


    /* display drop-down menu */

    li:hover ul{

    display: block;

    }


    Since most of standard-compatible browsers allow you to assign "hover" pseudo class to any web page element, it's pretty easy to display the respective menu items by using this neat feature. Unfortunately, IE doesn't support this, but in further articles of the series I'll be fixing this issue, so don't worry about it for the moment.

    So far, so good. At this stage I have created all of the CSS styles required to make the drop-down menu work as expected. However, all of the pieces look rather dis-articulated when analyzed separately, so in the next section I'm going to join them together. This will give you the complete source of this CSS-based drop-down menu.

    Click on the link that appears below and keep reading.

    More Style Sheets Articles
    More By Alejandro Gervasio


       · Building standard drop-down menus with CSS is a pretty straightforward task,...
       · It doesn't work on IE6 though
       · Thanks for the comments, but I guess you didn’t read the entire article, simply...
       · hallo,thanks for the article.one question, maybe newbie:the titles of the <a hrefs>...
       · Thanks for the comments. You can change the width of the menu by modifying the ul...
       · tried the code in ie7 we are developing with ref to mozilla so wanted a solution to...
       · You must be doing something wrong, since the menu works fine most browsers,...
     

    STYLE SHEETS ARTICLES

    - Image Replacement CSS Techniques
    - Using BlueTrip`s Success, Notice and Error C...
    - More Uses for the Thin and Caps CSS Classes ...
    - Styling Definition Lists with the BlueTrip C...
    - Styling Unordered and Ordered HTML Lists wit...
    - Using the BlueTrip CSS Framework`s Thin and ...
    - Adding Borders to Web Page Columns with Blue...
    - Introducing the BlueTrip CSS Framework
    - Using a Background Grid to Assist Web Page L...
    - Extending the Rule Of Thirds for Web Page La...
    - A Two-Column Web Page Layout Based on the Ru...
    - Using the Rule Of Thirds for Web Page Layout
    - Swapping Columns Using the Divine Ratio for ...
    - Using the Golden Ratio in Liquid Web Page De...
    - Fundamental Design Principles for Web Page L...







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