css 前后的伪元素问题
Problem with pseudo elements after and before in css
目前,我正在尝试制作一个环绕 link 的动画边框,但是当我将鼠标悬停在鼠标上时,动画仅适用于伪元素的一个实例,而不适用于两者。写在其他作品之上的实例。
我也不知道是哪里出了问题,也找不到问题所在。两个伪元素都设置了内容,我真的不知道哪里出了问题。
这是代码
nav a {
text-decoration: none;
color: white;
width: 100px;
height: 50px;
padding: 25px;
position: relative;
}
nav a::before {
content: "";
position: absolute;
top: -2px;
left: -2px;
width: 0;
height: 0;
padding: 0;
background: transparent;
border: 2px transparent solid;
}
nav a:hover::before {
animation: animate 0.5s linear forwards;
}
@keyframes animate
{
0% {
width: 0;
height: 0;
border-top-color: white;
border-right-color: transparent;
border-bottom-color: transparent;
border-left-color: transparent;
}
50% {
width: 100%;
height: 0;
border-top-color: white;
border-right-color: white;
border-bottom-color: transparent;
border-left-color: transparent;
}
100% {
width: 100%;
height: 100%;
border-top-color: white;
border-right-color: white;
border-bottom-color: transparent;
border-left-color: transparent;
}
nav a::after {
content: "";
position: absolute;
top: -2px;
left: -2px;
width: 0;
height: 0;
padding: 0;
background: transparent;
border: 2px transparent solid;
}
nav a:hover::after {
animation: animate2 0.5s linear forwards;
}
@keyframes animate2
{
0% {
width: 0;
height: 0;
border-top-color: transparent;
border-right-color: transparent;
border-bottom-color: transparent;
border-left-color: white;
}
50% {
width: 0%;
height: 100%;
border-top-color: transparent;
border-right-color: transparent;
border-bottom-color: white;
border-left-color: white;
}
100% {
width: 100%;
height: 100%;
border-top-color: transparent;
border-right-color: transparent;
border-bottom-color: white;
border-left-color: white;
}
对于 animate
和 animate2
,您都缺少 100%
代码块后的右括号。
放置它们使 css 对我有用。
例如:
html {
background: #333;
}
nav a {
text-decoration: none;
color: white;
width: 100px;
height: 50px;
padding: 25px;
position: relative;
}
nav a::before {
content: "";
position: absolute;
top: -2px;
left: -2px;
width: 0;
height: 0;
padding: 0;
background: transparent;
border: 2px transparent solid;
}
nav a:hover::before {
animation: animate 0.5s linear forwards;
}
@keyframes animate {
0% {
width: 0;
height: 0;
border-top-color: white;
border-right-color: transparent;
border-bottom-color: transparent;
border-left-color: transparent;
}
50% {
width: 100%;
height: 0;
border-top-color: white;
border-right-color: white;
border-bottom-color: transparent;
border-left-color: transparent;
}
100% {
width: 100%;
height: 100%;
border-top-color: white;
border-right-color: white;
border-bottom-color: transparent;
border-left-color: transparent;
}
}
nav a::after {
content: "";
position: absolute;
top: -2px;
left: -2px;
width: 0;
height: 0;
padding: 0;
background: transparent;
border: 2px transparent solid;
}
nav a:hover::after {
animation: animate2 0.5s linear forwards;
}
@keyframes animate2 {
0% {
width: 0;
height: 0;
border-top-color: transparent;
border-right-color: transparent;
border-bottom-color: transparent;
border-left-color: white;
}
50% {
width: 0%;
height: 100%;
border-top-color: transparent;
border-right-color: transparent;
border-bottom-color: white;
border-left-color: white;
}
100% {
width: 100%;
height: 100%;
border-top-color: transparent;
border-right-color: transparent;
border-bottom-color: white;
border-left-color: white;
}
}
<nav>
<a>TEST</a>
</nav>
目前,我正在尝试制作一个环绕 link 的动画边框,但是当我将鼠标悬停在鼠标上时,动画仅适用于伪元素的一个实例,而不适用于两者。写在其他作品之上的实例。
我也不知道是哪里出了问题,也找不到问题所在。两个伪元素都设置了内容,我真的不知道哪里出了问题。
这是代码
nav a {
text-decoration: none;
color: white;
width: 100px;
height: 50px;
padding: 25px;
position: relative;
}
nav a::before {
content: "";
position: absolute;
top: -2px;
left: -2px;
width: 0;
height: 0;
padding: 0;
background: transparent;
border: 2px transparent solid;
}
nav a:hover::before {
animation: animate 0.5s linear forwards;
}
@keyframes animate
{
0% {
width: 0;
height: 0;
border-top-color: white;
border-right-color: transparent;
border-bottom-color: transparent;
border-left-color: transparent;
}
50% {
width: 100%;
height: 0;
border-top-color: white;
border-right-color: white;
border-bottom-color: transparent;
border-left-color: transparent;
}
100% {
width: 100%;
height: 100%;
border-top-color: white;
border-right-color: white;
border-bottom-color: transparent;
border-left-color: transparent;
}
nav a::after {
content: "";
position: absolute;
top: -2px;
left: -2px;
width: 0;
height: 0;
padding: 0;
background: transparent;
border: 2px transparent solid;
}
nav a:hover::after {
animation: animate2 0.5s linear forwards;
}
@keyframes animate2
{
0% {
width: 0;
height: 0;
border-top-color: transparent;
border-right-color: transparent;
border-bottom-color: transparent;
border-left-color: white;
}
50% {
width: 0%;
height: 100%;
border-top-color: transparent;
border-right-color: transparent;
border-bottom-color: white;
border-left-color: white;
}
100% {
width: 100%;
height: 100%;
border-top-color: transparent;
border-right-color: transparent;
border-bottom-color: white;
border-left-color: white;
}
对于 animate
和 animate2
,您都缺少 100%
代码块后的右括号。
放置它们使 css 对我有用。
例如:
html {
background: #333;
}
nav a {
text-decoration: none;
color: white;
width: 100px;
height: 50px;
padding: 25px;
position: relative;
}
nav a::before {
content: "";
position: absolute;
top: -2px;
left: -2px;
width: 0;
height: 0;
padding: 0;
background: transparent;
border: 2px transparent solid;
}
nav a:hover::before {
animation: animate 0.5s linear forwards;
}
@keyframes animate {
0% {
width: 0;
height: 0;
border-top-color: white;
border-right-color: transparent;
border-bottom-color: transparent;
border-left-color: transparent;
}
50% {
width: 100%;
height: 0;
border-top-color: white;
border-right-color: white;
border-bottom-color: transparent;
border-left-color: transparent;
}
100% {
width: 100%;
height: 100%;
border-top-color: white;
border-right-color: white;
border-bottom-color: transparent;
border-left-color: transparent;
}
}
nav a::after {
content: "";
position: absolute;
top: -2px;
left: -2px;
width: 0;
height: 0;
padding: 0;
background: transparent;
border: 2px transparent solid;
}
nav a:hover::after {
animation: animate2 0.5s linear forwards;
}
@keyframes animate2 {
0% {
width: 0;
height: 0;
border-top-color: transparent;
border-right-color: transparent;
border-bottom-color: transparent;
border-left-color: white;
}
50% {
width: 0%;
height: 100%;
border-top-color: transparent;
border-right-color: transparent;
border-bottom-color: white;
border-left-color: white;
}
100% {
width: 100%;
height: 100%;
border-top-color: transparent;
border-right-color: transparent;
border-bottom-color: white;
border-left-color: white;
}
}
<nav>
<a>TEST</a>
</nav>