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

时间:2016-04-19 19:17:43

标签: html css

CSS-3中的white-space属性具有pre-wrap值,它将保留空格和换行符,并在必要时换行,以及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.

1 个答案:

答案 0 :(得分:2)

回答您的问题:否,没有办法仅使用CSSwhite-space使用prove

behaviors of all the different values for white-space prop 没有诸如以下的行: 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.

硬编码方法


第一

* {
  white-space: pre-wrap;
}

br {
    display: none;
}
This  example<br/>code<br/>should     render<br/>on  one  line  but<br/>with spaces     intact.<br/>


第二

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

相关问题