css - :active for parent div class not working for child div

标签 css

我有一个由 divs 组成的按钮; .parent div 有子 div;一个 div 带有图标,另一个带有 span 用于文本。

<div class="parent" style="width:100px;height:100px;">
    <div class="child1" style="width:50px;height:50px;">
        <img src="#"/>
    </div>
    <div class="child2" style="width:20px;height:20px;">
        <span>hii</span>
    </div>
</div>


.parent:active{ background: red;}

这仅在我点击子 div 和父 div 内部时才有效。 但我需要整个区域。

这在 chrome 中工作正常,但在 IE 中不工作。 我需要一个同时适用于 Chrome 和 IE 的解决方案。

最佳答案

为什么不使用具有事件状态的实际按钮?

编辑: 我注意到您提到这在 IE 中不起作用,这是因为在 IE 中,可点击区域位于您放置在父 div 上的内容的后面,无论出于何种原因,老实说我不太了解;但我发现的修复包括为父 div 设置一个 ::before::after 伪类,具有以下属性:

.parent:before {
    content: "";
    display: block;
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
}

button {
  width: 100px;
  height: 100px;
}

button:active {
  background-color: red;
}

.parent {
  width: 100px;
  height: 100px;
  border: 1px solid #000;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
}

.parent:active {
  background-color: red;
  border: 1px solid #000;
}

.parent:before {
  content: "";
  display: block;
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
}
<button>
  <img src="#"/>
  <span>Text</span>
</button>

<div class="parent">
  <div class="child1" style="width:50px;height:50px;border:1px solid blue;">
    <img src="#" />
  </div>
  <div class="child2" style="width:20px;height:20px;border:1px solid green;">
    <span>hii</span>
  </div>
</div>

关于css - :active for parent div class not working for child div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54693754/

相关文章:

jquery - 如何使用 jquery 重写一个类

css - 我可以使用 CSS 过渡动画绝对定位元素吗?

css - 如何在我的案例中设置 div 边框?

html - 将 Masonry 与 Bootstrap Columns 一起使用时的包装问题

html - 在标题横幅和导航菜单之间添加一个小空间

javascript - 用javascript html5将整个图像包裹在圆柱形杯子上

html - 为什么我的 CSS @keyframe 动画不起作用?

html - 当 css ul li 元素超出 ul 时,如何匹配 ul 的动态宽度

javascript - 有没有办法让一个表选择器在 CSS 中以不同的方式为多个表工作?

html - <ul> 元素未扩展到列表大小的 CSS 问题