#foo {
position: absolute;
height: auto;
top: 300px;
}
#foo:before {
content: "";
display: block;
height: 100px;
margin-top: -100px;
}
<div id="foo">
<a href="#">Link to something</a>
</div>
position: absolute
元素是否有规则将:before
伪类内的内容包含在其维度中?现在#foo height只包含锚元素。
修改 :之前有一个负的上边距,所以链接仍然会出现在正确的顶部:300px。
答案 0 :(得分:0)
是。实际上,代码中的 foo:before 正在运行。您只是看不到任何内容,因为 foo:之前中的内容样式设置为空,或者没有其他css样式可以让它以某种方式显示。
#foo {
position: absolute;
height: auto;
top: 300px;
border: 1px solid;
padding: 10px;
}
#foo:before {
content: "asd";
display: block;
height: 100px;
border: 1px solid
}
<div id="foo">
<a href="#">Link to something</a>
</div>