我需要通过子菜单实现此菜单:
http://newsletter.blueday.it/stackov_3.png
我将主菜单设置为ul / li,其中li float:left;显示:块;在它们里面有一个带有属性display的元素:block;和一些填充.
每个李都是位置:亲戚.
在每个li里面都有一个.main_menu_submenu div,其位置为:absolute.在里面,它有另一个ul / li,其中每个li都有属性显示:block;向左飘浮.
问题是.main_menu_submenu的宽度取决于容器li(主菜单)的宽度,并且子菜单元素不能全部保持在同一行.
这是结果:
http://newsletter.blueday.it/stackov_1.png
我甚至尝试使用display:inline-block而不是浮动lis,或者将display:inline-block放到.main_menu_submenu块中.没有办法使.main_menu_submenu div保持在一行上,其宽度不依赖于父li宽度.
如果我将position:relative而不是absolute分配给.main_menu_submenu,结果是父宽度适应子宽度:
http://newsletter.blueday.it/stackov_2.png
我认为唯一的解决方案是将子菜单构建为一行和n个单元格而不是列表,但我真的不喜欢这个解决方案.
有没有其他方法来实现结果?
小提琴:http://jsfiddle.net/STfGL/
码.
CSS:
.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; }
HTML:
<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(#main_menu_shadower_bottom和#main_menu_shadower_top)作为顶部和底部阴影,因为阴影是圆形的,底部阴影必须位于子菜单的顶部).
最佳答案 只需删除位置:relative; #main_menu li