我一直在构建一个CSS下拉菜单,我有一个轻微的障碍,并想知道是否有人可以帮我解决这个问题。我有一个包含此HTML的菜单
<div id="menu">
<ul>
<li><a class="active" href="#">Home</a>
<ul class="sub-menu">
<li><a href="#">This is sub</a></li>
<li><a href="#">sub menu item</a></li>
<li><a href="#">This is a long sub menu item</a></li>
</ul>
</li>
<li><a href="#">Location</a></li>
<li><a href="#">another link</a></li>
</ul>
</div>
目前,当我将鼠标悬停在主<li>
'主页'上时,它的宽度会扩展很多,我认为可以容纳我拥有的子类菜单。我怎么能阻止这种情况发生?实现类似的目标:
|主页| //漂亮的短主要标签
|子菜单链接|
|另一个子菜单链接|
与目前正在发生的情况相反(原谅我图表的粗糙)
|主页.................... | //真的很长时间搞乱了所有标签
|子菜单链接|
|另一个子菜单链接|
这是菜单的CSS代码:
#menu ul {
margin: 0;
list-style: none;
padding: 0 0 0 20px;
position: relative;
z-index: 40;
}
#menu ul li {
float: left;
padding: 10px 10px 13px 0;
position: relative;
z-index: 50;
}
#menu li a {
text-decoration: none;
font-family: Verdana, Geneva, sans-serif;
答案 0 :(得分:2)
我有基本相同的标记,因此以下代码应该可以很好地满足您的需求。
我留下了所有的颜色和图片信息,以防你需要帮助。
#menu {
background: none repeat scroll 0 0 #333333;
border: 0 none;
font: bold 14px "Lucida Sans Unicode","Bitstream Vera Sans","Trebuchet Unicode MS","Lucida Grande",Verdana,Helvetica,sans-serif;
margin: 0;
padding: 0;
width: 100%;
}
#menu ul {
background: none repeat scroll 0 0 #333333;
height: 35px;
list-style: none outside none;
margin: 0 0 3px;
padding: 0;
width: 100%;
}
#menu li {
float: left;
padding: 0;
}
#menu li a {
background: no-repeat scroll right bottom #333333;
color: #CCCCCC;
display: block;
font-weight: normal;
line-height: 35px;
margin: 0;
padding: 0 10px;
text-align: center;
text-decoration: none;
}
#menu li a:hover, #menu ul li:hover a {
background: no-repeat scroll center bottom #2580A2;
color: #FFFFFF;
text-decoration: none;
}
#menu li ul {
background: none repeat scroll 0 0 #333333;
border: 0 none;
display: none;
height: auto;
margin: 0;
padding: 0;
position: absolute;
width: 225px;
z-index: 200;
}
#menu li:hover ul {
display: block;
}
#menu li li {
display: block;
float: none;
margin: 0;
padding: 0;
width: 225px;
}
#menu li:hover li a {
background: none repeat scroll 0 0 transparent;
}
#menu li ul a {
display: block;
font-size: 12px;
font-style: normal;
height: 35px;
margin: 0;
padding: 0 10px 0 15px;
text-align: left;
}
#menu li ul a:hover, #menu li ul li:hover a {
background: no-repeat scroll left center #2580A2;
border: 0 none;
color: #FFFFFF;
text-decoration: none;
}
答案 1 :(得分:0)
标记很奇怪。
首先,尝试构建它比使用&lt; a&gt;更好一点。对于相同的主链接&lt; li&gt;作为另一个&lt; ul&gt;。这个链接可以在单独的&lt; li&gt;?
中另外,请勿将菜单包装在&lt; div&gt;中。只需将样式直接应用于父&lt; ul&gt;。