• ...">

    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
    source share
    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>
    Run codeHide result
    +21
    source

    Like other posts, there is no parent selector.

    Here's how it should work:

    li:has(> i:hover) { /* styles to apply to the li tag */ }
    

    , , li i :hover. css. , .

    +6

    There is currently no way to select the parent element of an element in CSS.

    +3
    source

    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
    source

    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>
    Run codeHide result
    0
    source

    All Articles