我的设计有这个简单的导航菜单。但问题是我在悬停时无法显示子菜单。有什么方法可以解决这个问题吗?或者我的CSS有一些缺少的样式?
nav ul {list-style-type: none; overflow: hidden; background: #000; position: relative;}
nav li {float: left;}
nav li > a {padding: 15px 15px; display: inline-block; text-decoration: none; color: white; text-align: center;}
nav li > a:hover {background: violet;}
nav ul ul { position: absolute; background: green; top: 100%;}
nav ul ul li > a:hover {color: red;}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>sample UL</title>
<style media="screen">
</style>
</head>
<body>
<nav>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Menu 1</a></li>
<ul>
<li><a href="#">submenu 1</a></li>
<li><a href="#">submenu 2</a></li>
</ul>
</ul>
</nav>
</body>
</html>
答案 0 :(得分:2)
您的代码中存在相当多的错误。最关键的是子菜单容器上的top: 100%
,它将其移出可见区域。
除此之外,将子菜单标题li
定义为position: relative
和子菜单ul
本身默认为display: none
并悬停{{1> }}。并将子菜单容器放入作为其标题的display: block
。请参阅下面的代码:
li
&#13;
nav ul {
list-style-type: none;
background: #000;
}
nav li {
display: inline-block;
position: relative;
}
nav li>a {
padding: 15px 15px;
display: inline-block;
text-decoration: none;
color: white;
text-align: center;
}
nav li>a:hover {
background: violet;
}
nav ul li ul {
display: none;
position: absolute;
top: 45px;
left: 0px;
width: 100px;
overflow: visible;
padding: 0;
}
nav ul li:hover ul {
display: block;
}
nav ul ul li {
background: green;
display: block;
}
nav ul ul li>a:hover {
color: red;
}
&#13;
答案 1 :(得分:0)
nav ul {
list-style-type: none;
overflow: hidden;
background: #000;
position: relative;
}
nav li {
float: left;
}
nav li > a {
padding: 15px 15px;
display: inline-block;
text-decoration: none;
color: white;
text-align: center;
}
nav li > a:hover {
background: violet;
}
nav ul ul {
background: green;
top: 100%;
}
nav ul li a + ul {
display: none;
}
nav ul li a:hover + ul {
display: block;
}
.sub-menu:hover {
display: block;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>sample UL</title>
</head>
<body>
<nav>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Menu 1</a>
<ul class="sub-menu">
<li><a href="#">submenu 1</a></li>
<li><a href="#">submenu 2</a></li>
</ul>
</li>
</ul>
</nav>
</body>
</html>
答案 2 :(得分:0)
试试这个。你可以从中学习。
<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}
li {
float: left;
}
li a, .dropbtn {
display: inline-block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover, .dropdown:hover .dropbtn {
background-color: red;
}
li.dropdown {
display: inline-block;
}
.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;
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>
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#news">News</a></li>
<li class="dropdown">
<a href="javascript:void(0)" class="dropbtn">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>
&#13;
答案 3 :(得分:0)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>sample UL</title>
<style media="screen">
</style>
</head>
<body>
<nav class="cf">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Menu 1</a>
<ul class="submenu">
<li><a href="#">submenu 1</a></li>
<li><a href="#">submenu 2</a></li>
</ul></li>
</ul>
</nav>
</body>
</html>
请参阅Clearfix
还将类名称导航为 cf ,将内部ul作为子菜单
.cf:before, .cf:after {
content:"";
display:table;
}
.cf:after {
clear:both;
}
.cf {
zoom:1;
}
nav {
background: #333A31;
height: 2.3em;
}
ul, li {
margin: 0;
padding: 0;
list-style: none;
float: left;
}
ul {
background: #D5333C;
height: 2em;
width: 100%;
}
li {
position: relative;
}
li a {
display: block;
line-height: 2em;
padding: 0 1em;
color: white;
text-decoration: none;
}
li a:hover {
background: #9155311;
height: 2em;
padding-top: .3em;
position: relative;
top: -.3em;
border-radius: .3em .3em 0 0;
}
.current, a:hover.current {
background: #AD9B7F;
color: #eee;
padding-top: .3em;
border-radius: .3em .3em 0 0;
position: relative;
top: -.3em;
border-bottom: .3em solid #917F63;
cursor: default;
}
/*dropdown menu styles*/
ul.submenu {
float: none;
background: #916A31;
width: auto;
height: auto;
position: absolute;
top: 2em;
left: -9000em;
}
ul.submenu li {
float: none;
}
nav li:hover ul {
left: 0;
}
ul.submenu li a {
border-bottom: 1px solid white;
padding: .2em 1em;
white-space: nowrap;
}
ul.submenu li:last-child a {
border-bottom: none;
}
ul.submenu li a:hover {
background: #D5973C;
height: 2em;
padding-top: .2em;
top: 0;
border-radius: 0;
}
希望这有帮助。