Aamir Rizwan
Aamir Rizwan

Reputation: 887

Simplest way to create drop down menu on a web page?

I am creating a small project on ASP.NET. I want a simple drop down menu. Most of the solutions on web use jquery. Is there any simpler way or should I learn jquery ?

One more thing. The menu should work on IE.

Upvotes: 1

Views: 48545

Answers (2)

Tim M.
Tim M.

Reputation: 54359

Some of the cleanest drop down implementations I have seen are based on semantic HTML (unordered lists, nav element(s), etc.) and the CSS :hover pseudo class. Semantic structures degrade nicely when script is not available and are interpreted well when consumed by devices like screen readers.

Older versions of IE which do not support the :hover pseudo class can be accommodated with a snippet of script (no jQuery required).

Suckerfish/Son of Suckerfish is a good example of this technique.

Code/Description
Examples

Example

Here is the simplest implementation I could create which works in IE7+, Chrome, FF, etc. No script required.

Complete sample: http://jsfiddle.net/BejB9/4/

HTML
I'd wrap this in a nav tag in a finished document

<ul class="nav">
    <li>This item has a dropdown
        <ul>
            <li>Sub item 1</li>
            <li>Sub item 2</li>
        </ul>
    </li>    
    <li>Item</li>
    <li>So does this item
            <ul>
            <li>Sub item 1</li>
            <li>Sub item 2</li>
        </ul>
    </li>
</ul>

CSS

UL.nav > LI {
    list-style: none;
    float: left;
    border: 1px solid red;
    position: relative;
    height: 24px; /* height included for IE 7 */
}

UL.nav UL{
    left: -10000px;
    position: absolute;            
}

UL.nav > LI:hover UL{
    left: 0;
    top: 24px; /* IE7 has problems without this */
}

Upvotes: 4

Obmerk Kronen
Obmerk Kronen

Reputation: 15949

There is not really a need to learn jQuery in order to use it, or its plugins. You can google for "jquery drop-down menu" and you will find hundreds of sites with ready-made code and / or tutorials.

example: http://www.1stwebdesigner.com/css/38-jquery-and-css-drop-down-multi-level-menu-solutions/

All you will have to do later is construct your menu usually with a <ul><li> structure, and call the right selector.

Other option you will have is use a CSS drop-down menu - just google for it like I wrote for jQuery.

Upvotes: 0

Related Questions