When hovering over child, change css of parent
I want to change the css of the parent on hover of the Child element.
<ul id="main-menu">
<li>
<a href="#">
<i class="fa fa-building-o" aria-hidden="true"></i>
Private Limited
<i class="fa fa-caret-down"></i>
</a>
<ul class="submenu">
<li><a href="#0">Company</a></li>
<li><a href="#0">Contact</a></li>
<li><a href="#0">Industry</a></li>
</ul>
</li></ ul>
What I want is if I am on a submenu , li of the main menu will be highlighted.
+7
5 answers
As already mentioned, the parent selector does not exist, but if you acknowledge that you are already above the parent, you can achieve what you want.
Example:
#main-menu > li:hover > a
{
background-color: #F00;
}
#main-menu > li > .submenu > li:hover
{
background-color:#00F;
}<ul id="main-menu">
<li>
<a href="#">
<i class="fa fa-building-o" aria-hidden="true"></i>
Private Limited
<i class="fa fa-caret-down"></i>
</a>
<ul class="submenu">
<li><a href="#0">Company</a>
</li>
<li><a href="#0">Contact</a>
</li>
<li><a href="#0">Industry</a>
</li>
</ul>
</li>
</ ul>+21
Here you can go ..
For Apply CSS..
$("#main-menu li").mouseover(function()
{
$("#main-menu a:eq(0)").css({'color':'blue','font-weight':'bold'});
});
For Remove CSS..
$("#main-menu li").mouseout(function()
{
$("#main-menu a:eq(0)").removeAttr("style");
});
[link] ( https://jsfiddle.net/aj23whnb/ )
0
If you want to call a child with a child, use this.
.triggerDiv{
display:none;
}
.child:hover ~.triggerDiv {
display:block
}<div class="main">
<div class="parent">
<div class="child">
<p>Hello</p>
</div>
<div class="triggerDiv">
<p>I'm Here</p>
</div>
</div>
</div>0