我想像这样显示一个简短的文本居中句子。
<p style="font-size: 13px; text-align: center; width: 600px;">
This is a variable-length sentence which is sometimes only a few words and sometimes up to three lines long.
</p>
这是一个可变长度的句子,有时只有几个单词,有时长达三行。
现在发生的情况是,由于宽度的原因,最后两个词“lines long”落在了段落的下一行。在居中对齐中,这看起来相当丑陋。我目前解决此问题的方法是在正确的位置手动插入一个中断。
<p style="font-size: 13px; text-align: center; width: 600px;">
This is a variable-length sentence which is sometimes<br/>only a few words and sometimes up to three lines long.
</p>
这是一个变长的句子,有时是
只有几个字,有时长达三行。
一直做下去会很累。 HTML 中有没有一种方法可以自动执行此操作?所以基本上首先让一个“p”元素确定它需要显示文本的最少行数,然后尽可能地水平缩小它,同时保持相同的行数?
最佳答案
与 CSS 最接近的是 text-align: justify;
p {
font-size: 13px;
text-align: justify;
}
p#first {
width: 100px;
}
p#second {
width: 150px;
}
<p id="first">
This is a variable-length sentence which is sometimes only a few words and sometimes up to three lines long.
</p>
<p id="second">
This is a variable-length sentence which is sometimes only a few words and sometimes up to three lines long.
</p>
关于html - 在 HTML 中,您可以缩小段落以使句子具有相同的宽度吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31235363/