我想在我的导航栏中做一个下拉菜单,但它总是被样式中的背景图像挡住。位置设置为绝对,因为如果我将其设置为相对,导航栏的高度也会增加。我希望它看起来或多或少像 w3 关于 dropdows 的教程https://www.w3schools.com/css/tryit.asp?filename=trycss_navbar_horizo ntal_black_fixed
这是html代码
<!DOCTYPE html>
<html>
<head>
<link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@300;400&display=swap html_test2.css" rel="stylesheet">
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="description" content="Yearbook">
<meta name="keywords" content="yearbook">
<meta name="author" content="Gerald">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body{
margin: 0px;
background: linear-gradient(45deg, #5fd5f3, #ffddc6);
background-size: 500% 1000%;
animation: gradient 10s ease infinite;
}
@keyframes gradient {
0% {
background-position-x: 0%;
}
50% {
background-position-x: 100%;
}
100% {
background-position-x: 0%;
}
}
</style>
</head>
<body>
<div class="navbar">
<ul>
<li><a>Contacts</a>
<ul>
<li>Person 1</li>
<li>Person 2</li>
</ul>
</li>
<li><a href="#">Students</a></li>
<li><a href="#">About</a></li>
<li><a>Home</a>
<ul>
<li>test 1</li>
<li>test 2</li>
</ul>
</li>
</ul>
</div>
</body>
</html>
这是css代码
.navbar ul {
position: fixed;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #f9f9f9;
z-index: 4;
width: 100%;
list-style-type: none;
}
.navbar ul li {
transition: 0.2s;
float: right;
margin: 0.7%;
}
.navbar ul li a {
position: relative;
display: block;
text-align: center;
margin: 7px 25px;
color: #252525;
text-decoration: none;
font-size: 23px;
}
.navbar ul:hover li {
opacity: 0.2;
filter: blur(2px);
}
.navbar ul li:hover {
opacity: 1;
filter: blur(0px);
}
.navbar ul ul {
display: none;
}
.navbar ul li:hover > ul{
display: block;
position: absolute;
float: left;
}