My::after悬停效果覆盖我的锚文本



我想做一个简单的悬停效果,但是当我悬停在div上时,::AFTER背景色覆盖了文本,但我想让文本放在一切之上,我试着调整锚的z-index,但那没有帮助。

*{
margin: 0;
padding: 0;
text-decoration: none;
list-style: none;
box-sizing: border-box;
}
body{
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
.box{
width: 300px;
height: 150px;
background-color: blueviolet;
cursor: pointer;
}
a{
position: relative;
display: flex;
justify-content: center;
align-items: center;
font-size: 20px;
height: 100%;width: 100%;
color: black;
}
a::after{
position: absolute;
content: "";
width: 100%;
height: 100%;
background-color: #000000ad;
left: 0;
top: 0;
z-index: -1;
}
a:hover{
color: white;
}
a:hover::after{
z-index: 1;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="styles.css">
<title>Hover</title>
</head>
<body>
<div class="box">
<a href="">Home</a>
</div>
</body>
</html>

你可以用像

这样的span分隔文本来解决这个问题
<a href=""><span style="z-index: 100;">Home</span></a>

我认为你只是想改变锚的背景颜色,而悬停锚所以我认为你不需要使用后伪类。你可以像我在代码中展示的那样,通过悬停来改变背景颜色。试试这个。如果我没有理解你的问题,请向我道歉。

*{
margin: 0;
padding: 0;
text-decoration: none;
list-style: none;
box-sizing: border-box;
}
body{
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
.box{
width: 300px;
height: 150px;
background-color: blueviolet;
cursor: pointer;
}
a{
position: relative;
display: flex;
justify-content: center;
align-items: center;
font-size: 20px;
height: 100%;width: 100%;
color: black;
}

a:hover{
color: white;
background-color: #000000ad;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="styles.css">
<title>Hover</title>
</head>
<body>
<div class="box">
<a href="">Home</a>
</div>
</body>
</html>

您可以将background-color添加为a:hover的一部分

a:hover {
color: white;
background-color: #000000ad;
}

工作片段:

* {
margin: 0;
padding: 0;
text-decoration: none;
list-style: none;
box-sizing: border-box;
}
body {
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
.box {
width: 300px;
height: 150px;
background-color: blueviolet;
cursor: pointer;
}
a {
position: relative;
display: flex;
justify-content: center;
align-items: center;
font-size: 20px;
height: 100%;
width: 100%;
color: black;
}
a::after {
position: absolute;
content: "";
width: 100%;
height: 100%;
left: 0;
top: 0;
z-index: -1;
}
a:hover {
color: white;
background-color: #000000ad;
}
a:hover::after {
z-index: 1;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="styles.css">
<title>Hover</title>
</head>
<body>
<div class="box">
<a href="">Home</a>
</div>
</body>
</html>

最新更新