所以我使用的是最新版的 Bootstrap,我有以下 div
<div class="col-sm-6 col-md-2 col-lg-8 memory textContainer">
<p>{{ $memory->description }}</p>
</div>
句子的最大长度为 200 个字符,因此不能放在一行中。对于我的垂直对齐,我有以下 CSS。
.textContainer {
height: 200px;
line-height: 200px;
}
.textContainer p {
height: 200px;
}
这是有效的,但当句子长于一行时无效,因为两行之间的间距为 200px,如下图所示。
是否有解决此问题的方法?非常感谢!
最佳答案
您追求什么样的跨浏览器兼容性?已接受的答案在不支持 css3 转换的浏览器中无法正常工作。如果您需要支持旧版浏览器,请尝试以下操作:
<div class="col-sm-6 col-md-2 col-lg-8 memory textContainer">
<div class="centered">
<p>{{ $memory->description }}</p>
</div>
</div>
.textContainer {
height: 200px;
}
/* The ghost, nudged to maintain perfect centering */
.textContainer:before {
content: '';
display: inline-block;
height: 100%;
vertical-align: middle;
margin-right: -0.25em; /* Adjusts for spacing */
}
/* The element to be centered, can also be of any width and height */
.centered {
display: inline-block;
vertical-align: middle;
}
关于css - div Bootstrap 3中的垂直对齐语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29552032/