2015-04-24 26 views
0

http://i.stack.imgur.com/RtB6p.png關於塊寬度..這是怎麼回事這裏

見上面的圖片,這是我的CSS:

.activity { 
    width: 70%; 
    margin: 0 auto; 
} 

.activity .head { 
    margin-top: 3%; 
    text-align: center; 
} 

.activity .body { 
    margin-top: 3%; 
} 

爲什麼都直列塊不能在同一行?

<div class="body" style="background-color:red"> 
    <div style="background-color:green; display:inline-block; width:50%"> 
    </div> 
    <div style="background-color:blue; display:inline-block; width:49.6%"> 
    </div> 
</div> 

當我將第二塊寬度設置爲49.6%時,爲什麼它在下一行開始?

+2

請具體談談您的問題!你想從你的圖像中獲得標記嗎? –

+0

什麼問題?告訴我們,所以我們可以幫助您解決它! :) – Cullub

+0

我編輯帖子添加它 - 現在看他的評論 – Cullub

回答

1

內聯塊之間的空白字符佔用一些空間,因此增加了線框上的總寬度。

如果在HTML代碼中使內聯塊元素彼此相鄰,那麼它的工作方式與您的預期相同。

.body div { 
 
    height: 40px; 
 
    vertical-align: top; 
 
}
<div class="body" style="background-color:red"> 
 
<div style="background-color:green; display:inline-block; width:50%"></div><div style="background-color:blue; display:inline-block; width:50%"></div> 
 
</div>

+0

謝謝,我已經嘗試過「vertical-align:top;」但不工作。儘管如此,我的大多數元素都有自己的字體樣式,所以「font-size:0;」可能會很好。 –

+0

垂直對齊對於解決方案並不重要。關鍵在於兩個'div'元素在HTML代碼中彼此相鄰。 –