因此,我目前正在W3Schools上学习CSS,并且试图创建一个包含下拉菜单的导航栏。我正在尝试重新创建此(顺便说一句,效果很好):
<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}
li {
float: left;
}
li a {
display: inline-block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover, .dropdown:hover {
background-color: red;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content a {
color: black;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {background-color: #f1f1f1;}
.dropdown:hover .dropdown-content {
display: block;
}
</style>
</head>
<body>
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#news">News</a></li>
<li class="dropdown">
<a href="#">Dropdown</a>
<div class="dropdown-content">
<a href="#">Link 1</a>
<a href="#">Link 2</a>
<a href="#">Link 3</a>
</div>
</li>
</ul>
<h3>Dropdown Menu inside a Navigation Bar</h3>
<p>Hover over the "Dropdown" link to see the dropdown menu.</p>
</body>
</html>
这是我的代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Dropdown Navigation Bar</title>
<style>
div.nrm {
position: fixed;
top: 0;
left: 0;
}
body {
background-color: #BCBF69;
font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
color: white;
}
ul {
list-style-type: none;
padding: 0;
margin: 0;
overflow: hidden;
background-color: #212226;
width: 100%;
}
li {
float: left;
}
li a {
display: inline-block;
color: white;
text-align: center;
padding: 14px 15px;
text-decoration: none;
}
.home {
background-color: #D9A76A;
}
.home:hover {
background-color: rgb(104, 94, 81);
}
li a:hover, .dropdown:hover {
background-color: #494b50;
}
.dd-content {
display: none;
position: absolute;
background-color: #D9A76A;
min-width: 120px;
z-index: 1;
}
.dd-content a {
text-decoration: none;
text-align: left;
display: block;
}
.dd-content a:hover {
background-color: rgb(230, 204, 172);
}
.dropdown:hover .dd-content {
display: block;
}
</style>
</head>
<body>
<div class="nrm">
<ul>
<li><a href="#home" class="home">Home</a></li>
<li><a href="#explore">Explore</a></li>
<li class="dropdown">
<a href="#">Create</a></li>
<div class="dd-content">
<a href="#">Media</a>
<a href="#">Games</a>
<a href="#">Other</a>
</div>
</ul>
</div>
</body>
</html>
由于某种原因,当我将鼠标悬停在下拉菜单上时,它似乎没有任何作用。我真的可以帮忙。
答案 0 :(得分:0)
dd内容必须位于<li>
标记内,这就是为什么不起作用的原因。
<div class="nrm">
<ul>
<li><a href="#home" class="home">Home</a></li>
<li><a href="#explore">Explore</a></li>
<li class="dropdown">
<a href="#">Create</a>
<div class="dd-content">
<a href="#">Media</a>
<a href="#">Games</a>
<a href="#">Other</a>
</div>
</li>
</ul>
</div>
答案 1 :(得分:0)
第一件事是消除溢出:隐藏;从ul统治。您还需要将下拉列表放在li内。将父li设置为position:relative;
div.nrm {
position: fixed;
top: 0;
left: 0;
}
body {
background-color: #BCBF69;
font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
color: white;
}
ul {
list-style-type: none;
padding: 0;
margin: 0;
background-color: teal;
width: 100%;
display: flex;
}
li a {
display: inline-block;
color: white;
text-align: center;
padding: 14px 15px;
text-decoration: none;
}
.home {
background-color: #D9A76A;
}
.home:hover {
background-color: rgb(104, 94, 81);
}
li a:hover,
.dropdown:hover {
background-color: tomato;
}
.dropdown {
position: relative;
}
.dd-content {
display: none;
position: absolute;
right: 0;
top: 100%;
background-color: DarkOrchid;
width: 100%;
z-index: 1;
}
.dd-content a {
text-decoration: none;
text-align: left;
display: block;
}
.dd-content a:hover {
background-color: rgb(230, 204, 172);
}
.dropdown:hover .dd-content {
display: block;
}
<body>
<div class="nrm">
<ul>
<li><a href="#home" class="home">Home</a></li>
<li><a href="#explore">Explore</a></li>
<li class="dropdown">
<a href="#">Create</a>
<div class="dd-content">
<a href="#">Media</a>
<a href="#">Games</a>
<a href="#">Other</a>
</div>
</li>
</ul>
</div>
</body>
答案 2 :(得分:0)
Css读取您的样式:
.dropdown:hover .dd-content
嘿,当我将鼠标悬停在.dropdown:hover
上时,我会看着我身边的一个目标{strong>
不存在,因为根据上述结构,您的目标是同级
所以在这种情况下,您必须说些别的话
.dd-content
现在css的读起来就像当我将鼠标悬停在.dropdown:hover + .dd-content
上时,看看下一个兄弟姐妹称为.dropdown:hover