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

前端 未结 8 2303
灰色年华
灰色年华 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: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 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;
    }    
    parent - you can hover over here and it won't trigger
    hover over the child instead!

    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)

提交回复
热议问题