我是HTML的新手,尝试使用css在HTML中创建菜单。我创建了一个2级css下拉菜单,但问题是子菜单或第二级菜单的子菜单没有按预期显示。
这是HTML文件:
<nav>
<ul>
<li class="menuList"><a href="#" class="menuItem">Menu 1</a></li>
<li class="menuList">
<a href="#" class="menuItem">Menu 2</a>
<ul class="menuLevel1">
<li class="subMenu1">
<a href="#" class="itemLevel1">Sub menu 1</a>
</li>
<li class="subMenu1">
<a href="#" class="itemLevel1">Sub menu 2</a>
<ul class="menuLevel2">
<li class="subMenu2"><a href="#" class="itemLevel2">Sub Sub menu 1</a></li>
<li class="subMenu2"><a href="#" class="itemLevel2">Sub Sub menu 2</a></li>
<li class="subMenu2"><a href="#" class="itemLevel2">Sub Sub menu 3</a></li>
</ul>
</li>
<li class="subMenu1">
<a href="#" class="itemLevel1">Sub menu 3</a>
<ul class="menuLevel2">
<li class="subMenu2"><a href="#" class="itemLevel2">Sub Sub menu 1</a></li>
<li class="subMenu2"><a href="#" class="itemLevel2">Sub Sub menu 2</a></li>
<li class="subMenu2"><a href="#" class="itemLevel2">Sub Sub menu 3</a></li>
</ul>
</li>
</ul>
</li>
<li class="menuList"><a href="#" class="menuItem">Menu 3</a></li>
</ul>
</nav>
和css文件:
.menuBar {
background-color: #92CCDA;
}
ul{
float: left;
padding: 0;
margin: 0;
list-style: none;
}
.menuList{
float: left;
}
.menuItem{
display: block;
width: 100px;
height: 25px;
padding: 10px;
background-color: #92CCDA;
color: white;
text-decoration: none;
text-align: center;
}
a:hover {
background-color: orange;
}
.menuLevel1{
display: none;
}
.subMenu1 {
display: block;
}
.itemLevel1{
display: block;
width: 120px;
height: 25px;
background-color: #92CCDA;
text-align: center;
padding: 10px;
text-decoration: none;
}
li:hover ul.menuLevel1 {
display: block;
position: absolute;
}
.menuLevel2 {
display: none;
}
.itemLevel2{
display: block;
width: 120px;
height: 25px;
background-color: #92CCDA;
text-align: center;
padding: 10px;
text-decoration: none;
}
.menuLevel1 li:hover ul.menuLevel2{
display:block;
position: absolute;
left:100%;
}
答案 0 :(得分:1)
你快到了。只需执行以下操作:
position:relative;
添加到.subMenu1
,以使其充当其子女的绝对位置的参考。top: 0
上设置.menuLevel2
,使其与其父级的顶部对齐。那应该解决它。检查更新的小提琴:http://jsfiddle.net/hcKHy/1/