我正在尝试使用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/
答案 0 :(得分:0)
请尝试此操作并根据您的需要进行更改。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Show Hide Dropdown Using CSS</title>
<style type="text/css">
ul{
padding: 0;
list-style: none;
background: #f2f2f2;
}
ul li{
display: inline-block;
position: relative;
line-height: 21px;
text-align: left;
}
ul li a{
display: block;
padding: 8px 25px;
color: #333;
text-decoration: none;
}
ul li a:hover{
color: #fff;
background: #939393;
}
ul li ul.dropdown{
min-width: 100%; /* Set width of the dropdown */
background: #f2f2f2;
display: none;
position: absolute;
z-index: 999;
left: 0;
}
ul li:hover ul.dropdown{
display: block; /* Display the dropdown */
}
ul li ul.dropdown li{
display: block;
}
</style>
</head>
<body>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li>
<a href="#">Products ▾</a>
<ul class="dropdown">
<li><a href="#">Laptops</a></li>
<li><a href="#">Monitors</a></li>
<li><a href="#">Printers</a></li>
</ul>
</li>
<li><a href="#">Contact</a></li>
</ul>
</body>
</html>
我在此处上传了我的代码,请参阅此演示。https://jsfiddle.net/hansraj/z2sojhbm/
答案 1 :(得分:0)
你好,我根据你的要求改变了。请使用此代码。我知道任何查询。
<!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/