Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

css

hover

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>
like image 825
hairynuggets Avatar asked Jan 09 '12 14:01

hairynuggets


1 Answers

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! :)

like image 118
字姓名 Avatar answered Nov 15 '22 04:11

字姓名