繁体   English   中英

如何保留空格但忽略CSS中的换行符?

[英]How to preserve whitespace but ignore line breaks in CSS?

CSS-3中的white-space属性具有pre-wrap值,它将保留空格和换行符,并在必要时换pre-line ,以及折叠空白的pre-line值,但保留换行符,并在必要时换行。

没有什么,不过,是保留空白, 崩塌换行,同时还包裹否则的值。

是否有其他CSS属性的组合可用于获得此效果?

例如:

This  example
code
should     render
on  one  line  but
with spaces     intact.

应该是这样的:

This  example code should     render on  one  line  but with spaces     intact.

回答你的问题 :不,只有使用CSS才能做到这一点( 证明white-space

白空间道具的所有不同值的行为 没有诸如: collapse | 保存 | ...

唯一的方法是使用JavaScript

 document.body.innerHTML = document.body.innerHTML.replace(/\\n/g, ''); 
 html { white-space: pre-wrap; } 
 This example code should render on one line but with spaces intact. 

硬编码方法


1

 * { white-space: pre-wrap; } br { display: none; } 
 This example<br/>code<br/>should render<br/>on one line but<br/>with spaces intact.<br/> 


第2

 This&nbsp; example code should &nbsp; &nbsp; render on&nbsp; one&nbsp; line&nbsp; but with spaces &nbsp; &nbsp; intact. 

暂无
暂无

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

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