Pure CSS hover menu

Pure CSS-driven hover menu - image

You can create hover menus like the above without any javascript code by using the pseudo-class :hover. To make it happen, you need the following 2 components:

  • Menu element

    This would preferrably be an unordered list.

  • Trigger element

    This element would contain the menu, and trigger the showing and hiding of it when rolled over.

Let's say that you've given a class called trigger to your trigger elements, and menu to your menus. To enable the menus to show and hide on rollovers, just add these lines to your stylesheet:

.trigger ul.menu
{
    display: none;
}
.trigger:hover ul.menu
{
    display: block;
}

To explain the code above:

  • ul elements within an element with the class trigger are not displayed; and
  • ul elements within an element with the class trigger are displayed as a block element on hover-over.

This is it, and the rest is pretty much just styling up the menus.

To see it in action, check out this live sample.

Please note that the hover menus only work on browsers that support the use of the pseudo-class :hover on your Trigger elements. Internet Explorer 6 unfortunately does not support it.

Hope this helps someone.

Valid XHTML 1.0 Transitional

Published by Shingo Tamura on 1/6/2009 11:13 PM Posted in HTML CSS Tutorial

Rate This Posting

 

Comments

  • Re: Pure CSS hover menu

    very good

    Comment from sivas resimleri on 4/19/2009 6:10 AM

  • Re: Pure CSS hover menu

    Thank you, your technique saved me a lot of time!

    Comment from Juliana Garcia Sales on 5/4/2009 4:06 AM

  • Re: Pure CSS hover menu

    Useful technique brought across in a simple, straightforward manner. ありがとう!

    Comment from Shanshui Liu on 9/7/2009 6:15 PM

  • Re: Pure CSS hover menu

    neat and simple approach. this is good than relying on Javascript since other browsers which doesn't use the same javascript in computers (like iPhone, PSP, etc). with this, hovering will still show the desired effects. javascript should handle DOM manipulation and some AJAX request.

    Comment from RichieRyanRReyes on 11/10/2009 7:49 PM

  • Re: Pure CSS hover menu

    I'm trying to convert this into a horizontal list. I am having trouble with the positioning of the popup menu... Anyone able to help?

    Comment from Tom on 2/4/2010 1:28 AM

  • Re: Pure CSS hover menu

    Hi Tom,

    If you are just wanting to turn the popup menus into horizontal ones, you can add float: left; to this rule:

    .trigger ul.menu li
    {
        padding: .1em .5em;
        float: left;
    }

    This way you can position the popup menus just like the vertical ones.

    Hope this answers your question.

    Comment from Shingo Tamura on 2/4/2010 1:48 AM

Leave a Comment

Some HTML is allowed.

If ticked Name, Email Address, and Url fields will be pre-filled the next time.

         

Recent ASP.NET Forums Posts