How to create a basic css horizontal drop down menu

Thursday 05 February 09 by Onsitus in CSS Tutorials

In this tutorial, we will see how to create a basic horizontal drop down menu using unordered lists and nested lists.

We will use the css property visibility to hide the menu’s sub items and make them visible on the hover state of the buttons.

The css menu functions for the main browser of recent edition.
At the end of the article, I will explain how to adjust it to make it also works for Internet Explorer 6.

The basic html structure consists of unordered lists which contains a second list for the sub items:

<div id="menu">
<ul>
<li><a href="#nogo">Link 1</a>
<ul>
<li><a href="#nogo">Link 1-1</a></li>
<li><a href="#nogo">Link 1-2</a></li>
<li><a href="#nogo">Link 1-3</a></li>
</ul>
</li>
<li><a href="#nogo">Link 2</a>
<ul>
<li><a href="#nogo">Link 2-1</a></li>
<li><a href="#nogo">Link 2-2</a></li>
<li><a href="#nogo">Link 2-3</a></li>
</ul>
</li>
<li><a href="#nogo">Link 3</a>
<ul>
<li><a href="#nogo">Link 3-1</a></li>
<li><a href="#nogo">Link 3-2</a></li>
<li><a href="#nogo">Link 3-3</a></li>
</ul>
</li>
</ul>
</div>

In the following preview image, we can see our menu with no css style applied:

preview 1 del menu css dropdown

As the browsers have different way to interpret the margins and paddings, we specify a value of null for both properties for the menu (#menu), ul (unordered list and li (list items).
We apply a float:left; to create a basic horizontal css menu and a relative position which will next allow us to position the sub items in a absolute mode in relation to its containing element <li>. With the list-style:none;, we remove the default bullets added by default to the list:

#menu{
padding:0;
margin:0;
}
#menu ul{
padding:0;
margin:0;
}
#menu li{
position: relative;
float: left;
list-style: none;
margin: 0;
padding:0;
}

At this point the result is as follow:

preview 2 del menu css dropdown

Next, the css style for the links themselves (element <a>):

  • the width width:100px;
  • the height height:30px;
  • convert from inline element to block element display:block;
  • remove the text underline added by default text-decoration:none;
  • horizontal align the text link text-align: center; and vertical align line-height:30px;
  • a black background color background-color:black; and a text of white color color:white;

#menu li a{
width:100px;
height: 30px;
display: block;
text-decoration:none;
text-align: center;
line-height: 30px;
background-color: black;
color: white;
}

To add a minimum of visual effect, we change the background color from black to red on the hover state of the links inside the list items (li):

#menu li a:hover{
background-color: red;
}

A detail of the result obtained:

preview 3 del menu css dropdown

We can now position the sub items:

We use the css properties position:absolute; to remove the sub ul from the normal flux of the page and move them of 30px (top:30px; - 30px being the height specified first) from the top towards the bottom so to place them underneath the first element.

#menu ul ul{
position: absolute;
top: 30px;

Image preview:

preview 4 del menu css dropdown

Using the css property visibility:hidden; we hide the sub items:

visibility: hidden;
}

Finally, we finish our horizontal drop down css menu changing the value of the property visible from hidden to visible on the hover state:

#menu ul li:hover ul{
visibility:visible;
}

Pass over the links with mouse to see the final result:

After testing this basic horizontal drop downcss menu with all the main browser (IE7, Firefox, Opera e Safari), a problem is encountered with Internet Explorer 6 for which the hover state is completely ignored and the sub items not displayed.

As many users still use Internet Explorer 6, I tried to find the easiest solution to implement without modifying the html structure and css.

I found the solution in the book Eric Meyer on CSS which use an .htc file created by Peter Nederloff.

In brief, IE6 does not support the :hover state on othe elements then for the tag <a>, which makes this drop down menu useless.
But IE supports the so-called ‘behaviors’ (ignored by FF or other moderns browsers).
Those ‘behaviors’ allows to upload a .htc file which change the comportment of elements with an hover state using eventsonmouse.
I am not a programmer(ess?), so I didn’t start studying the script included in this .htc file which can be downloaded freely from Peter Nederloff site.

I used the version csshover2.htc for this tutorial.

Back to our menu, the .htc file which will allow this drop down menu to also function for IE6 is added in the css rules to the element body, the following way:

NB: this css rule has to be added in an external css file and not in between tag style in the head of your html page

body{
behavior: url(csshover2.htc);
}

PS: for IE6 users, the .htc file is not applied to the example in this tutorial

17 comments

Stay updated with our RSS Feed

{ 2 trackbacks }

Free css horizontal drop down menu - css example 3
02.05.09 at 10:01
How to create a basic css horizontal drop down menu | Vectormesh
04.06.09 at 19:56

{ 15 comments… read them below or add one }

Ravi 04.20.09 at 05:19

This menu is not working with IE6 plz help if you have another example that work for IE6 too.

Onsitus 04.20.09 at 07:27

Ciao Ravi,
the example posted in this article is not working with IE6 as I did not apply the .htc file needed for IE6.
You can find a working example on: http://www.onsitus.it/css-tutorials/css-dropdown-orizzontale/
The css file applied to the above page is on:
http://www.onsitus.it/css-tutorials/css-dropdown-orizzontale/stile.css
As said in the article, you can download the .htc file needed for IE6, here: http://www.xs4all.nl/~peterned/csshover.html
At the time, I wrote the article, I used the second version ‘csshover2.htc’

Still related to this article, you can download this free menu which uses the same structure and in the zip file the .htc is included:
http://css.flepstudio.org/en/css-menu/free-horizontal-drop-down-menu-3.html

Kurt 04.30.09 at 02:54

In the first line of CSS, I think you meant to put ‘float: left;’ rather than ‘display: left’

#menu li{
position: relative;
display: left; <—-Here - should be ‘float: left;’
list-style: none;
margin: 0;
padding:0;
}

Onsitus 04.30.09 at 06:42

True, I saw this error in the italian version but forgot to correct the english one!
Thanks for letting me know.

Tamouz 05.15.09 at 15:32

This is a great tutorial. Thanks.

The only thing I can’t figure out: how can I make the menu close when the user clicks on an item? Is this even possible?

Thanks again.

kim 05.27.09 at 17:07

I read http://css.flepstudio.org/en/css-menu/free-horizontal-drop-down-menu-3.html too. But I am using Blogger and having problem in add drop down menubar in it. Please guide me how to do it in Blogger?

Andy 06.03.09 at 10:44

Hi, is there anyway to make the drop-down links wider than the top buttons?

Winson 06.12.09 at 01:24

How do i center the horizontal drop down menu ? Appreciate it if anyone could guide me along.

Kathy 06.15.09 at 06:48

I am not sure it can be done in blogger as you can’t upload and leave files in blogger’s directory. If anyone knows how to get a horizontal drop-down menu in blogger. Please let me know! Thanks.

asivin 06.26.09 at 09:15

Nice staff

metalwing 08.25.09 at 13:40

Nice tuts here.Keep it up helping…

ganesh 11.25.09 at 10:19

hi, I really appriaciate your job, but iam very sorry to say that this does not work with IE6 browser..! i need to knw if there is nayway to do..?

Marcell Purham 05.01.10 at 19:08

Great tutorial. Very simple and works in most browsers

Hotal 05.05.10 at 12:32

thax very very much for this lesson

:(

James donohue 05.15.10 at 09:10

Thanks, I followed your tutorial, and it works perfect, very simple, but WORKS!! thanks!

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Previous post: 100 percent background image in css

Next post: Free css horizontal drop down menu - css example 3