2015-02-23 61 views
0

我在玩CSS和HTML,我不確定爲什麼輸出會在div標籤中添加文本時發生變化。爲什麼這個HTML輸出會改變?

第一:

div.bar { 
 
    display: inline-block; 
 
    width: 20px; 
 
    height: 75px; /* We'll override height later */ 
 
    background-color: teal; 
 
    margin-right: 2px; 
 
}
<div class="bar" style="height: 15px;"></div> 
 
<div class="bar" style="height: 20px;"></div> 
 
<div class="bar" style="height: 25px;"></div> 
 
<div class="bar" style="height: 30px;"></div>

輸出是四間酒吧。

二:

div.bar { 
 
    display: inline-block; 
 
    width: 20px; 
 
    height: 75px; /* We'll override height later */ 
 
    background-color: teal; 
 
    margin-right: 2px; 
 
}
<div class="bar" style="height: 15px;">15</div> 
 
<div class="bar" style="height: 20px;">20</div> 
 
<div class="bar" style="height: 25px;">25</div> 
 
<div class="bar" style="height: 30px;">30</div>

輸出是相同的,但倒掛。

我希望你能告訴我爲什麼。

+0

您可能想要檢出flexbox:http://css-tricks.com/snippets/css/a-guide-to-flexbox/ – mkaatman 2015-02-23 20:18:46

回答

2

這是因爲默認的垂直對齊baseline。使用vertical-align: bottom修復行爲:

div.bar { 
 
     display: inline-block; 
 
     width: 20px; 
 
     height: 75px; /* We'll override height later */ 
 
     background-color: teal; 
 
     margin-right: 2px; 
 
     vertical-align: bottom; 
 
    }
<div class="bar" style="height: 15px;">15</div> 
 
    <div class="bar" style="height: 20px;">20</div> 
 
    <div class="bar" style="height: 25px;">25</div> 
 
    <div class="bar" style="height: 30px;">30</div>

+0

另請參閱@JamesMontagne對「baseline」影響渲染效果的解釋的評論。 – 2015-02-23 20:25:00

2

嘗試使用vertical-align: bottom;

+0

這是正確的。對於更多細節,默認的垂直對齊是「基線」。這根據文本的基線對齊。當沒有文本時,它只是簡單地對齊框的底部。一旦文本被引入,對齊是基於文本而不是框。 – 2015-02-23 20:22:42

相關問題