我試圖制作一個簡單的懸停效果,但是當我懸停在 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>
uj5u.com熱心網友回復:
您可以通過使用跨度分隔文本來解決此問題
<a href=""><span style="z-index: 100;">Home</span></a>
uj5u.com熱心網友回復:
我想你只是想在懸停到錨點時改變錨點的背景顏色所以我認為你不需要在偽類之后使用。正如我在代碼中顯示的那樣,您只需懸停即可更改背景顏色。試試這個。如果我不明白你的問題,請向我道歉。
*{
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>
uj5u.com熱心網友回復:
您可以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>
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/474259.html
上一篇:在if陳述句中添加HTML
