【发布时间】:2019-01-22 17:43:29
【问题描述】:
我正在查看一个 html + css 按钮,但我对为什么将 before 伪元素与 hover 伪类一起使用感到困惑。
.anchor-style {
font-size: 18px;
letter-spacing: 2px;
text-transform: uppercase;
display: inline-block;
text-align: center;
width: 270px;
font-weight: bold;
padding: 14px 0px;
border: 3px solid #ff0072;
border-radius: 2px;
position: relative;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.16), 0 3px 6px rgba(0, 0, 0, 0.1);
}
.anchor-style::before {
-webkit-transition: 0.5s all ease;
transition: 0.5s all ease;
position: absolute;
top: 0;
left: 50%;
right: 50%;
bottom: 0;
opacity: 0;
content: '';
background-color: #ff0072;
z-index: -2;
}
.anchor-style:hover::before {
-webkit-transition: 0.5s all ease;
transition: 0.5s all ease;
left: 0;
right: 0;
opacity: 1;
}
<a class="anchor-style" target="_blank" href="http://www.infinity2o.com">
Say Hi to My New Matches
</a>
锚标签上已经有了常规样式,为什么还需要使用 before 伪元素进行更多样式设置?
我尝试查看之前的伪元素文档,并了解到它对于在每个 p 标签之前添加元素很有用......但我不明白它是如何被不同地解释为按钮的。
【问题讨论】:
-
伪元素正在创建背景效果,只需阅读CSS并检查每个属性,更改它们,测试等,您就会明白......顺便说一句我们可以不用
-
您可以尝试在没有
:before的情况下应用样式并比较结果。 -
如果你要否决我的问题,至少告诉我为什么
-
@Hunter690 仅供参考,您可能被正在查看您问题的第一部分并认为它过于主观或会提供主观答案的人投反对票。 SO 希望所有问题都非常具体,并带有明确的问题,并提示非常具体的数据驱动的答案。
标签: html css hover pseudo-element pseudo-class