将所有嵌套的li设置为最宽的li的宽度

时间:2010-06-15 12:40:47

标签: html css

如何使嵌套的li具有相同的宽度?

当我使用下面的代码时,每个嵌套的li只有它的文本+边距宽。

我希望所有的li都和父母ul下最宽的li一样宽。

例如:

<ul id="menu">
    <li <a href="#" title="Menu a">Menu a</a></li>
    <li <a href="#" title="Menu b">Menu b</a></li>
    <li <a href="#" title="Nested Menu">Nested Menu</a>
        <ul>
            <li <a href="#" title="Menu Item">Menu Item</li>
            <li <a href="#" title="Long Menu Item">Long Menu Item</a></li>
            <li <a href="#" title="Longer Menu Item">Longer Menu Item</a></li>
        </ul>
    </li>
    <li <a href="#" title="Menu z">Menu z</a></li>
</ul>

用css:

<style type="text/css" media="all">
* {
    padding:0; 
    margin:0;
}
body, html {
    width: 100%;
    height: 100%;
}
#wrapper {
    width: 800px;
    height: 100%;
    margin: auto;
}
#menu {
    margin: 0 0 0 8px; 
    padding: 0;
    font-size: 14px; 
    font-weight: normal;
}

#menu ul {
    list-style-type:none; 
    list-style-position:outside; 
    position:relative; 
    z-index:300; 
    height: 32px;
    font-weight:bold; 
    white-space: nowrap;
    padding:0;
} 
#menu a {text-decoration:none; 
    line-height: 32px;
} 
#menu a:hover {

} 
#menu li {
    float:left; 
    position:relative; 
    display: inline; 
    height: 100%; 
    list-style-type: none; 
    padding: 0 20px;
    background: #ccc;
} 
#menu ul {
    position:absolute; 
    display:none; 
    left:0px;
    background: #BDCCD4;
    width:100%;
} 
#menu ul a, #menu li a {
    display: block;
}
#menu li ul {
    background: #BDCCD4;
    display:block;
}
#menu li ul a {
    font-weight: normal;
    height:auto; 
    float:left;
} 
#menu ul ul {
    padding: 0 9px;
    display:block;
} 
#menu li ul li {
    padding: 0 9px;
    background: #BDCCD4;
}
#menu li:hover {
    background: #ddd;
    height: 32px;
}
#menu li li:hover, #menu li li li:hover {
    background: #ddd;
    height: 32px;
}
#menu li a:link, #menu li a:visited {
    text-decoration: none;
    color: #003E7E;
    margin: auto;
}

6 个答案:

答案 0 :(得分:3)

只需为width: 100%添加#menu li ul li即可。要使其适用于更长的项目,请在width: auto上使用#menu li ul。编辑2:添加填充解决方法。

新的CSS:

<style type="text/css" media="all">
* {
    padding:0;
    margin:0;
}
body, html {
    width: 100%;
    height: 100%;
}
#wrapper {
    width: 800px;
    height: 100%;
    margin: auto;
}
#menu {
    margin: 0 0 0 8px;
    padding: 0;
    font-size: 14px;
    font-weight: normal;
}

#menu ul {
    list-style-type:none;
    list-style-position:outside;
    position:relative;
    z-index:300;
    height: 32px;
    font-weight:bold;
    white-space: nowrap;
    padding:0;
}
#menu a {text-decoration:none;
    line-height: 32px;
}
#menu a:hover {

}
#menu li {
    float:left;
    position:relative;
    display: inline;
    height: 100%;
    list-style-type: none;
    padding: 0 20px;
    background: #ccc;
}
#menu ul {
    position:absolute;
    display:none;
    left:0px;
    background: #BDCCD4;
    width:100%;
}
#menu ul a, #menu li a {
    display: block;
}
#menu li ul {
    background: #BDCCD4;
    display:block;
    width: auto;
}
#menu li ul a {
    font-weight: normal;
    height:auto;
    float:left;
}
#menu ul ul {
    padding: 0 0 0 9px;
    display:block;
}
#menu li ul li {
    padding: 0 9px;
    background: #BDCCD4;
    width: 100%;
}
#menu li:hover {
    background: #ddd;
    height: 32px;
}
#menu li li:hover, #menu li li li:hover {
    background: #ddd;
    height: 32px;
}
#menu li a:link, #menu li a:visited {
    text-decoration: none;
    color: #003E7E;
    margin: auto;
}

结果在这里:http://jsfiddle.net/y83zm/2/ 编辑2添加了修复以解决奇怪的填充问题,请参阅http://jsfiddle.net/y83zm/5/

答案 1 :(得分:2)

您需要使用JavaScript将所有LI设置为与最宽LI相同的宽度。如果你想使用jQuery库,这是代码:

$(document).ready(function(){
  $("#menu > li > ul").each(function() { // Loop through all the menu items that got submenu items
    var Widest=0; // We want to find the widest LI... start at zero
    var ThisWidth=0; // Initiate the temporary width variable (it will hold the width as an integer)

    $($(this).children()).each(function() { // Loop through all the children LIs in order to find the widest
      ThisWidth=parseInt($(this).css('width')); // Grab the width of the current LI

      if (ThisWidth>Widest) { // Is this LI the widest?
        Widest=ThisWidth; // We got a new widest value
      }
    });

    Widest+='px'; // Add the unit

    $(this).parent().css('width',Widest);
    $(this).children().css('width',Widest);
  });
});

CSS更改:

#menu li ul li {
  padding: 0 9px;
  background: #BDCCD4;
  padding: 0 20px;
}

JSFiddle处查看。

编辑:修正了我的误解。 :)

答案 2 :(得分:2)

会有一个非常简单的动态解决方案。对于你的CSS,当你在问题中发布它时,只需添加

#menu ul {
    display: inline-block;
    min-width: 100px;
}

#menu ul li, #menu ul li a{
    width: 100%;
    display:block;
}

然后它将完全按照你想要的那样。

您可以在此处查看它的外观:http://jsfiddle.net/ramsesoriginal/y83zm/61/

答案 3 :(得分:1)

要使所有列表项的长度与最长的相同,您需要手动设置宽度。据我所知,没有纯粹的CSS方法可以自动实现这一点。

li {width:100%}将使列表项填充其容器的宽度。如果没有设置,那么它将是用户浏览器窗口的宽度。

答案 4 :(得分:0)

一个简单的jQuery解决方案对我有用:

$(document).ready(function(){
    $('.sub-menu').each(function(){
        $(this).width(2000);
        var width = 0;
        $(this).children('li').each(function(){
            if ($(this).children('a').width() > width)
                width = $(this).children('a').width();
        });
        $(this).width(width);
    });
});

答案 5 :(得分:-1)

我使用以下CoffeeScript来实现所描述的行为:

$ ->
    menu_toggle = (e, show) ->
        ul = $(e).find('ul')
        ul.toggle()

        computed = ul.hasClass 'computed_width'
        if show && !computed
            ul.width 2000 # enormous with to be sure that li’s’ texts are not wrapped
            max_width = 0
            ul.find('li').each ->
                li = $(this)
                width = li.width()
                max_width = width if width > max_width
            ul.width max_width + 30 if max_width # 20 is 2 * padding + some reserve
            ul.toggleClass 'computed_width' # no need to compute every time

    $('li').has('ul').hover ->
        menu_toggle this, true
    , ->
        menu_toggle this, false