Html - Newline Char in Div Content Editable?

Html - Newline Char in Div Content Editable?

I am storing content in the database, for example:

Hello
This
is 
Text

and when I pass that to a textarea, it stays with the new line breaks. But if I pass that text to a div with content editable, it would stay like this:

Hello This is Text

How can I fix this problem?

2

4 Answers

Set a style on the div: white-space: pre or white-space: pre-wrap

Example:

10

To add some info on @Explosion Pills correct answer and extract some info from MDN:

The CSS white-space attribute is there to help:

pre:

white-space: pre

Sequences of white space are preserved. Lines are only broken at newline characters in the source and at <br> elements.

This might result in unwanted horizontal scrollbars as shown in the example!

Elena Rostova
Author

Elena Rostova

Elena Rostova holds a Master's degree in Public Health Journalism. She covers groundbreaking medical research, holistic wellness trends, mental health awareness, and nutritional science.