css - Using::after css 在下面不工作

标签 css

肯定有一些我遗漏的东西,但我试图在我的 css 中使用 ::after,但不幸的是它不起作用。

我的CSS代码如下。

.test {
  height: 40px;
  width: 40px;
  background: #444;
}
.test::after {
  position: relative;
  top: 15px;
  height: 240px;
  width: 240px;
  background: red;
}
<div class="test"></div>

最佳答案

你只需要将content: '' 添加到伪类:after 或:before 并将位置设置为绝对。

.test {
  height: 40px;
  width: 40px;
  background: #444;
  position:relative;
}
.test:after {
  position: absolute;
  top: 15px;
  height: 240px;
  width: 240px;
  background: red;
  content: ''
}
<div class="test"></div>

但是如果你想你可以在没有绝对值的情况下使用它,只需向它添加一些 float ,因为伪类的生成就像在父节点内部一样。

.test {
  height: 40px;
  width: 40px;
  background: #444;
  position:relative;
}
.test:after {
  content: '';
  background: red;
  width: 10px;
  height: 10px;
  float: right;
  
}
<div class="test"></div>

但是如果你需要像图标一样使用它,在 block 内更好的方法是使用绝对值。

关于css - Using::after css 在下面不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39857687/

相关文章:

javascript - 脚本内联工作但是当我移动到外部时不起作用?

html - 在 Play Framework 中,如何将图像放在另一个图像之上?

javascript - CSS:在计时器上暂停动画

jquery - 单击下拉菜单

php - 带有 echo 的窗口背景图像显示

html - 滚动时 body 背景在移动设备上重叠 Bootstrap 列?

css - 我可以使用 :hover to trigger a CSS3 animation (or transition) which keeps running even when the mouse is no longer hovering 吗

javascript - 使用 css 自定义链接

圆圈中的 CSS Center 文本

HTML img 有不需要的边缘