【发布时间】:2016-07-24 13:00:56
【问题描述】:
是否可以在较小的 div 中水平居中文本?字符长度会随着时间而改变。所以不能为每个项目做一个偏移量。
参见示例:
.container {
width: 100%;
height: 50px;
background: yellow;
}
a {
width: 100px;
height: 100px;
margin: 0 50px;
background: red;
white-space: nowrap;
float: left;
}
span {
position: absolute;
top: 0px;
text-align: center;
}
<div class="container">
<a href="" class="box-1">
<span>Lorem</span>
</a>
<a href="" class="box-2">
<span>Lorem ipsum dolor sit amet</span>
</a>
<a href="" class="box-3">
<span>Lorem ipsum dolor sit</span>
</a>
</div>
【问题讨论】:
-
只需在锚点上使用
text-align:center;并删除跨度的绝对定位。 -
看起来像 win 3.1 的热狗摊主题
-
见here 这可能是你需要的。