2010-06-15 90 views
5

如何使嵌套li的寬度相同?設置所有嵌套li的寬度最寬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; 
} 

+0

創建的jsfiddle: http://jsfiddle.net/y83zm/ – MvanGeest 2010-06-15 12:43:51

回答

1

使所有的列表項相同長度的最長,您需要手動設置寬度。就我所知,沒有純粹的CSS方法可以實現這一點。

li {width:100%}將使列表項填充其容器的寬度。如果沒有設置,那麼它將是用戶瀏覽器窗口的寬度。

+2

你檢查過我的jsFiddle嗎?在Firefox 3.6和Internet Explorer 8中工作正常。在IE7中有問題,但與寬度無關。 – MvanGeest 2010-06-15 13:11:47

+0

該死 - 這讓動態內容變得很難! – nugget 2010-06-15 13:31:57

+0

不錯。我想可以添加一個條件樣式表來設置寬度爲<= IE 7 – theorise 2010-06-15 13:33:53

3

簡單地增加width: 100%#menu li ul li爲我工作。要使其適用於更長的物品,請使用#menu li ul上的width: auto。編輯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/ EDIT 2添加修復來解決一個奇怪的填充問題,看http://jsfiddle.net/y83zm/5/

+0

嘿,謝謝MvanGeest - 與我提供的測試代碼一起工作,但與我的真實代碼無關。 如果您添加另一個文本爲「What about a more longer title」的文本,背景會被切斷。 有什麼辦法可以避免這種情況? 乾杯 – nugget 2010-06-15 13:12:56

+0

你可以編輯我提供的jsFiddle,使其出錯,點擊「更新」併發布新的鏈接?另外,你的目標是什麼瀏覽器?我無法爲IE6提供任何保證。 – MvanGeest 2010-06-15 13:14:23

+0

沒關係,自己做了,並編輯瞭解決方案。這有幫助嗎?適用於Firefox。 (哦,如果解決方案的工作,你可以標記它是這樣的嗎?) – MvanGeest 2010-06-15 13:23:16

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

編輯:修正了我的誤解。 :)

+0

非常酷的被接受的答案 - 適用於「#menu li」,而不適用於「#menu li ul li」或僅僅是我? – nugget 2010-06-15 13:52:55

+0

您將獲得與最寬子菜單LI相同寬度的父LI。如果這不可取,那麼您應該使用純粹的CSS解決方案。 :) – 2010-06-15 14:48:36

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/

-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 
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); 
    }); 
});