按钮中的文本进入 :before 状态
Text in button gone in :before state
我正在使用此 codepen 中的第三个按钮。
.btn {
line-height: 50px;
height: 50px;
text-align: center;
width: 250px;
cursor: pointer;
}
.btn-three {
color: #FFF;
transition: all 0.5s;
position: relative;
}
.btn-three::before {
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 1;
background-color: rgba(0,0,0,1);
transition: all 0.3s;
}
.btn-three:hover::before {
opacity: 0 ;
transform: scale(0.5,0.5);
}
.btn-three::after {
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 1;
opacity: 0;
transition: all 0.3s;
border: 1px solid rgba(255,255,255,0.5);
transform: scale(1.2,1.2);
}
.btn-three:hover::after {
opacity: 1;
transform: scale(1,1);
}
每当我将按钮背景的不透明度更改为完全黑色时,文本在 :before 状态下完全消失。
有什么原因吗?似乎文本与按钮在同一个 'layer' 中,而不是在按钮上方
如果你想保持黑色不透明度并让文本一直到前面,那么你需要 "force" 使用 z-index
将文本放在前面
.btn-three span {
z-index: 2;
display: block;
position: absolute;
width: 100%;
height: 100%;
}
.btn-three::before {
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 1;
background-color: rgba(0,0,0,1);
transition: all 0.3s;
}
如果不将跨度设置在 div 以上,您的文本将始终落后于它,因为您的 ::before
状态的 z-index 设置在文本上方。
这将始终使它消失,将 z-index 设置为 -1 会将它发送到后面,但它不会再使您的块变黑
.btn-three::before {
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: -1;
background-color: rgba(0,0,0,1);
transition: all 0.3s;
}
我建议将锚点 link 移到按钮标签之外,并使用 z-index 将文本强制放在前面。
CSS:
.btn-three {
transition: all 0.5s;
position: relative;
line-height: 25px;
height: 50px;
align-items: center;
text-align: center;
width: 250px;
cursor: pointer;
}
.btn-three::before {
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 1;
background-color: rgba(16, 84, 35, 1);
transition: all 0.3s;
}
.btn-three:hover::before {
opacity: 0 ;
transform: scale(0.5,0.5);
}
.btn-three::after {
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
opacity: 0;
transition: all 0.3s;
border: 1px solid rgba(0, 0, 0, 0.5);
transform: scale(1.2,1.2);
z-index: 2;
}
.three-text {
position: relative;
margin-top: -10px;
z-index: 1;
display: block;
}
.btn-three:hover::after {
opacity: 1;
transform: scale(1,1);
}
.button-wrapper {
padding-top: 3%;
text-align: center;
padding-bottom: 3%;
}
HTML:
<div class="button-wrapper">
<button class="btn btn-three" />
<a class="three-text">HERE YOUR TEXT</a>
</div>
我正在使用此 codepen 中的第三个按钮。
.btn {
line-height: 50px;
height: 50px;
text-align: center;
width: 250px;
cursor: pointer;
}
.btn-three {
color: #FFF;
transition: all 0.5s;
position: relative;
}
.btn-three::before {
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 1;
background-color: rgba(0,0,0,1);
transition: all 0.3s;
}
.btn-three:hover::before {
opacity: 0 ;
transform: scale(0.5,0.5);
}
.btn-three::after {
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 1;
opacity: 0;
transition: all 0.3s;
border: 1px solid rgba(255,255,255,0.5);
transform: scale(1.2,1.2);
}
.btn-three:hover::after {
opacity: 1;
transform: scale(1,1);
}
每当我将按钮背景的不透明度更改为完全黑色时,文本在 :before 状态下完全消失。
有什么原因吗?似乎文本与按钮在同一个 'layer' 中,而不是在按钮上方
如果你想保持黑色不透明度并让文本一直到前面,那么你需要 "force" 使用 z-index
将文本放在前面.btn-three span {
z-index: 2;
display: block;
position: absolute;
width: 100%;
height: 100%;
}
.btn-three::before {
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 1;
background-color: rgba(0,0,0,1);
transition: all 0.3s;
}
如果不将跨度设置在 div 以上,您的文本将始终落后于它,因为您的 ::before
状态的 z-index 设置在文本上方。
这将始终使它消失,将 z-index 设置为 -1 会将它发送到后面,但它不会再使您的块变黑
.btn-three::before {
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: -1;
background-color: rgba(0,0,0,1);
transition: all 0.3s;
}
我建议将锚点 link 移到按钮标签之外,并使用 z-index 将文本强制放在前面。
CSS:
.btn-three {
transition: all 0.5s;
position: relative;
line-height: 25px;
height: 50px;
align-items: center;
text-align: center;
width: 250px;
cursor: pointer;
}
.btn-three::before {
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 1;
background-color: rgba(16, 84, 35, 1);
transition: all 0.3s;
}
.btn-three:hover::before {
opacity: 0 ;
transform: scale(0.5,0.5);
}
.btn-three::after {
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
opacity: 0;
transition: all 0.3s;
border: 1px solid rgba(0, 0, 0, 0.5);
transform: scale(1.2,1.2);
z-index: 2;
}
.three-text {
position: relative;
margin-top: -10px;
z-index: 1;
display: block;
}
.btn-three:hover::after {
opacity: 1;
transform: scale(1,1);
}
.button-wrapper {
padding-top: 3%;
text-align: center;
padding-bottom: 3%;
}
HTML:
<div class="button-wrapper">
<button class="btn btn-three" />
<a class="three-text">HERE YOUR TEXT</a>
</div>