Prabhu Prabhu -4 years ago 91
Javascript Question

Creating a dropdown button with just HTML & CSS?

Is it possible to create a button with a dropdown menu with just HTML & CSS?

<a id="TakeAction">Take Action</a>

<ul id="actions">
<li>action 1</li>
<li>action 2</li>
...
</ul>


When the link is clicked (hover is fine too, but click is preferred), I want ul#actions to show, where I can then chose my action. I tried to do something like this, but the menu (ul#actions) disappears when the cursor moves out of the button.

ul#actions
{
display:none;
}
#TakeAction:hover + ul#actions
{
display: block;
}


Do I need javascript/jquery to do something like this?

Answer Source

Try enclose it all in a div and put the hover on that div:

HTML:

<div class="actions">
  <a id="TakeAction">Take Action</a>
  <ul id="actions">
    <li>action 1</li>
    <li>action 2</li>
  </ul>
</div>

CSS:

ul#actions
{
    display:none;
}
.actions:hover ul#actions
{
    display: block;
}

On hover: http://jsfiddle.net/gpf5n/

On click: http://jsfiddle.net/5p2SQ/

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download