简体   繁体   English

溢出换行:断词不影响文本

[英]overflow-wrap: break-word doesn't effect text

I have this css code我有这个 css 代码

 .LogLink { font-family:courier; white-space: pre; font-size: 11.6px; border: 2px solid rgb(44, 44, 44); background-color:rgb(240, 240, 240); padding:5px; margin-top:5px; margin-bottom:5px; margin-left:20px; margin-right:20px; overflow-wrap: break-word; }
 <h2 style='text-align:center'>LogLink: $BuildID</h2> <p class='LogLink'>[Env] Lorem ipsum dolor sit amet, consectetur adipiscing elit [Env] Lorem ipsum dolor sit amet, consectetur adipiscing elit [Env] Lorem ipsum dolor sit amet, consectetur adipiscing elit [Env] Lorem ipsum dolor sit amet, consectetur adipiscing elit.............................................................................................................................................................................................................</p>

This is how it's displayed on my webpage, and that's good enough:这就是它在我的网页上的显示方式,这已经足够了: 是这样显示的

the problem is when I have these long lines of text/symbols:问题是当我有这些长行文本/符号时: 在此处输入图像描述

I thought that overflow-wrap: break-word;我认为overflow-wrap: break-word; would help, but it doesn't do anything.会有所帮助,但它没有做任何事情。 Is there a way to make all the text fit page width, without getting a horizontal scrollbar, but a new line?有没有办法让所有文本都适合页面宽度,而不需要水平滚动条,而是换行? NOTE: I can't edit the text I receive.注意:我无法编辑收到的文本。

The reason why i use white-space: pre;我使用white-space: pre; is to display my received database text.是显示我收到的数据库文本。 It's received in one line.它是在一行中收到的。

EDIT white-space: pre;编辑white-space: pre; changed tro white-space: pre-line;更改 tro white-space: pre-line; worked!工作!

 .LogLink { font-family:courier; white-space: pre-line; font-size: 11.6px; border: 2px solid rgb(44, 44, 44); background-color:rgb(240, 240, 240); padding:5px; margin-top:5px; margin-bottom:5px; margin-left:20px; margin-right:20px; overflow-wrap: break-word; }
 <h2 style='text-align:center'>LogLink: $BuildID</h2> <p class='LogLink'>[Env] Lorem ipsum dolor sit amet, consectetur adipiscing elit [Env] Lorem ipsum dolor sit amet, consectetur adipiscing elit [Env] Lorem ipsum dolor sit amet, consectetur adipiscing elit [Env] Lorem ipsum dolor sit amet, consectetur adipiscing elit.............................................................................................................................................................................................................</p>

try setting a max-width on the container containing the <p> tag, or even on the <p> tag itself.尝试在包含<p>标记的容器上,甚至在<p>标记本身上设置max-width using width and max-width together will allow it to be responsive;一起使用widthmax-width将使其具有响应性; width: 400px max-width: 85% width: 400px max-width: 85%

Use white-space: pre-wrap;使用white-space: pre-wrap; or white-space: pre-line;white-space: pre-line;

Here's a codepen showing an example with pre-wrap : https://codepen.io/DavidSabine/pen/Exxvybe这是一个显示带有pre-wrap示例的代码笔: https://codepen.io/DavidSabine/pen/Exxvybe

And here's the CSS from my example:这是我的示例中的 CSS:

.LogLink {
    font-family:courier;
    white-space: pre-wrap;
    font-size: 11.6px;
    border: 2px solid rgb(44, 44, 44);  
    background-color:rgb(240, 240, 240);  
    padding:5px;  
    margin-top:5px;  
    margin-bottom:5px;  
    margin-left:20px;  
    margin-right:20px;  
    overflow-wrap: break-word;
  }

The different between pre-wrap and pre-line is described here: https://developer.mozilla.org/en-US/docs/Web/CSS/white-space pre-wrap 和 pre-line 之间的区别在这里描述: https://developer.mozilla.org/en-US/docs/Web/CSS/white-space

Whether to use pre-wrap or pre-line depends whether you want white space sequences preserved or collapsed.是使用pre-wrap还是pre-line取决于您是要保留还是折叠空白序列。 This would largely depend on what your raw output looks like (from your database).这在很大程度上取决于您的原始 output 的样子(来自您的数据库)。 I recommend try both.我建议尝试两者。 Both are well supported in browsers.两者都在浏览器中得到很好的支持。 See the reports at caniuse.com:请参阅 caniuse.com 上的报告:

https://caniuse.com/#search=pre-line https://caniuse.com/#search=pre-line

https://caniuse.com/#search=pre-wrap https://caniuse.com/#search=pre-wrap

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

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