简体   繁体   中英

Add sub menu using css in html

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

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;
}

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM