How to style the parent element when hovering a child element?

前端 未结 8 2193
灰色年华
灰色年华 2020-11-22 05:57

I know that there does not exist a CSS parent selector, but is it possible to style a parenting element when hovering a child element without such a selector?

To giv

相关标签:
8条回答
  • 2020-11-22 06:18

    This is extremely easy to do in Sass! Don't delve into JavaScript for this. The & selector in sass does exactly this.

    http://thesassway.com/intermediate/referencing-parent-selectors-using-ampersand

    0 讨论(0)
  • 2020-11-22 06:26

    Well, this question is asked many times before, and the short typical answer is: It cannot be done by pure CSS. It's in the name: Cascading Style Sheets only supports styling in cascading direction, not up.

    But in most circumstances where this effect is wished, like in the given example, there still is the possibility to use these cascading characteristics to reach the desired effect. Consider this pseudo markup:

    <parent>
        <sibling></sibling>
        <child></child>
    </parent>
    

    The trick is to give the sibling the same size and position as the parent and to style the sibling instead of the parent. This will look like the parent is styled!

    Now, how to style the sibling?

    When the child is hovered, the parent is too, but the sibling is not. The same goes for the sibling. This concludes in three possible CSS selector paths for styling the sibling:

    parent sibling { }
    parent sibling:hover { }
    parent:hover sibling { }
    

    These different paths allow for some nice possibilities. For instance, unleashing this trick on the example in the question results in this fiddle:

    div {position: relative}
    div:hover {background: salmon}
    div p:hover {background: white}
    div p {padding-bottom: 26px}
    div button {position: absolute; bottom: 0}
    

    Style parent image example

    Obviously, in most cases this trick depends on the use of absolute positioning to give the sibling the same size as the parent, ánd still let the child appear within the parent.

    Sometimes it is necessary to use a more qualified selector path in order to select a specific element, as shown in this fiddle which implements the trick multiple times in a tree menu. Quite nice really.

    0 讨论(0)
  • 2020-11-22 06:35

    there is no CSS selector for selecting a parent of a selected child.

    you could do it with JavaScript

    0 讨论(0)
  • 2020-11-22 06:36

    I know it is an old question, but I just managed to do so without a pseudo child (but a pseudo wrapper).

    If you set the parent to be with no pointer-events, and then a child div with pointer-events set to auto, it works:)
    Note that <img> tag (for example) doesn't do the trick.
    Also remember to set pointer-events to auto for other children which have their own event listener, or otherwise they will lose their click functionality.

    div.parent {  
        pointer-events: none;
    }
    
    div.child {
        pointer-events: auto;
    }
    
    div.parent:hover {
        background: yellow;
    }    
    <div class="parent">
      parent - you can hover over here and it won't trigger
      <div class="child">hover over the child instead!</div>
    </div>

    Edit:
    As Shadow Wizard kindly noted: it's worth to mention this won't work for IE10 and below. (Old versions of FF and Chrome too, see here)

    0 讨论(0)
  • 2020-11-22 06:37

    Another, simpler approach (to an old question)..
    would be to place elements as siblings and use:

    Adjacent Sibling Selector (+) or General Sibling Selector (~)

    <div id="parent">
      <!-- control should come before the target... think "cascading" ! -->
      <button id="control">Hover Me!</button>
      <div id="target">I'm hovered too!</div>
    </div>
    
    #parent {
      position: relative;
      height: 100px;
    }
    
    /* Move button control to bottom. */
    #control {
      position: absolute;
      bottom: 0;
    }
    
    #control:hover ~ #target {
      background: red;
    }
    

    Demo Fiddle here.

    0 讨论(0)
  • 2020-11-22 06:39

    As mentioned previously "there is no CSS selector for selecting a parent of a selected child".

    So you either:

    • use a CSS hack as described in NGLN's answer
    • use javascript - along with jQuery most likely

    Here is the example for the javascript/jQuery solution

    On the javascript side:

    $('#my-id-selector-00').on('mouseover', function(){
      $(this).parent().addClass('is-hover');
    }).on('mouseout', function(){
      $(this).parent().removeClass('is-hover');
    })
    

    And on the CSS side, you'd have something like this:

    .is-hover {
      background-color: red;
    }
    
    0 讨论(0)
提交回复
热议问题