2015-03-25 217 views
2

我想垂直居中對齊按鈕中的兩組文本。我該怎麼做呢?垂直居中對齊文本按鈕

我演示:http://jsfiddle.net/fc6317ne/

a.block { 
 
    color: #ffffff; 
 
    background: #F0F0F0; 
 
    font-size: 0.8rem; 
 
    height: 60px; 
 
    text-align: center; 
 
    text-decoration: none; 
 
    width: 30%; 
 
    float: left; 
 
    margin-right: 10px; 
 
    margin-bottom: 10px; 
 
    display: inline-block; 
 
    vertical-align: middle; 
 
}
<a href="" class="block active">Button</a> 
 

 
<a href="" class="block active">Button That Has More Words</a>

回答

3

您可以在錨使用display:table屬性,然後換一個跨度內的文本,並將其顯示爲table-cell,具有垂直取向中間跨度。

您不需要爲此調整行高或填充。 Fiddle

a.block { 
 
    color: #red; 
 
    background: #F0F0F0; 
 
    font-size: 0.8rem; 
 
    height: 60px; 
 
    text-align: center; 
 
    text-decoration: none; 
 
    width: 30%; 
 
    float: left; 
 
    margin-right: 10px; 
 
    margin-bottom: 10px; 
 
    display: table; 
 
} 
 
span { 
 
    display: table-cell; 
 
    vertical-align: middle; 
 
}
<a href="" class="block active"><span>Button</span></a> 
 

 
<a href="" class="block active"><span>Button That Has More Words</span></a>

+0

謝謝!!這看起來很完美:) – michaelmcgurk 2015-03-25 12:29:51

+1

@michaelmcgurk ..很高興 – 2015-03-25 12:30:21

0

嘗試這樣的:Demo

CSS:

a.block { 
    color: #000; 
    background: #F0F0F0; 
    font-size: 0.8rem; 
    height: 60px; 
    line-height:60px; 
    text-align: center; 
    text-decoration: none; 
    width: 30%; 
    float: left; 
    margin-right: 10px; 
    margin-bottom: 10px; 
    display: block; 
    vertical-align: middle; 
} 
span{ 
    line-height:22px !important; 
    display: inline-block; 
    vertical-align: middle; 
} 

HTML:

<a href="" class="block active"><span>Button</span></a> 

<a href="" class="block active"><span>Button That Has More Words</span></a>