[英]Wrapping 2 lines at at time in CSS
I have thought about this and I'm not sure if it's possible with a CSS only solution, although I'm not even sure how I'd do it with JS.我已经考虑过这一点,但我不确定是否可以仅使用 CSS 解决方案,尽管我什至不确定如何使用 JS 来做到这一点。
Given text like the following:给定如下文本:
123456789012345678901234567890
This is a whole bunch of words
123456789012345678901234567890
Yes it is yes it is oh yes....
And having a screen so narrow that only the first 20 chars can fit.而且屏幕很窄,只能容纳前 20 个字符。 How can I wrap the text so that it displays like this:
如何包装文本以使其显示如下:
1234567890123456789
This is a whole bun
01234567890
ch of words
12345678901234567890
Yes it is yes it is
1234567890
oh yes....
That is, wrapping 2 lines at a time.也就是说,一次换行 2 行。 Typical wrapping rules will give me the wrong result:
典型的包装规则会给我错误的结果:
1234567890123456789
01234567890
This is a whole bun
ch of words
1234567890123456789
01234567890
Yes it is yes it is
oh yes....
Placing the texts above each other and using a big line-height can approximate this:将文本放在彼此之上并使用大的行高可以近似于:
.box { position: relative; font-size: 1.2em; line-height: 2.4em; /* twice the font-size*/ padding-top: 1.2em; /* equal to font-size*/ font-family: monospace; /* to illustrate */ overflow: hidden; border: 1px solid; resize: horizontal; word-break: break-all; /* omit this to keep full words */ }.box div { position: absolute; top: 0; left: 0; right: 0; bottom: 0; word-break: break-all; }
<div class="box"> <div>123456789012345678901234567890123456789012345678901234567890</div> This is a whole bunch of words Yes it is yes it is oh yes.... </div>
:root { --font-size: 16px; } div { position: relative; width: 22.5ex; border: 1px solid red; overflow: hidden; resize: horizontal; } div > pre { white-space: pre-wrap; word-break: break-all; vertical-align: baseline; margin: 0; font-size: var(--font-size); line-height: calc(var(--font-size) * 2 * 1.2); } div > pre:first-child { position: absolute; } div > pre:last-child { padding-top: calc(var(--font-size) * 1.2); }
<div> <pre>123456789012345678901234567890123456789012345678901234567890</pre> <pre>This is a whole bunch of words Yes it is yes it is oh yes...</pre> </div>
Unfortunately there's still some issues with this.不幸的是,这仍然存在一些问题。 There's no way to get the character width in CSS so, without javascript, you're left to guessing.
没有办法获得 CSS 中的字符宽度,因此,如果没有 javascript,您只能猜测。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.