将文本垂直对齐h2标签的中心与背景

时间:2013-07-30 15:55:33

标签: html css alignment vertical-alignment

我有一个h2标签,背景图片为图标。我想在h2标签内垂直对齐文本,但它不起作用。我尝试过使用display: table-cell;,但是也没有用。文本也可以交叉到两行。

HTML:

<ul class="FeatureRow">
    <li><a href="#"><span><h2 class="SpeechBg">Need some help?</h2><p>Feel free to get in contact with us</p></span></a></li>
    <li><a href="#"><span><h2 class="PinBg">Great locations!</h2></span></a></li>
    <li><a href="#"><span><h2 class="ViewBg">View our apartments</h2></span></a></li>
</ul>

CSS:

a {
color: #2b6893;
text-decoration: none;
}
.FeatureRow{
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
}
.FeatureRow li{
float: left;
padding: 10px;
margin: 10px;
height: auto;
-webkit-box-shadow: 0px 4px 8px #f5f5f5;
-moz-box-shadow: 0px 4px 8px #f5f5f5;
box-shadow: 0px 4px 8px #f5f5f5;
background-color: #fdfdfd;
background-image: -moz-linear-gradient(top,#fbfbfb,#ffffff);
background-image: -webkit-gradient(linear,0 0,0 100%,from(#fbfbfb),to(#ffffff));
background-image: -webkit-linear-gradient(top,#fbfbfb,#ffffff);
background-image: -o-linear-gradient(top,#fbfbfb,#ffffff);
background-image: linear-gradient(to bottom,#fbfbfb,#ffffff);
background-repeat: repeat-x;
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#fffbfbfb',endColorstr='#ffffffff',GradientType=0);
filter: progid:DXImageTransform.Microsoft.gradient(enabled= false);
}
.FeatureRow span{
display: block;
width: 260px;
}
.FeatureRow h2{
background-repeat: no-repeat;
padding-left: 65px;
font-size: 22px;
height: auto;
min-height: 60px;
color: #3F92ED;
}
.SpeechBg{
background-image: url(http://joshblease.co.uk/Apartments/images/icons/speech.png);
}
.PinBg{
background-image: url(http://joshblease.co.uk/Apartments/images/icons/pin.png);
}
.ViewBg{
background-image: url(http://joshblease.co.uk/Apartments/images/icons/view.png);
}

JSFiddle示例:http://jsfiddle.net/YPnTp/

2 个答案:

答案 0 :(得分:2)

更改您的

  

.FeatureRow h2,添加 line-height

css如下

.FeatureRow h2{
background-repeat: no-repeat;
padding-left: 65px;
font-size: 22px;
height: auto;
min-height: 60px;
color: #3F92ED;
line-height:2.5em;
}

答案 1 :(得分:1)

.SpeechBg.PinBg.ViewBg中,添加line-height: ##px;,其中##是背景图片的高度(以px为单位)。