[英]How to make single-line text center align while multi-line left align?
我使用下面的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>
<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>
我建议使用JQuery解决方案:
$(".my-text").keypress(function() {
if($(this).val().length >= 50) {
$(this).css("text-align", "left");
}
else {
$(this).css("text-align", "center");
}
});
可能有更好的选择,因为这个显然不防水,但它应该有所作为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.