【发布时间】:2014-09-10 21:44:55
【问题描述】:
我正在使用 Twitter Bootstrap 构建一个模板,并尝试使文本在图像上方水平和垂直居中,例如:
我正在尝试这个:
<div class="container"> <!--Bootstrap-->
<div class="row"> <!--Bootstrap-->
<div class="col-md-4"> <!--Bootstrap-->
<div class="box">
<div class="thumb"><img src="..."></div>
<div class="title">Post title</div>
</div>
</div> <!--Bootstrap-->
</div> <!--Bootstrap-->
</div> <!--Bootstrap-->
.box {height:350px; width:100%; border:4px solid #fff;}
.thumb {position:relative; width:100%; height:100%; overflow: hidden;}
.thumb img{width:100%; height:100%; overflow: hidden;}
.title {position:absolute;}
有了这个,如果我使用一些值 top 到 div title 它会出现在 img 上方,但我无法将其水平或垂直居中。
到目前为止,我搜索的所有内容都不起作用:left:50%, left:0 和 right:0; margin:auto 等等...
我也不打算将 img 用作 css 背景。
我怎样才能做到这一点?
【问题讨论】:
-
你能在这个项目中使用
display:table-cell吗? -
我尝试使用
display:table到 thumb div 和display:table-cell加上vertical-align:middle到 title div,但是没有用
标签: html css twitter-bootstrap center absolute