1

I'm trying to add a sub menu using css. When I hover my mouse over the a link "Top Menu", I want a sub menu to appear. See the code at here

    <div id="navbar"> <span class="navbutton">
                <a id="button-1" href="#">Shop</a>
            </span>
 <span class="navbutton">
                   <a href="#">
                       Test1</a>   
                </span>
 <span class="navbutton"><a id="button-3" href="#">Top Menu</a>
    </span>
 <span class="navbutton"><a id="button-4" href="#">Test1</a>
    </span>
 <a class="linefreak"></a><a class="linefreak"></a>
 <span class="navbutton"><a id="button-5" href="#">Test2</a>  
    </span>
</div>

Any help is much appreciated. Thanks

2 Answers 2

2

Demo Fiddle

The below should get you started- note you're likely better off using lists (ul, li) for this type of setup.

HTML

<ul>
    <li>Shop</li>
    <li>Test1</li>
    <li>Top Menu
        <ul>
            <li>Sub Item</li>
            <li>Sub Item</li>
            <li>Sub Item</li>
            <li>Sub Item</li>
            <li>Sub Item</li>
        </ul>
    </li>
    <li>Test1</li>
    <li>Test2</li>
</ul>

CSS

ul {
    list-style:none;
    background:green;
}
ul, li {
    margin:0;
    padding:0;
}
li {
    display:inline-block;
    padding:10px 10px;
}
ul li ul {
    display:none;
    position:absolute;
}
ul li ul li {
    display:block;
}
ul li:hover ul {
    display:block;
}
Sign up to request clarification or add additional context in comments.

2 Comments

This is not keyboard accessible but still... +1
Adding "ul li a:hover + ul{display: block;}" would be helpful
0

SW4 is right.. and for your example here the fiddle:

http://jsfiddle.net/fm7Ct/4/

.navbutton:hover #button-3+.submenu{
    display:inline;
}
.submenu{
    display:none;
}

Comments

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.