我已经通过W3Schools尝试了解下拉菜单的编码结构。打开页面并将光标悬停在“Merch”文本时,应显示下拉菜单。然而,出于某种原因,它没有显示出来。请放大并告诉我出错的地方。
<!DOCTYPE html>
<html>
<head>
<style>
ul {
margin: 0;
overflow: hidden;
background-color: dimgray;
list-style-type: none;
}
li {
float: left;
}
li a {
text-decoration: none;
font-family: sans-serif;
display: inline-block;
color: white;
padding: 12px;
}
li a:hover {
background-color: gray;
}
#dropdown {
position: absolute;
display: none;
background-color: darkgray;
min-width: 140px;
}
#dropdown a {
color: white;
text-align: left;
padding: 10px;
display: block;
text-align: left;
}
#dropdown a:hover {
background-color: gray;
}
#dropbtn:hover #dropdown {
display: block;
}
</style>
</head>
<body>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#" id="dropbtn">Merch</a>
<div id="dropdown">
<a href="#">Shirts</a>
<a href="#">Pants</a>
</div>
</li>
<li><a href="#">About Us</a></li>
</ul>
</body>
</html>
答案 0 :(得分:0)
您需要在CSS中进行此更改
#dropbtn:hover + #dropdown {
display: block;
}
Dropdown不是孩子,它是您当前html设置中的下一个元素,因此,selector会找到它。
或者,更好,将ID放在 li元素(父级)上:
<ul>
<li><a href="#">Home</a></li>
<li id="dropbtn"><a href="#" >Merch</a>
<div id="dropdown">
<a href="#">Shirts</a>
<a href="#">Pants</a>
</div>
</li>
<li><a href="#">About Us</a></li>
</ul>
#dropbtn:hover #dropdown {
display: block;
}
演示:https://jsfiddle.net/3bfgzf37/
如果您使用第一个解决方案,下拉消息会快速消失,并且表现得很奇怪...... 说明:将鼠标悬停在 a 上不悬停在下拉列表中(a是兄弟),将鼠标悬停在li元素上,同时将鼠标悬停在下拉列表中(父子,下拉是“内部的” - 产生一致的,期望的行为。)
第二个更好。
ul {
margin: 0;
overflow: hidden;
background-color: dimgray;
list-style-type: none;
}
li {
float: left;
}
li a {
text-decoration: none;
font-family: sans-serif;
display: inline-block;
color: white;
padding: 12px;
}
li a:hover {
background-color: gray;
}
#dropdown{
position: absolute;
display:none;
background-color: darkgray;
min-width: 140px;
}
#dropdown a {
color: white;
text-align: left;
padding: 10px;
display: block;
text-align: left;
}
#dropdown a:hover {
background-color: gray;
}
#dropbtn:hover #dropdown {
display: block;
}
&#13;
<ul>
<li><a href="#">Home</a></li>
<li id="dropbtn"><a href="#" >Merch</a>
<div id="dropdown">
<a href="#">Shirts</a>
<a href="#">Pants</a>
</div>
</li>
<li><a href="#">About Us</a></li>
</ul>
<h3>Dropdown Menu inside a Navigation Bar</h3>
<p>Hover over the "Dropdown" link to see the dropdown menu.</p>
&#13;
答案 1 :(得分:0)
您的样式标记应位于head标记之外。此外,此代码中的下拉列表现在可用。只需根据您的需求对颜色进行一些微小的改动。 :)
<html>
<head></head>
<style>
ul {
margin: 0;
overflow: hidden;
background-color: dimgray;
list-style-type: none;
}
li {
float: left;
}
li a {
text-decoration: none;
font-family: sans-serif;
display: inline-block;
color: white;
padding: 12px;
}
/* Dropdown Button */
.dropbtn {
background-color: #4CAF50;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}
/* The container <div> - needed to position the dropdown content */
.dropdown {
}
/* Dropdown Content (Hidden by Default) */
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}
/* Links inside the dropdown */
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
/* Change color of dropdown links on hover */
.dropdown-content a:hover {background-color: #f1f1f1}
/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
display: block;
}
/* Change the background color of the dropdown button when the dropdown content is shown */
.dropdown:hover .dropbtn {
background-color: gray;
}
</style>
<body>
<ul>
<li><a href="#">Home</a></li>
<li>
<div class="dropdown">
<button class="dropbtn">Merch</button>
<div class="dropdown-content">
<a href="#">Shirts</a>
<a href="#">Pants</a>
</div>
</div>
</li>
<li><a href="#">About Us</a></li>
</ul>
</body>
</html>