gpt4 book ai didi

javascript - 如何仅在遇到换行符时左对齐居中文本?

转载 作者:太空宇宙 更新时间:2023-11-04 14:31:30 25 4
gpt4 key购买 nike

如果我的文本到达其包含空间的末尾并且必须切换到下一行,我如何才能使我的文本左对齐? CSS 中是否有内在的东西可以检测到它,或者它必须是 JavaScript 解决方案?

这是 fiddle ,您可以看到它的行为方式:https://jsfiddle.net/fj4ddmey/2/

.text.container {
margin: 0 auto;
text-align: center;
width: 350px;
border: 1px solid black;
}
.text.container.two {
text-align: left;
}
<div class="text container">
<p>This is how short sentences should look</p>
</div>
<div class="text container">
<p>This text should be left aligned because it hits a line break</p>
</div>
<div class="text container two">
<p>This is how it should look, but it needs to be a fluid solution</p>
</div>

最佳答案

像那样使用flexbox

.container {
display: flex;
justify-content: center;
}
<div class="container">
<p>This is how it should look, but it needs to be a fluid solution</p>
</div>

这里是 JSFiddle demo

关于javascript - 如何仅在遇到换行符时左对齐居中文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35067498/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com