这是另一种不使用display: inline-block和 IE7 hack 的方法:
<div class="parent">
<img src="http://img585.imageshack.us/img585/3989/m744.png"
width="100%" height="68" />
<h1>Variable length text</h1>
</div>
使用 CSS:
.parent {
display: inline;
position: relative;
}
.parent img {
position: absolute;
top: 0;
left: 0;
}
.parent h1 {
display: inline;
position: relative; /* this makes sure the h1 is in
front of img in stacking order */
border: 1px dotted lightgray;
padding: 0 10px; /* optional, tweak left/right as needed */
vertical-align: top;
line-height: 68px;
}
演示小提琴:http: //jsfiddle.net/audetwebdesign/BfMDJ/
这与Vector早期的解决方案相同,只是图像高度的处理方式不同。
我选择将行高设置为h1匹配背景图像的行高(本例中为 68px),然后应用vertical-align: top. 如果您愿意,这可以更轻松地垂直对齐文本。
潜在问题
如果将图像宽度设置为 100% 并设置高度,则图像会失真。如果您想要一个更可靠的答案,您需要指定有关背景图像的内容以及您希望文本如何垂直居中。