我是用下面的css对齐文字中线和左线时的多行,但如何对齐文本中间和中间时一行呢?
.my-text {
border: 1px solid black;
width: 400px;
height: 160px;
vertical-align: middle;
display: table-cell;
overflow: hidden;
line-height: 20px;
padding: 20px;
}<div class="my-text">
carpe diem
</div>
<div class="my-text">
carpe diem carpe diem carpe diem carpe diem carpe diem carpe diem carpe diem carpe diem carpe diem carpe diem carpe diem carpe diem carpe diem
</div>
发布于 2017-04-26 07:29:27
<span>。inline-block,并将其文本对齐设置为left。
.my-text {
border: 1px solid black;
width: 400px;
height: 160px;
vertical-align: middle;
display: table-cell;
overflow: hidden;
line-height: 20px;
text-align: center;
padding: 10px;
}
.my-text span {
display: inline-block;
vertical-align: top;
text-align: left;
}<div class="my-text">
<span>carpe diem</span>
</div>
<div class="my-text">
<span>carpe diem carpe diem carpe diem carpe diem carpe diem carpe diem carpe diem carpe diem carpe diem carpe diem carpe diem carpe diem carpe diem</span>
</div>
发布于 2017-04-26 07:25:45
我建议使用JQuery解决方案:
$(".my-text").keypress(function() {
if($(this).val().length >= 50) {
$(this).css("text-align", "left");
}
else {
$(this).css("text-align", "center");
}
});可能有更好的选择,因为这显然不是防水的,但它应该有一定的作用。
https://stackoverflow.com/questions/43627349
复制相似问题