简体   繁体   English

空白:预不按预期工作

[英]white-space: pre not working as expected

I have text coming back from the server containing carriage return (enter).我有从服务器返回的包含回车(回车)的文本。 I asked some people what to do to display this text on multiple lines, and they advised me to use the white-space: pre .我问一些人如何在多行上显示此文本,他们建议我使用white-space: pre

Is there any other way to display the text on multiple lines without white-space: pre ?有没有其他方法可以在没有white-space: pre情况下在多行上显示文本white-space: pre Or any way to make pre act like a paragraph but with multiple lines without having the extra padding?或者有什么方法可以让pre得像一个段落,但有多行而没有额外的填充?

Here's what I have:这是我所拥有的:

 .gallery-Gcontainer { margin: 0 !important; padding: 0; } #genericpartTitle, .content { padding-left: 20px; } .content .sidebar, .content section { display: inline-block; width: 30%; border: none; vertical-align: top; } .content .sidebar img { width: 200px; }
 <div class="gallery-Gcontainer"> <div class="header-area"> <h3 id="genericpartTitle">my page</h3> <hr> </div> <div class="content"> <div class="sidebar"> <img class="img-sidebar" src="https://static.pexels.com/photos/30738/pexels-photo-30738.jpg" /> </div> <section> <h5 class="item-title">Welcome to my first page</h5> <p style="white-space: pre" class="description"> Hello, anything will go here, text will come from the server like this, and I need to display it on multiple lines. </p> </section> </div> </div>

Update: Putting: white-space: pre-line as suggested in the answers, solved the padding left issue, but still there's a big space in padding-top.更新: Putting: white-space: pre-line 正如答案中所建议的那样,解决了 padding left 问题,但 padding-top 中仍然有很大的空间。

Instead of white-space: pre use white-space: pre-line .代替white-space: pre使用white-space: pre-line

From MDN:来自 MDN:

The white-space propertywhite-space属性

The white-space property is used to describe how whitespace inside the element is handled. white-space属性用于描述如何处理元素内的空白。

normal Sequences of whitespace are collapsed.正常的空白序列被折叠。 Newline characters in the source are handled as other whitespace.源中的换行符作为其他空格处理。 Breaks lines as necessary to fill line boxes.根据需要换行以填充行框。

nowrap Collapses whitespace as for normal , but suppresses line breaks (text wrapping) within text. nowrapnormal一样折叠空白,但禁止文本中的换行符(文本换行)。

pre Sequences of whitespace are preserved. pre保留空白序列。 Lines are only broken at newline characters in the source and at <br> elements.仅在源和<br>元素中的换行符处换行。

pre-wrap Sequences of whitespace are preserved. pre-wrap保留了空白序列。 Lines are broken at newline characters, at <br> , and as necessary to fill line boxes.在换行符、 <br>以及根据需要填充行框时换行。

pre-line Sequences of whitespace are collapsed.前行空白序列被折叠。 Lines are broken at newline characters, at <br> , and as necessary to fill line boxes.在换行符、 <br>以及根据需要填充行框时将换行。

Also, the padding-top issue you mention isn't really padding.此外,您提到的padding-top问题并不是真正的填充。 In looking at your code there appears to be at least one line break at the start of the line.在查看您的代码时,该行的开头似乎至少有一个换行符。

If possible then use below javascript code for CSS .如果可能,请使用以下 javascript 代码进行 CSS。

var ss = " Hello`, anything will go here, text will come from the server like this, and I need to display it on multiple lines.";

document.getElementById('test').innerHTML = ss.replace(/\n/g, '<br/>');  

or或者

document.getElementById('test').innerHTML = ss.replace(/\n/g, '<br>\n');

<p id="test"> </p>

use "display:block" as your css styling.使用“display:block”作为你的 css 样式。 This should break onto new line.这应该打破到新的行。

<p style="display: block" class="description">

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

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