【问题标题】:::after doesn't seem to do what I'm expecting? [duplicate]::after 似乎没有达到我的预期? [复制]
【发布时间】:2016-03-27 20:08:03
【问题描述】:

我可能误解了 CSS 中的 ::before::after 功能。

我想要实现的是一个 200x200 像素的盒子,然后在右上角会有另一个盒子 (24x24)。这是我所拥有的:

https://jsfiddle.net/xd6L3h6v/

<div id="foo">bla test</div>

#foo {
    position: relative;
    width: 200px;
    height: 200px;
    background: green;
}
#foo::before {
    position: absolute; top: 0; left: 0;
    background: red;
    width: 24px;
    height: 24px;
}

但是,这不起作用。当我在 Firefox 中检查它时,我在 Firebug 的 DOM 检查器中看不到 ::before 部分。

我哪里错了?

谢谢!

【问题讨论】:

  • Firebug 在 DOM 检查器中不显示伪元素。 Chrome 确实...

标签: css pseudo-element


【解决方案1】:

您只需添加content: '';

#foo {
  position: relative;
  width: 200px;
  height: 200px;
  background: green;
}
#foo::before {
  position: absolute; top: 0; left: 0;
  background: red;
  width: 24px;
  height: 24px;
  content: '';
}
&lt;div id="foo"&gt;bla test&lt;/div&gt;

【讨论】:

  • ARGH...现在我觉得自己很愚蠢!不敢相信我错过了那一点。它是那些日子之一:/它不会让我接受这个答案,但我会尽快接受。
  • 无需删除问题 - 它可以帮助将来遇到相同问题的其他人:)
猜你喜欢
  • 2020-04-22
  • 1970-01-01
  • 2020-09-15
  • 2016-01-24
  • 1970-01-01
  • 2022-12-17
  • 1970-01-01
  • 2021-06-07
  • 2013-02-10
相关资源
最近更新 更多