Pregunta

Sorry if this is a silly question but I'm looking to add a dropdown list of items to this navbar. It's in a navbar.php file and here is the code:

<link rel="stylesheet" href="/styles/navbar.css" type="text/css" /> <!-- navbar styles -->

<ul id="nav">
    <li><a href="/index.php">Home</a></li>
    <li><a href="/item1/">Item 1</a></li>
    <li><a href="/item2/">Item 2</a></li>
    <li><a href="/item3/">Item 3</a></li>
    <li><a href="/item4/">Item 4</a></li>
</ul>

Here's the navbar.css file:

#nav 
{
    width: 100%;
    float: left;
    margin: 0 0 3em 0;
    padding: 0;
    list-style: none;
    background-color: #242424;
    border-bottom: 1px solid #ccc; 
    border-top: 1px solid #ccc; 
    position:fixed;
    top:0px;
}

#nav li 
{
    float: left; 
}

#nav li a 
{
    display: block;
    padding: 8px 15px;
    text-decoration: none;
    font-weight: bold;
    color: #7ACC01;
    border-right: 1px solid #ccc; 
}

#nav li a:hover 
{
    color: #c00;
    background-color: #fff; 
}

So my question is, is there a simple way of adding a dropdown list of items to "Item 4" for example, where the dropdown menu will appear on mouseover?

¿Fue útil?

Solución

I would like you to learn how we do that, instead of giving you the fish. With a rod, you can always fish by yourself.

Look at what I have done in this fiddle:

http://jsfiddle.net/jLkeH/

It actually comes to this:

nav ul ul {
    display: none;
}

    nav ul li:hover > ul {
        display: block;
    }

You have to hide ul you want to toggle and set it back to visible if someone hovers on it.

PS: as you can see, I used HTML5, which is recommended, because it is (more) semantic.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top