【发布时间】:2012-11-06 07:30:03
【问题描述】:
在没有为height 或line-height 分配常量(因此,不是自动和可重用)值的情况下,我无法将背景图像与文本垂直对齐。我想知道是否真的有一种方法可以垂直居中对齐 bg 图像,比如a element, with its text without assigning constant values toline-heightorheight`?
此处提供现场演示:http://cssdesk.com/8Jsx2。
这是 HTML:
<a class="">background-position: center</a>
<a class="contain">background-size: contain</a>
<a class="line-height">Constant line-height</a>
这是 CSS:
a {
display: block;
margin: 10px;
width: 200px;
padding-left: 34px;
font-size: 14px;
background: url('http://cdn1.iconfinder.com/data/icons/freeapplication/png/24x24/Thumbs%20up.png');
background-repeat: no-repeat;
background-position: 5px center;
border: 1px solid black;
}
/* I don't want to use constant line-height */
.line-height {
line-height: 24px;
}
/* I don't want to use this, because I want my bg image's size to stay intact */
.contain {
background-size: contain;
}
【问题讨论】: