CSS: Prevent parent element getting :active pseudoclass when child element is clicked

后端 未结 12 893
暖寄归人
暖寄归人 2021-01-07 16:19

JSFiddle

When you click the button, you see that :active pseudoclass is triggered for the parent div. Is ther

12条回答
  •  春和景丽
    2021-01-07 16:51

    Instead of div:active {...} you should code div:active:not(:hover) {...} and the background-color stays untouched.

    (old snippet removed)

    UPDATE

    To keep the main div behaviour intact and a more generic approach I usually create several layers.

    Check the snippet below, toggling to green is just to prove that it works while position and abolute are just quick and dirty for now:

    #layer-data {
      width: 200px;
      height: 200px;
      background-color: #eee;
      border: 1px solid black;
    }
    #layer-data:active {
      background-color: red
    }
    #layer-btns:active {
      background-color: green
    }
    #layer-btns {
      z-index: 1;
      position: absolute;
      top: 1px;
      left: 1px;
      background: transparent;
      padding: 5px;
      width: auto;
      height: auto
    }
    #layer-data {
      z-index: 0;
      position: absolute;
      top: 0;
      left: 0;
      text-align: center;
      line-height: 200px
    }


    some data-layer

提交回复
热议问题