`:hover::before` 的目的是什么?
What's the purpose of `:hover::before`?
我正在查看 html + css 按钮,但我很困惑为什么 before 伪元素与 hover 伪元素一起使用 class。
.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 标签之前添加元素很有用...但我不明白它对按钮的解释有何不同。
动画效果。 ::before
的使用也是cleaner.
The ::before selector inserts something before the content of each selected element(s).
所以你可以把::before
伪想成一个空的div
。以下等效于没有 ::before
伪
.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);
}
.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">
<div class="before"></div>
Say Hi to My New Matches
</a>
我正在查看 html + css 按钮,但我很困惑为什么 before 伪元素与 hover 伪元素一起使用 class。
.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 标签之前添加元素很有用...但我不明白它对按钮的解释有何不同。
动画效果。 ::before
的使用也是cleaner.
The ::before selector inserts something before the content of each selected element(s).
所以你可以把::before
伪想成一个空的div
。以下等效于没有 ::before
伪
.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);
}
.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">
<div class="before"></div>
Say Hi to My New Matches
</a>