我是 HTML 新手,并尝试使用 css 在 HTML 中创建菜单。我创建了一个 2 级 css 下拉菜单,但问题是子菜单或 2 级菜单的子菜单没有按应有的方式显示。
这是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%;
}