英文:
Wrapping <pre> to a fixed width such that each line is the same length in characters
问题
我试图将这个长的<pre>包装起来,使其形成一个矩形(而不是出现我得到的“锯齿状”结束)。我已经将white-space设置为pre-wrap,word-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.>@|..xpu....{.D|.....isolinux.bin missing or corrupt...f`f1.f...{f...{fRfP.Sj.j...f.6.{.........6.{....A......{...d.fa....Operating system load error...^....>b.....<.u........................</pre>
我的现状:
我想要实现的:
英文:
I'm attempting to wrap this long <pre> 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 <pre> into rows a-priori because both the length of the content, as well as the width of the <pre> 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 -->
<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>
<!-- end snippet -->
What I have:
What I want to achieve:
答案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.>@|..xpu....{.D|.....isolinux.bin missing or corrupt...f`f1.f...{f...{fRfP.Sj.j...f.6.{.........6.{....A......{...d.fa....Operating system load error...^....>b.....<.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 -->
<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>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。




评论