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

后端 未结 12 831
暖寄归人
暖寄归人 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 17:15

    Perhaps the simplest way of achieving what you probably really want to do is to put not put the button inside the div you don't want activated.

    Here, you have a container div, which contains a background div (the equivalent of the parent div in your original example). The background div has an active state separate from the button's.

    .container {
      position: relative;
      width: 200px;
      height: 200px;
    }
    .background {
      position: absolute;
      width: 100%;
      height: 100%;
      border: 1px solid black;
      background-color: #eee;
    }
    .background:active {
      background-color: red;
    }
    button {
      position: relative;
    }

提交回复
热议问题