Is there an opposite CSS pseudo-class to :hover?

前端 未结 4 793
借酒劲吻你
借酒劲吻你 2020-12-13 03:33

Is there a pseudo-class in CSS to specify

:not(:hover)

Or is that the only way to specify an item that is not being hovered?

I wen

4条回答
  •  鱼传尺愫
    2020-12-13 03:49

    Yes, use :not(:hover)

    .child:not(:hover){
      opacity: 0.3;
    }
    

    jsBin demo

    Another example; I think you want to: "when one is hovered, dim all other elements".

    If my assumption is correct, and assuming all your selectors are inside the same parent:

    .parent:hover .child{
        opacity: 0.2;      // Dim all other elements
    }
    .child:hover{
       opacity: 1;         // Not the hovered one
    }
    

    .child{
      display:inline-block;
      background:#000;
      border:1px solid #fff;
      width: 50px;
      height: 50px;
      transition: 0.4s;
    }
    
    .parent:hover .child{
      opacity: 0.3;
    }
    .parent .child:hover{
      opacity: 1;
    }

    Otherwise... simply use the default logic:

    .child{
       opacity: 0.2;
    }
    .child:hover{
       opacity: 1;
    }
    

提交回复
热议问题