2012-11-27 99 views
0

我有以下代碼:如何填充對齊塊的背景?

<div style="background-color:Aqua"> 
    <div style="float:left">left</div> 
    <div style="float:right">right</div> 
</div> 

我想顯示...

left                  right 

...有一個水上背景,但塊不帶顏色填充。

我在做什麼錯?

回答

0

清除你的花車,例如與overflow: hidden

<div style="background-color:Aqua; overflow: hidden"> 
    <div style="float:left">left</div> 
    <div style="float:right">right</div> 
</div> 
+0

該代碼不工作..至少在IE – clarity

+0

這需要工作在IE8 – clarity

+0

http://jsfiddle.net/47yvK/ - 在IE7中工作 - 剛測試 –

0

浮點 - 定位規則依賴的東西,所以你永遠不會沒有的東西對齊對象。 所以,你應該使用以下順序:

<div style="background-color:Aqua"> 
    <div style="float:right;max-width:50%;">Right</div><!-- 100% by default --> 
    <div>Left</div> 
    <div style="clear:right;"></div><!-- closing the floating --> 
</div> 

否則:

<div style="background-color:Aqua"> 
    <div style="float:left;max-width:50%;">Left</div> 
    <div>Right</div> 
    <div style="clear:left;"></div> 
</div> 

或兩個:

<div style="background-color:Aqua"> 
    <div style="float:left;max-width:30%;">Left</div> 
    <div style="float:right;max-width:30%;">Right</div> 
    <div>Center</div> 
    <div style="clear:both;"></div> 
</div> 
+0

使用標記地址樣式是非語義的。 – crowjonah

+0

你也可以嘗試另一種解決方案:http://stackoverflow.com/questions/11805352/floatleft-vs-displayinline-vs-displayinline-block-vs-displaytable-cell – iegik