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

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

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

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

    I'm hovered too!
    #parent {
      position: relative;
      height: 100px;
    }
    
    /* Move button control to bottom. */
    #control {
      position: absolute;
      bottom: 0;
    }
    
    #control:hover ~ #target {
      background: red;
    }
    

    Demo Fiddle here.

提交回复
热议问题