将`
`标签包装成固定宽度,使每行字符长度相同。

huangapple go评论83阅读模式
英文:

Wrapping <pre> to a fixed width such that each line is the same length in characters

问题

我试图将这个长的<pre>包装起来,使其形成一个矩形(而不是出现我得到的“锯齿状”结束)。我已经将white-space设置为pre-wrapword-break设置为break-all,就像其他StackOverflow帖子建议的那样,但似乎浏览器仍然会在哪里断行应用一些启发式规则。

我不能事先将<pre>分成行,因为内容的长度和<pre>的宽度都可能是动态的。

pre {
  width: 300px;
  white-space: pre-wrap;
  word-break: break-all;
}
<pre>ER..............................3......|..f1.f1.fSfQ.W....R..|.........K...R.A..U1.0....r...U.u....t.f.....B....1.ZQ....[...@P..?Q..SRP..|...f....D.....f@.....f.&gt;@|..xpu....{.D|.....isolinux.bin missing or corrupt...f`f1.f...{f...{fRfP.Sj.j...f.6.{.........6.{....A......{...d.fa....Operating system load error...^....&gt;b.....&lt;.u........................</pre>

我的现状:

将`<pre>`标签包装成固定宽度,使每行字符长度相同。

我想要实现的:

将`<pre>`标签包装成固定宽度,使每行字符长度相同。

英文:

I'm attempting to wrap this long &lt;pre&gt; so that it forms a rectangle (rather than having the "jagged" endings I'm getting). I've set white-space to pre-wrap and word-break to break-all like other StackOverflow posts have suggested, but it seems like the browser is still applying some heuristics on where to break the line.

I cannot break the &lt;pre&gt; into rows a-priori because both the length of the content, as well as the width of the &lt;pre&gt; may be dynamic.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

pre {
width: 300px;
white-space: pre-wrap;
word-break: break-all;
}

<!-- language: lang-html -->

&lt;pre&gt;ER..............................3......|..f1.f1.fSfQ.W....R..|.........K...R.A..U1.0....r...U.u....t.f.....B....1.ZQ....[...@P..?Q..SRP..|...f....D.....f@.....f.&amp;gt;@|..xpu....{.D|.....isolinux.bin missing or corrupt...f`f1.f...{f...{fRfP.Sj.j...f.6.{.........6.{....A......{...d.fa....Operating system load error...^....&amp;gt;b.....&amp;lt;.u........................&lt;/pre&gt;

<!-- end snippet -->

What I have:

将`<pre>`标签包装成固定宽度,使每行字符长度相同。

What I want to achieve:

将`<pre>`标签包装成固定宽度,使每行字符长度相同。

答案1

得分: 1

line-break: anywhere; 在Chrome(Canary)和Firefox中运行良好,但在Edge中不起作用。如果内容包含空格,您可能应该使用white-space:break-spaces而不是pre-wrap

pre {
  width: 300px;
  white-space: break-spaces;
  line-break: anywhere;
}
<pre>ER..............................3......|..f1.f1.fSfQ.W....R..|.........K...R.A..U1.0....r...U.u....t.f.....B....1.ZQ....[...@P..?Q..SRP..|...f....D.....f@.....f.&gt;@|..xpu....{.D|.....isolinux.bin missing or corrupt...f`f1.f...{f...{fRfP.Sj.j...f.6.{.........6.{....A......{...d.fa....Operating system load error...^....&gt;b.....&lt;.u........................</pre>
英文:

line-break: anywhere; works well in Chrome (Canary) and Firefox, but not Edge. If the content contains spaces, you should probably use white-space:break-spaces rather than pre-wrap as well.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

pre {
width: 300px;
white-space: break-spaces;
line-break: anywhere;
}

<!-- language: lang-html -->

&lt;pre&gt;ER..............................3......|..f1.f1.fSfQ.W....R..|.........K...R.A..U1.0....r...U.u....t.f.....B....1.ZQ....[...@P..?Q..SRP..|...f....D.....f@.....f.&amp;gt;@|..xpu....{.D|.....isolinux.bin missing or corrupt...f`f1.f...{f...{fRfP.Sj.j...f.6.{.........6.{....A......{...d.fa....Operating system load error...^....&amp;gt;b.....&amp;lt;.u........................&lt;/pre&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2020年1月4日 00:59:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/59582433.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定