In a textarea, there are new lines using for example 
, but how can I do this to a div element without using htmll (plain text like \n) ?
Asked
Active
Viewed 7.1k times
30

user3681138
- 337
- 1
- 3
- 9
-
`\n` || `\r` || `"\n"` || `"\r"` – SpYk3HH Jul 25 '14 at 20:19
-
It doesn't work inside a div element. – user3681138 Jul 25 '14 at 20:20
-
2Add `white-space: pre;` CSS rule. – Jul 25 '14 at 20:27
1 Answers
83
You can use CSS to force HTML to recognize new lines as line breaks.
Just set the white-space
property to pre
:
div {
white-space: pre;
}

AstroCB
- 12,337
- 20
- 57
- 73
-
-
1All of them, all the way back to IE6. https://developer.mozilla.org/en-US/docs/Web/CSS/white-space#Browser_compatibility – mrdommyg Jul 06 '16 at 19:38
-
3Excellent fix for when taking text value from a text-area and applying to a div. Extra credit: use pre-wrap to allow word-break property work as needed (IE8+) – Mark Sep 01 '16 at 22:45
-
-
7Or you can use `pre-wrap` https://www.w3schools.com/cssref/pr_text_white-space.asp – onmyway133 Jul 07 '17 at 08:15
-
-