2013-07-05 154 views
0

正如在difference between width auto and width 100 percent的答案中指定的寬度自動取全寬度,但在此demo寬度auto不取全寬度,但當我們對此應用100%只有它需要全寬。寬度auto如何實際取其元素?寬度自動和寬度的差值100%浮動元素

演示總結

HTML

<ul> 
    <li>one</li> 
    <li>two</li> 
    <li>three</li> 
</ul> 

CSS

ul{background-color: gray;} 
li{background-color: red; float: left; clear: left; width: auto;} 
+2

你已經回答了這個.... http://stackoverflow.com/questions/17468733/difference-between-width-auto-and-width-100-percent/17468803#17468803 –

+0

100%=父母的寬度: :; auto =取決於其他參數(顯示|浮動)可以作爲父母或儘可能少 – Svetoslav

+0

是啊!我已經回答了這個問題,但是在我們浮動元素時詢問寬度auto。 –

回答

0

試試這個

http://jsfiddle.net/p5bQP/1/

CSS

ul{ 
    background-color: gray; 
    float:left; 
    } 
li{ 
    background-color: red; 
    float: left; 
    clear: left; 
    width:100%; 
    } 
+0

我在問寬度自動與百分比。 –

0

問題是漂浮在li元素上。

如果你從你的CSS去除浮一部分這樣的:

ul{background-color: gray;} 
li{background-color: red; width: auto;} 

然後正常工作。

+0

我知道,但爲什麼浮動元素不會佔用它的全部寬度通過分配自動,但需要100%。 –

+0

浮動元素被處理爲內聯元素。 [http://webdesign.about.com/od/htmltags/qt/block_vs_inline_elements.htm] – mordilion

+0

如果這樣寬度100%也應該作爲寬度自動。 –