【发布时间】:2020-01-24 16:58:52
【问题描述】:
我有一个简单的问题,我似乎找不到答案,可能是我没有在 Google 中输入正确的搜索字词,而是在试图获得更好的理解时挠了挠头对此,我仍然空手而归。
我有一个span 元素,它有一个::before 伪元素,没什么特别的,只是一个简单的圆圈。但是,我注意到我必须绝对定位伪元素才能使其可见。我发现这很奇怪,并且不确定我是否只是不完全理解这种性质的伪元素,或者我是否错误地实现了这个想法。
不管怎样,这里有一个有和没有绝对定位的例子。
html,
body {
margin: 0;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
}
span {
margin-bottom: 15px;
}
.no-absolute::before,
.absolute::before {
content: '';
width: 10px;
height: 10px;
margin-right: 5px;
border-radius: 50%;
background-color: #f33;
cursor: pointer;
}
.absolute {
position: relative;
}
.absolute::before {
position: absolute;
top: 4px;
left: -15px;
}
<span class="no-absolute">No Absolute</span>
<span class="absolute">Absolute</span>
为什么我的::before 伪元素必须绝对定位才能可见?
【问题讨论】:
-
@HarunYilmaz
::before伪类已经有content: '';。 -
因为你不能将高度设置为内联元素和绝对位置强制显示阻塞
标签: html css visibility pseudo-element