6

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?

2
  • Not sure exactly what you're going for, but try this: #TakeAction:hover + ul#actions, ul#actions:hover Commented May 8, 2013 at 23:51
  • take a look at cssmenumaker.com or cssmenubuilder.com/home or purecssmenu.com Commented May 9, 2013 at 0:19

2 Answers 2

10

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/

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you. Is there anyway to change it to onclick rather than hover?
Thanks, that's exactly what I was looking for however, there's an issue with the jsFiddle code if you could help with that...the menu stays open even after I click outside it.
Is there a reason you don't want to use Javascript?
A little bit of js is fine. I saw a lot of jquery and js solutions online, and didn't want to deal with all that code. But a little bit is fine...
2

You can just use HTML Select Tag.

Here is my solution. Fiddle: Dropdown button with CSS

html

<select name='takeation'>
  <option class='head'>Select Action</option>
  <option value='Action 1'>Action 1</option>
  <option value='Action 2'>Action 2</option>
  <option value='Action 3'>Action 3</option>
</select>

css

option.head {
  selected:selected;
  display:none;
  disabled:disabled;
}

I hope this helps.

2 Comments

Why are you styling inline? You should be using CSS as that is what the question is asking; "Can this be done with HTML & CSS". Also inline styling is pretty much a no-no, except for when you build HTML emails.
Interesting way to hide the placeholder item.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.