572

Is there a way to make HTML properly treat \n line breaks? Or do I have to replace them with <br/>?

<div class="text">
  abc
  def
  ghi
</div>
Penny Liu
  • 15,447
  • 5
  • 79
  • 98
stkvtflw
  • 12,092
  • 26
  • 78
  • 155

11 Answers11

900

This is to show new line and return carriage in HTML. Then you don't need to do it explicitly. You can do it in CSS by setting the white-space attribute pre-line value.

<span style="white-space: pre-line">@Model.CommentText</span>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
khakishoiab
  • 9,673
  • 2
  • 16
  • 22
349

You can use CSS white-space property for \n. You can also preserve the tabs as in \t.

For line break \n:

white-space: pre-line;

For line break \n and tabs \t:

white-space: pre-wrap;

document.getElementById('just-line-break').innerHTML = 'Testing 1\nTesting 2\n\tNo tab';

document.getElementById('line-break-and-tab').innerHTML = 'Testing 1\nTesting 2\n\tWith tab';
#just-line-break {
  white-space: pre-line;
}

#line-break-and-tab {
  white-space: pre-wrap;
}
<div id="just-line-break"></div>

<br/>

<div id="line-break-and-tab"></div>
Darlesson
  • 5,742
  • 2
  • 21
  • 26
  • 7
    This is available for a long time and it's [supported by all major browsers](https://developer.mozilla.org/en-US/docs/Web/CSS/white-space#Browser_compatibility). – Darlesson Jun 21 '18 at 21:45
94

It can be done various ways.

For example, if you want to insert a new line in a text area, you can use these:

&#10; line feed and &#13; carriage return, used like this:

<textarea>Hello &#10;&#13;Stackoverflow</textarea>

You can also use <pre>---</pre> preformatted text like this:

<pre>
  This is line 1
  This is line 2
  This is line 3
</pre>

Or you can use a <p>----</p> paragraph tag like this:

<p>This is line 1</p>
<p>This is line 2</p>
<p>This is line 3</p>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sampad
  • 1,645
  • 11
  • 14
  • 7
    Could you please elaborate on your last note? I don't understand why you think server side support is required for this... – Shadow Jul 18 '17 at 23:27
  • 3
    The last sentence here is objectively false, as several other answers have demonstrated. And even if it weren't supported natively, you could easily change it with client-side JavaScript. – John Montgomery May 12 '20 at 22:48
37

You could use the <pre> tag

<div class="text">
<pre>
  abc
  def
  ghi
</pre>
  abc
  def
  ghi
</div>
Johan Brännmar
  • 886
  • 7
  • 21
28

Using white-space: pre-line allows you to input the text directly in the HTML with line breaks without having to use \n

If you use the innerText property of the element via JavaScript on a non-pre element e.g. a <div>, the \n values will be replaced with <br> in the DOM by default

  • innerText: replaces \n with <br>
  • innerHTML, textContent: require the use of styling white-space

It depends on how your applying the text, but there are a number of options

const node = document.createElement('div');
node.innerText = '\n Test \n One '
Drenai
  • 11,315
  • 9
  • 48
  • 82
  • 1
    Interesting. I ran into this on accident. Is there any documentation describing this? – Trade-Ideas Philip Aug 14 '21 at 23:46
  • 1
    There's a number of different concepts here, and the `br` behavior is browser dependent - so it's unlikely to be described as a single piece of documentation. I've rarely used `innerText`, but use `white-space` styling regularly – Drenai Aug 15 '21 at 16:36
21

You can use <pre> tag:

<div class="text">
<pre>
abc
def
ghi
</pre>
</div>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ehsan
  • 12,655
  • 3
  • 25
  • 44
14

You can use this CSS property:

white-space: pre-line
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
11

Simple and linear:

 <p> my phrase is this..<br>
 the other line is this<br>
 the end is this other phrase..
 </p>
Alessandro Ornano
  • 34,887
  • 11
  • 106
  • 133
  • The
    HTML element produces a line break in text (carriage-return). It is useful for writing a poem or an address, where the division of lines is significant. This is easier. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/br
    – Diego Lope Loyola Dec 04 '21 at 06:08
  • @DiegoLopeLoyola I agree with you but sometimes some compilers or programming languages unfortunately accepts only this HTML tag for this purpose. – Alessandro Ornano Dec 04 '21 at 12:00
10

You can use any of the following CSS,

white-space: pre-line;

or

white-space: pre-wrap;

or

white-space: break-spaces;

For more info read: https://developer.mozilla.org/en-US/docs/Web/CSS/white-space

Ajay Sivan
  • 2,807
  • 2
  • 32
  • 57
3

A simple and more natural solution that doesn't involve CSS styles or numeric character references like &#13;&#10; would be to use the &NewLine; character entity reference:

The primary colors are:&NewLine;- Red&NewLine;- Green&NewLine;- Blue

Note: Since this is defined simply as the LF (line feed, or the U+000A Unicode code point) character, it can be debatable whether it suits scenarios where the entire CR + LF (carriage return + line feed) sequence is required. But then, it worked in my Chrome, Edge and WebView2 tests done in Windows 10, so it should be safe to use.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Yin Cognyto
  • 986
  • 1
  • 10
  • 22
  • This worked but required CSS `white-space: pre-line;`. Both work well together since some code formatters remove white space. – HelloWorldPeace Oct 11 '22 at 14:59
  • What is the intent of `&NewLine;`? Literal? Rendered somehow? Some kind of encoding of non-printable ASCII character? Can you elaborate? – Peter Mortensen Feb 14 '23 at 01:46
  • 1
    @PeterMortensen It's just the character entity reference of a line feed, similar to how ` ` from the accepted answer is its numerical (decimal) entity reference in XML / HTML. They are standard textual or numerical representations of a character defined in the [HTML specifications](https://html.spec.whatwg.org/multipage/named-characters.html#named-character-references) and can be used for almost all Unicode characters, irrespective if they are printable or not. It's not specially rendered, it's just one of the three ways (literal, numerical, textual) to represent such an "entity" in HTML. – Yin Cognyto Feb 14 '23 at 16:00
1

\n or \t or \r are interpreted inside of <pre>---</pre>

It worked for me.

Tireuuuuu
  • 43
  • 6