On child hover change the css of Parent

后端 未结 5 2163
野性不改
野性不改 2021-02-20 02:41

I want to change the css of parent on hover of Child element.

相关标签:
5条回答
  • 2021-02-20 03:13

    If you want trigger a child element with a child element 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 讨论(0)
  • 2021-02-20 03:14

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

    0 讨论(0)
  • 2021-02-20 03:19

    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 讨论(0)
  • 2021-02-20 03:23

    As other posts say there is no parent selector.

    This is how it should work:

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

    What this does is check if li has a i with :hover state in it. If that's the case it applies the css. Unfortunately this is not supported yet..

    0 讨论(0)
  • 2021-02-20 03:28

    As already mentioned there is no parent selector but if you recognise that you are already hovering over the parent you can achieve what you want.

    A rough 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>

    0 讨论(0)
提交回复
热议问题