How to keep list item in active state whilst hovering on dropdown?

可紊 提交于 2019-12-23 10:24:25

问题


I am creating a dropdown menu on my website and have more or less completed the task successfully apart from one thing.

When hovering on the dropdown menu, the hover state of the main menu link disappears due to the fact that I am no longer hovering on it.

How can I keep the active state styling on the link whilst hovering on the dropdown items?

I have copied the code to http://cssdesk.com/PZBM2 if you hover the first list item you will see the hover state I am talking about and the dropdown.

Here is the code also:

/*Main nav*/
.main_nav_container{
    margin-top:10px;
    margin-bottom:10px;
    display:block;
    float:right;
}
ul.main_nav{
    margin:0;
    padding:0;
}
ul.main_nav li{
    display:inline-block;
    margin:0;
    padding:0;
}
ul.main_nav li a{
    font-size:13px;
    display:block;
    font-weight:bold;
    position:relative;
    height:25px;
    line-height:25px;
    padding:0 13px;
    text-decoration:none;
    color:#1122cc;
    border:1px solid transparent;
}
ul.main_nav li a:hover{
    text-decoration:underline !important;
    border-top:solid 1px #ccc;
    border-left:solid 1px #ccc;
    border-right:solid 1px #ccc;
}
ul.main_nav li ul{
    display:none;
    position:absolute;
    background: #fff;
    margin:0;
    padding:0;
    border:solid 1px #ccc;
    -moz-border-radius: 4px;
    -webkit-border-radius: 4px;
    -khtml-border-radius: 4px;
    border-radius: 4px;
}
ul.main_nav li ul:hover #hover{
    border:#ccc 1px solid;
}
ul.main_nav li ul li{
    display:block;
    margin:0;
    padding:0;
    text-align:left;
}
ul.main_nav li ul li a{
    font-weight:normal;
}
ul.main_nav li:hover ul{
    display:inline;
}

HTML

<div class="main_nav_container">
    <ul class="main_nav">
        <li>
            <a id="hover" href="#">For sale</a>
            <ul>
                <li><a href="#">Property for sale</a></li>
                <li><a href="#">New homes for sale</a></li>
                <li><a href="#">Find estate agents</a></li>
            </ul>
        </li>
        <li><a href="#">To rent</a></li>
        <li><a href="#">New homes</a></li>
        <li><a href="#">House prices</a></li>
        <li><a href="#">Blog</a></li>
        <li><a href="#">Property advice</a></li>
        <li><a href="#">Contact</a></li>
    </ul>
</div>

回答1:


you should replace

ul.main_nav li a:hover{
    text-decoration:underline !important;
    border-top:solid 1px #ccc;
    border-left:solid 1px #ccc;
    border-right:solid 1px #ccc;
}

with

ul.main_nav li:hover{
    text-decoration:underline !important;
    border-top:solid 1px #ccc;
    border-left:solid 1px #ccc;
    border-right:solid 1px #ccc;
}

in your css. Enjoy! :)



来源:https://stackoverflow.com/questions/8789754/how-to-keep-list-item-in-active-state-whilst-hovering-on-dropdown

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!