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

前端 未结 8 2199
灰色年华
灰色年华 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:41

    A simple jquery solution for those who don't need a pure css solution:

        $(".letter").hover(function() {
          $(this).closest("#word").toggleClass("hovered")
        });
    .hovered {
      background-color: lightblue;
    }
    
    .letter {
      margin: 20px;
      background: lightgray;
    }
    
    .letter:hover {
      background: grey;
    }
    
    
    T
    E
    S
    T

提交回复
热议问题