html - 我的 ::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>

回答1

您可以通过使用跨度分隔文本来解决此问题

<a href=""><span style="z-index: 100;">Home</span></a>

回答2

我想你只是想在悬停到锚点时改变锚点的背景颜色所以我认为你不需要在伪类之后使用。正如我在代码中显示的那样,您只需悬停即可更改背景颜色。试试这个。如果我不明白你的问题,请向我道歉。

*{
    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>

回答3

您可以将 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>

相似文章

r - 在 R 中使用数据 masking 创建一个函数

为了进行配对分析,我需要编写一个对整数计数求和的函数。需要求和的总数在“df”中相应的“Yrs_Before”和“Yrs_After”列中指定。有没有一种方法可以避免只用一个函数编写“fm_after...

随机推荐

最新文章