2013-04-12 56 views
0
<ul style="background: blue; list-style: none; width: 40px; height: 40px; color: white"> 
    <li style="float: left; width: 20px; background: red">a</li> 
    <li style="float: left; width: 20px; background: green; height: 40px">b</li> 
</ul> 

這一點的HTML/CSS似乎產生了一個80px寬 - 而不是40px寬。我不知道爲什麼。有任何想法嗎?這裏的小提琴:爲什麼左側有空格?

http://jsfiddle.net/qJvmM/

謝謝!

+0

你肯定是沒有margin和padding? – 2013-04-12 06:32:58

回答

3

因爲ul默認情況下通常具有填充。

你需要將它重置爲零:

ul { 
    padding: 0; 
} 
0

這是因爲UL的默認margin和padding的

<ul style="background: blue; list-style: none; width: 40px; height: 40px; color: white;margin:0; padding:0"> 
    <li style="float: left; width: 20px; background: red">a</li> 
    <li style="float: left; width: 20px; background: green; height: 40px">b</li> 
</ul> 

DEMO

相關問題