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

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

    This solution depends fully on the design, but if you have a parent div that you want to change the background on when hovering a child you can try to mimic the parent with a ::after / ::before.

    design x

    CSS:

    .item {
        background: blue;
        border-radius: 10px;
        position: relative;
        z-index: 1;
    }
    .item span.icon-cross:hover::after {
        background: DodgerBlue;
        border-radius: 10px;
        display: block;
        position: absolute;
        z-index: -1;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        content: "";
    }
    

    See a full fiddle example here

提交回复
热议问题