CSS :: child set to change color on parent hover, but changes also when hovered itself

前端 未结 2 539
庸人自扰
庸人自扰 2020-12-08 13:00
相关标签:
2条回答
  • 2020-12-08 13:30

    If you don't care about supporting old browsers, you can use :not() to exclude that element:

    .parent:hover span:not(:hover) {
        border: 10px solid red;
    }
    

    Demo: http://jsfiddle.net/vz9A9/1/

    If you do want to support them, the I guess you'll have to either use JavaScript or override the CSS properties again:

    .parent span:hover {
        border: 10px solid green;
    }
    
    0 讨论(0)
  • 2020-12-08 13:41

    Update

    The below made sense for 2013. However, now, I would use the :not() selector as described below.


    CSS can be overwritten.

    DEMO: http://jsfiddle.net/persianturtle/J4SUb/

    Use this:

    .parent {
      padding: 50px;
      border: 1px solid black;
    }
    
    .parent span {
      position: absolute;
      top: 200px;
      padding: 30px;
      border: 10px solid green;
    }
    
    .parent:hover span {
      border: 10px solid red;
    }
    
    .parent span:hover {
      border: 10px solid green;
    }
    <a class="parent">
        Parent text
        <span>Child text</span>    
    </a>

    0 讨论(0)
提交回复
热议问题