简体   繁体   English

html5 或 css '智能文本流' 将句子或段落均匀分布在同样宽的行上?

[英]html5 or css 'smart text flow' to evenly distribute a sentence or paragraph over equally wide lines?

Suppose I have a dynamic HTML element (a span or div or whatever) with a sentence or paragraph of text inside, like this:假设我有一个动态 HTML 元素( spandiv或其他),里面有一个句子或一段文本,如下所示:

两条线

Now if I make the sentence slightly longer, it might need an extra line, like this:现在如果我把句子稍微长一点,它可能需要额外的一行,像这样:

三行错误

Note how the last word is on a separate line.请注意最后一个单词是如何在单独的行上的。 So the first two lines are full occupied.所以前两行已满。 In the sense that they take up the entire available with in this element.从某种意义上说,它们占据了这个元素中的全部可用空间。 And the third line contains only one word and remains mostly empty.第三行只包含一个单词,而且大部分都是空的。

I find this rather disturbing, and I wonder if there is a trick to distribute the free space more evenly amongst all lines.我觉得这很令人不安,我想知道是否有一个技巧可以在所有行之间更均匀地分配可用空间。 I mean like this: (note that this is the exact same sentence)我的意思是这样的:(请注意,这是完全相同的句子)

右三行

It should re-flow automatically depending on font and browser view size, and width of the element, which may depend on screen resolution.它应该根据字体和浏览器视图大小以及元素的宽度自动重新流动,这可能取决于屏幕分辨率。

Is there some clever HTML5 or CSS3 way to achieve this?是否有一些聪明的 HTML5 或 CSS3 方法来实现这一点?

Upon searching and looking in topics similar to this question, I did find the text-align: justify property which I am familiar with but isn't really what I'm looking for.在搜索和查看与此问题类似的主题后,我确实找到了我熟悉但并不是我真正想要的text-align: justify属性。
text-align: justify tends to extend all lines to the full with of the containing element by increasing the whitespace between words. text-align: justify倾向于通过增加单词之间的空格来将所有行扩展到包含元素的完整行。

Instead, what I'm really looking for is something that redistributes the words so that all lines are narrower ie not take up all horizontal element space.相反,我真正要寻找的是重新分配单词的东西,以便所有行都更窄,即不占用所有水平元素空间。 In a way that results in all lines being equally wide (or approximating this as close as possible).以某种方式导致所有线条都一样宽(或尽可能接近)。

So I mean NOT like this: (which is what text-align:justify does)所以我的意思不是这样:(这就是text-align:justify所做的)

文本对齐对齐

Compare this to the previous example above.将此与上面的示例进行比较。

I know you asked for a CSS solution but I don't think there is one yet.我知道您要求提供 CSS 解决方案,但我认为还没有。 Below is my JS solution for the meantime.以下是我的 JS 解决方案。

 function createBetterLineBreaks(el) { var originalWidth = el.offsetWidth; var originalHeight = el.offsetHeight; for (let i = originalWidth - 1; i >= 0; i--) { el.style.width = `${i}px`; if (el.offsetHeight > originalHeight) { el.style.width = `${i + 1}px`; break; } } } // hover effects document.querySelector('.box').addEventListener('mouseenter', function () { createBetterLineBreaks(document.querySelector('.box.text')); }); document.querySelector('.box').addEventListener('mouseleave', function () { document.querySelector('.box.text').style.width = 'unset'; });
 .box { width: 300px; font-size: 30px; border: 1px solid black; border-radius: 5px; padding: 20px; display: flex; align-items: center; justify-content: center; }
 <div class="box"><span class="text">hover this box to distribute the text more evenly</span></div>

You're looking for the CSS Text Module 4 property text-wrap: balance .您正在寻找 CSS文本模块 4属性text-wrap: balance It has been proposed but unfortunately not yet implemented, so in the meantime, use this jQuery polyfill balance-text .它已被提出但遗憾的是尚未实施,因此在此期间,请使用此 jQuery polyfill balance-text

 balanceText();
 .box { width: 300px; font-size: 30px; border: 1px solid black; border-radius: 5px; padding: 20px; display: flex; align-items: center; justify-content: center; }.balance-text { text-wrap: balance; }
 <script src="//cdn.jsdelivr.net/npm/balance-text@3.3.0/balancetext.min.js"></script> <div class="box"><span class="text balance-text">The words in this sentence cause it to require three lines.</span></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM