Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to preserve whitespace but ignore line breaks in CSS?

Tags:

html

css

The white-space property in CSS-3 has the pre-wrap value, which will preserve whitespace and line breaks, and wrap when necessary, and the pre-line value, which collapses whitespace, but preserves line breaks, and wraps when necessary.

What it does not have, though, is a value that preserves whitespace and collapses line breaks, while still wrapping otherwise.

Is there a combination of other CSS properties that I can use to get this effect?

For example:

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

should look like:

This  example code should     render on  one  line  but with spaces     intact.
like image 782
Roger Avatar asked Apr 19 '16 19:04

Roger


1 Answers

Answer to your question: no, there is no way to do it by only using CSS (prove for white-space)

behaviors of all the different values for white-space prop There is no line such as: collapse | preserve | ...

The only way to do it is by using 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.

Hardcode methods


1st

* {
  white-space: pre-wrap;
}

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

2nd

This&nbsp; example
code
should &nbsp; &nbsp; render
on&nbsp; one&nbsp; line&nbsp; but
with spaces &nbsp; &nbsp; intact.
like image 106
Kas Elvirov Avatar answered Oct 07 '22 00:10

Kas Elvirov