How do I add a horizontal submenu? can't figur

2019-09-19 13:12发布

问题:

Hey I would like to have a dropdown sub- menu in the same style, I know it's simple but I'm still new to making websites and I can't figure it out by myself.

here's my HTML file:

<nav>
        <ul>
            <li><a href="./index.html"><span class ="s2">Startpagina</span></a></li>
            <li><a href="./aanwinsten.html">Aanwinsten</a></li>
            <li><a href="./catalogus.html">Catalogus</a></li>
                <ul class="sub">
                    <li><a href="#">Pages</a></li>
                    <li><a href="#">Archives</a></li>
                    <li><a href="#">New Posts</a></li>
                </ul>
            <li><a href="./uitlening.html">Uitlening</a></li>
            <li><a href="./reservatie.html">Reservatie</a></li>
            <li><a href="./suggestie.html">Suggestie</a></li>
            <li><a href="./contact.html">Contact</a></li>
        </ul>
    </nav>

and this is my CSS file:

  nav li
{
    display: inline;
    padding-right: 20px;

}
nav {
    text-align: center;
    margin: -20px 0px 0px 0px;

}
nav ul{
    background-color: rgba(126, 4, 0, 0.79);
    border: 1px solid black;


}
nav ul li{
    display: inline;

}
nav ul li a{
    padding-left: 1em;
    padding-right: 1em;
    font-size: 12px;
    font-family: Arial, Helvetica, sans-serif;
    text-decoration: none;
    color: lightgray;
}
nav ul li a:hover{
    color: #999999;

}

EDIT: I've edited the /li problem and I've added this to my CSS:

nav ul ul{display: none; position: relative;}
nav li ul li{float:none;display: inline-block; }
nav ul li:hover ul  {display: inline-block;}

which gives this when I hoover over it: http://gyazo.com/e095151ebf644b4b44c7556193328163

回答1:

You closed the li that will hold the submenu ul too early.

Try working with this:

<nav>
        <ul>
            <li><a href="./index.html"><span class ="s2">Startpagina</span></a></li>
            <li><a href="./aanwinsten.html">Aanwinsten</a></li>
            <li><a href="./catalogus.html">Catalogus</a>
                <ul class="sub">
                    <li><a href="#">Pages</a></li>
                    <li><a href="#">Archives</a></li>
                    <li><a href="#">New Posts</a></li>
                </ul>
            </li> **<! - See the difference?-->**
            <li><a href="./uitlening.html">Uitlening</a></li>
            <li><a href="./reservatie.html">Reservatie</a></li>
            <li><a href="./suggestie.html">Suggestie</a></li>
            <li><a href="./contact.html">Contact</a></li>
        </ul>
    </nav>

JSFiddle



回答2:

The problem lies here:

    <li><a href="./catalogus.html">Catalogus</a></li>
        <ul class="sub">
            <li><a href="#">Pages</a></li>
            <li><a href="#">Archives</a></li>
            <li><a href="#">New Posts</a></li>
        </ul>
    <li><a href="./uitlening.html">Uitlening</a></li>

You need to keep the <ul> inside the previous <li>. So make it this way:

    <li><a href="./catalogus.html">Catalogus</a>
        <ul class="sub">
            <li><a href="#">Pages</a></li>
            <li><a href="#">Archives</a></li>
            <li><a href="#">New Posts</a></li>
        </ul></li>
    <li><a href="./uitlening.html">Uitlening</a></li>

And now you can give the styling by positioning the <UL> tag in such a way that when you hover your mouse over the LI it shows up.

li.drop ul {display: none; position: absolute;}
li.drop {position: relative}
li.drop:hover ul {display: block; left: auto; top: auto;}


标签: html css menu