<分区>
我有一个带有背景图像的 div,需要水平和垂直居中。在该图像之上,我还想显示 1 行文本,同样水平和垂直居中。
我设法让图像居中,但文本没有垂直居中。我认为 vertical-align:middle 可以解决问题。
这是我的代码:
<div style="background: url('background.png') no-repeat center; width:100%; height:100%; text-align:center;">
<div style="color:#ffffff; text-align: center; vertical-align:middle;" >
Some text here.
</div>
</div>
有什么想法吗?
解决方法:我实际上是通过使用表格来实现它的。 (我可能会被 HTML 社区诅咒到 hell 。)有什么重要的理由不使用这个 btw 吗?不过,我仍然对使用 div 的解决方案感兴趣。
<table width="100%" height="100%">
<tr>
<td align="center" style="background: url('background.png') no-repeat center; color:#ffffff;">Some text here.</td>
</tr>
</table>