简体   繁体   中英

Drop down menu on hover using CSS not working

I was wondering if anyone could see the bug in my code I am clearly not seeing. I am trying to create a simple navigation bar with a sub menu under 'brands' that appears on hover. Not sure if I am targeting the menu wrong, or what is happening. Any help would be appreciated. Thank you

<div class="nav">
        <ul>
            <li> <a href="#"> Home </a> </li>
            <li> <a href="#"> History </a> </li>
            <li> <a href="#"> Brands </a> </li>
                <ul> 
                    <li> <a href="#"> Proprietary Brands </a> </li>
                    <li> <a href="#"> Licenced Brands </a> </li>
                    <li> <a href="#"> Distributed Brands </a> </li>
                </ul>
            <li> <a href="#"> Corp. Info </a> </li>
            <li> <a href="#"> Investors </a> </li>
        </ul>
    </div>



.nav{
    width: 100%;
}

.nav ul{
    font-size: 1.2em;
    text-align: right;
    padding-right: 5%;
    padding-top: 2%;
    list-style: none;
}

.nav li {
    text-decoration: none;
    padding-left: 2%;
    display: inline-block;
}

.nav li li{
    font-size: 1em;
}

.nav li ul{
    position: absolute;
    display: none;
    width: inherit;
}

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

.nav li ul li{
    display: block;
}

Your html just a bit wrong, it should be

...
    <li>
        <ul> ... </ul>
    </li>
...

instead

...
    <li> ... </li>
    <ul> ... </ul>
    <li> ... </li>
...

That mean you close li tag that should be the container of too soon, that make your csss for .nav li ul and anything with that become invalid because no structure like that exist in your html

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