简体   繁体   English

保持div内的文字:换行符,空格和长字

[英]Keeping text within div: Both newline, white-space and long words

Say I have the following string: 说我有以下字符串:

"This line should always break on newline \n, spaces if it is about to overflow and veryLongLinksThatDoNotFitInsideTheDiv"

I want it to break on: 我希望它断开:

  • The \\n (always) \\ n(总是)
  • The white spaces (if it is about to overflow) 空白(如果即将溢出)
  • The long word (if it does not fit on an entire single line) 长字(如果不适合整行)
  • But preferably not on random small words that could fit on the next line ( How to limit break-all to only long words? ) 但最好不要在可能适合下一行的随机小单词上使用( 如何将“全部”限制为仅长单词?

Based on text flowing out of div , I tried: 基于从div流出的文本 ,我尝试了:

    <div id="log" style="word-break: break-all"></div>
    <!-- <div id="log" style="word-wrap: break-word"></div> -->
    <!-- <div id="log" style="white-space: pre-line"></div> -->
    <!-- <div id="log" style="white-space: pre; word-break: break-all"></div> -->

But I can not get all 3 (nor 4) criteria met. 但是我不能满足所有3个(也没有4个)标准。

Fiddle: https://jsfiddle.net/6kq14cfc/ 小提琴: https : //jsfiddle.net/6kq14cfc/

You can use 您可以使用

<div id="log" style="overflow-wrap: break-word;white-space: pre-wrap;"></div>

or 要么

<div id="log" style="word-wrap: break-word;word-break: normal;white-space: pre-wrap;"></div>

I think you're answer lies in replacing "\\n" with "<br />". 我认为您的答案在于将“ \\ n”替换为“ <br />”。 HTML does not recognize new line characters - it needs the <br /> tag instead. HTML无法识别换行符-它需要<br />标记。

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

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