柔性解决方案
如果您想采用该display: table-cell
解决方案,那就太好了。但是,与其破解它,我们有一个更好的方法来完成同样的事情,使用display: flex;
. flex
是有不错支持的东西。
.wrap {
height: 200px;
width: 200px;
border: 1px solid #aaa;
margin: 10px;
display: flex;
}
.wrap span {
align-self: flex-end;
}
<div class="wrap">
<span>Align me to the bottom</span>
</div>
在上面的示例中,我们首先将父元素设置为display: flex;
,然后使用align-self
to flex-end
。这可以帮助您将项目推送到flex
父项的末尾。
旧解决方案(如果您不愿意使用,则有效flex
)
如果你想将文本对齐到底部,你不必为此写那么多属性,使用display: table-cell;
withvertical-align: bottom;
就足够了
div {
display: table-cell;
vertical-align: bottom;
border: 1px solid #f00;
height: 100px;
width: 100px;
}
<div>Hello</div>
(或JSFiddle)