html - 伪元素后不显示框阴影

标签 html css

我正在尝试制作一个类似地铁的菜单,当用户将鼠标悬停在每个图 block 上时,我希望它显示一个框阴影。现在我让它工作了,但我读到最好在 ::after 伪元素上使用它会更流畅,但它似乎不起作用。这是 jsFiddle .

p.tile {
  text-align: center;
  padding: 5rem;
  background: #58588E;
  color: white;
  margin: 2rem;
  box-sizing: border-box;
}
p.tile::after {
  content: "";
  box-shadow: 3px 5px 20px -1px rgba(0, 0, 0, 0.8);
  opacity: 0;
  transition: opacity 0.3s ease-in-out;
}
p.tile:hover::after {
  opacity: 1;
}
<a href="#" class="tile-link">
  <p class="tile">My Progress</p>
</a>

最佳答案

问题是 ::after 伪元素实际上没有任何宽度或高度。可以在容器上设置position:relative,设置position:absolute + left:0, right:0top:0bottom:0 技巧使其与容器大小相同。

p.tile {
  text-align: center;
  padding: 5rem;
  background: #58588E;
  color: white;
  margin: 2rem;
  box-sizing: border-box;
  /* new */
  position: relative;
}
p.tile::after {
  content: "";
  box-shadow: 3px 5px 20px -1px rgba(0, 0, 0, 0.8);
  opacity: 0;
  transition: opacity 0.3s ease-in-out;
  /* new */
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
}
p.tile:hover::after {
  opacity: 1;
}
<a href="#" class="tile-link">
  <p class="tile">My Progress</p>
</a>

关于html - 伪元素后不显示框阴影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36379426/

相关文章:

jQuery:将 "Filter"转换为 "Contains"以获得文本字符串的更多匹配项

html - 任意子字符串属性值选择器在 IE7 中不起作用

html - 推特 Bootstrap : nav tabs and pills not contained within hero example 'container' element

html - 如何让我的 flexbox 布局占用 100% 的垂直空间?

jquery - 第 n 个 child 不工作

html - 图像 slider - 循环不起作用

html - 具有背景颜色的表头的 CSS 圆 Angular

css - 而不是表格,我设法获得了一个带有 div 和 Bootstrap 的响应式表格,但最大高度不起作用

javascript - 在javascript中更改按钮文本onclick

html - 如果行低于特定高度,则固定位置