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

前端 未结 8 2366
灰色年华
灰色年华 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: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;
    }
    

提交回复
热议问题