我正在尝试用 html 和 css 为网站制作导航。这非常简单,我唯一想要的就是显示下拉菜单并在悬停时为它们着色。
现在我快到了,但是我还有两个问题:
- 元素重叠
- 当您将鼠标悬停在子菜单上时,会出现两个“悬停”方形背景 显示。
我有以下 CSS:
header {
background-color: rgb(147, 147, 147);
position: fixed;
top: 0;
margin: 0;
padding: 0;
left: 0;
height: 8%;
width: 100%;
border-bottom: 0.3em rgb(44, 171, 185) outset;
box-shadow: 0 0.18em 0.625em rgba(0, 0, 0, 0.50);
}
header nav ul {
list-style-type: none;
top: 0;
margin: 0;
}
.active {
background-color: rgba(0, 0, 0, 0.35);
}
header nav ul li.active:hover {
background-color: rgba(48, 67, 91, 0.64);
}
header nav ul li {
position: relative;
display: block;
}
header nav ul li a {
display: block;
line-height: 40px;
padding: 8px;
margin: 0;
top: 0;
float: left;
text-decoration: none;
color: #FFF;
font-family: Arial, Helvetica, sans-serif;
background-color: inherit;
}
header nav ul li:hover:not(.active) {
background-color: rgba(48, 67, 91, 0.35);
}
header nav ul li.dropdown ul {
display: none;
}
header nav ul li.dropdown:hover ul {
display: block;
position: absolute;
}
header nav ul li ul li {
display: block;
float: none;
padding: 30px;
}
我的 html 如下:
<body>
<header>
<nav>
<ul>
<li class="active"><a href="#">Element1</a></li>
<li><a href="element2/index.html">Element2</a></li>
<li class="dropdown"><a href="menu/index.html">Menu</a>
<ul>
<li><a href="#submenu1.html">Submenu1</a></li>
<li><a href="#submenu2.html">Submenu2</a></li>
</ul>
</ul>
</nav>
</header>
</body>
我想要的是更改悬停元素的背景并在下拉列表中正确显示子元素。
希望我说的够清楚了,谢谢
您可以在此处查看代码:https://jsfiddle.net/hdd260s4/
最佳答案
您好,我是根据您的要求改的。请使用此代码。如有任何疑问,请告诉我。
<!DOCTYPE html>
<html>
<head>
<style>
header {
background-color: rgb(147, 147, 147);
position: fixed;
top: 0;
margin: 0;
padding: 0;
left: 0;
height: 56px;
width: 100%;
border-bottom: 0.3em rgb(44, 171, 185) outset;
box-shadow: 0 0.18em 0.625em rgba(0, 0, 0, 0.50);
}
header nav ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
}
header nav li {
float: left;
}
header nav ul li a {
display: block;
line-height: 40px;
padding: 8px;
text-decoration: none;
color: #FFF;
font-family: Arial, Helvetica, sans-serif;
background-color: inherit;
}
header nav li a, .dropbtn {
display: inline-block;
color: white;
text-align: center;
text-decoration: none;
}
.active {
background-color: rgba(0, 0, 0, 0.35);
}
header nav ul li.active:hover {
background-color: rgba(48, 67, 91, 0.64);
}
header nav li a:hover, .dropdown:hover .dropbtn {
background-color: rgba(48, 67, 91, 0.35);
}
header nav li.dropdown {
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: rgba(48, 67, 91, 0.35);
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {background-color: #f1f1f1}
.dropdown:hover .dropdown-content {
display: block;
}
</style>
</head>
<body>
<header>
<nav>
<ul>
<li class="active"><a href="#home">Element1</a></li>
<li><a href="#news">Element2</a></li>
<li class="dropdown">
<a href="#" class="dropbtn">Menu</a>
<div class="dropdown-content">
<a href="#">Submenu1</a>
<a href="#">Submenu2</a>
<a href="#">Submenu3</a>
</div>
</li>
</ul>
</header>
</nav>
</body>
</html>
我在这里上传了我的代码,请看这个演示。 https://jsfiddle.net/hansraj/bmbuLcfk/
关于html - 下拉菜单显示不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40855783/