div {
width: 100px;
height: 100px;
background: red;
}
div:before {
content: "";
width: 100px;
height:30px;
background: yellow;
}
为什么未设置位置值(分别为相对值和绝对值)时,div 元素上方不显示 before 伪元素?
最佳答案
::before
和 ::after
伪元素默认是display:inline
,width
和height
不会影响。
您可以将伪元素设置为display:block
。
div {
width: 100px;
height: 100px;
background: red;
}
div:before {
content: "";
display: block;
width: 100px;
height: 30px;
background: yellow;
}
<div></div>
另见 What is the default display property of the :before and :after pseudo-elements?
关于html - 在伪元素未显示之前,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50009402/