2011-04-28 101 views
1

div sub如何將div center設置爲中間? 文字可以不同,所以在這種情況下邊距或線條高度不是一個好的解決方案。垂直對齊問題:中間;

CSS:

.center { 
    background-color: #336699; 
    width: 200px; 
    height: 200px; 
} 

.sub { 
    display: inline-block; 
    vertical-align: middle; 
} 

HTML:

<div class="center"> 
    <span class="sub"> 
     Some text text... 
    </span> 
</div> 

回答

3

一種可能的解決方案是:

HTML

<div class="center"> 
    <span class="sub"> 
     Some text text...<br /> 
     An some more text... 
    </span> 
</div> 

CSS

.center 
{ 
    background-color: #336699; 
    width: 200px; 
    height: 200px; 
    display: table; 
} 

.sub 
{ 
    display: table-cell ; 
    vertical-align: middle; 
    text-align: center; 
} 

現場演示,如果需要的話,是在http://jsfiddle.net/vladsaling/6nTGF/