我需要用它的子菜单来实现这个菜单:
我将主菜单设置为ul
/ li
,li
float: left; display: block;
其中有一个带有属性display: block;
和一些填充的元素。
每个li
都有position: relative
。每个li
里面都有一个.main_menu_submenu
div,带有position: absolute
. 在里面,它有另一个ul
/ li
where eachli
又有 properties display: block; float: left
。问题是宽度会.main_menu_submenu
根据容器li
(主菜单)的宽度进行调整,并且子菜单元素不能全部保持在同一行。
这是结果:
(来源:blueday.it)
我什至尝试使用display: inline-block
而不是浮动li
s,或者将 display: inline-block 放到.main_menu_submenu
块中。没有办法让它.main_menu_submenu
div
停留在一条宽度不取决于父 li 宽度的单行上。
如果我分配position: relative
而不是absolute
to .main_menu_submenu
,结果是父宽度适应子宽度:
(来源:blueday.it)
我认为唯一的解决方案是将子菜单构建为一行和 n 个单元格的表格而不是列表,但我真的不喜欢这个解决方案。
有没有其他方法可以达到结果?
小提琴:http: //jsfiddle.net/STfGL
.clearfix:after {
clear: both;
display: block;
content: " ";
height: 0px;
visibility: hidden;
}
.clearfix {
display: inline-block;
}
/* Hide these rules from IE-mac \*/
* html .clearfix {
height: 1%;
}
.clearfix {
display: block;
}
li.clearfix {
display: list-item;
}
#top_header_menu {
width: 100%;
background: transparent none;
height: 70px;
}
#main_menu_cage {
width: 100%;
background: none #E9E9E9;
}
#main_menu_cage,
#main_menu li {
height: 50px;
}
#main_menu_shadower_top {
background: transparent url('/images/shadow_bg_up.png') center top no-repeat;
height: 10px;
width: 100%;
}
#main_menu_shadower_bottom {
background: transparent url('/images/shadow_bg_down.png') center bottom no-repeat;
height: 10px;
width: 100%;
position: relative;
z-index: 11;
}
#main_menu {
font-size: 14px;
}
#main_menu li {
display: block;
float: left;
position: relative;
color: #255B9A;
}
#main_menu li:hover {
background: none #6092BB;
color: #FFFFFF;
text-decoration: none;
}
#main_menu li a {
font-weight: bold;
text-decoration: none;
color: inherit;
display: block;
height: 100%;
padding: 10px 6px 0;
box-sizing: border-box;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
-ms-box-sizing: border-box;
}
#main_menu li a:hover {
color: inherit;
text-decoration: inherit;
}
#main_menu li a span {
font-size: 12px;
font-weight: normal;
display: block;
}
#main_menu .main_menu_submenu ul {
padding-left: 0;
}
#main_menu .main_menu_submenu {
position: absolute;
z-index: 10;
padding: 10px 5px 5px;
background: none #A2C9E9;
border-radius: 0 0 3px 3px;
-moz-border-radius: 0 0 3px 3px;
-webkit-border-radius: 0 0 3px 3px;
white-space: nowrap;
}
#main_menu .main_menu_submenu li {
display: block;
float: left;
padding: 0 5px;
border-left: 1px solid #255B9A;
height: 20px;
font-size: 14px;
}
#main_menu .main_menu_submenu li:hover {
background: none transparent;
color: inherit;
text-decoration: inherit;
}
#main_menu .main_menu_submenu li:first-child {
border-left: none;
}
#main_menu .main_menu_submenu li a {
display: block;
padding: 2px 4px;
color: #255B9A;
text-decoration: none;
font-weight: normal;
}
#main_menu .main_menu_submenu li a:hover {
color: #FFFFFF;
background: #6092BB none;
text-decoration: none;
}
<div id="top_header_menu">
<div id="main_menu_shadower_top"></div>
<div id="main_menu_cage">
<div id="main_menu" class="contents_cager">
<ul class="clearfix">
<li>
<a href="/page/lazienda.html">
L'Azienda
<span>Scopri Blue Day srl</span>
</a>
<div class="main_menu_submenu">
<ul class="clearfix">
<li>
<a href="/">La storia</a>
</li>
<li>
<a href="/">Le persone</a>
</li>
</ul>
</div>
</li>
</ul>
</div>
</div>
<div id="main_menu_shadower_bottom"></div>
</div>
(我使用了两个div
s (#main_menu_shadower_bottom
以及#main_menu_shadower_top)
用于顶部和底部阴影,因为阴影是圆形的,底部的必须位于子菜单的顶部)。