Hướng dẫn slash n in html

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

abc def ghi

Hướng dẫn slash n in html

Penny Liu

12.9k5 gold badges71 silver badges86 bronze badges

asked Sep 5, 2016 at 7:08

0

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.

@Model.CommentText

Hướng dẫn slash n in html

bad_coder

9,39619 gold badges37 silver badges61 bronze badges

answered Sep 5, 2016 at 7:13

khakishoiabkhakishoiab

8,3332 gold badges15 silver badges22 bronze badges

1

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;

answered Jul 18, 2017 at 23:14

DarlessonDarlesson

5,0822 gold badges20 silver badges25 bronze badges

1

As per your question, it can be done by various ways.

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

line feed and carriage return, used like this:

You can also use

---
preformatted text like this:

  This is line 1
  This is line 2
  This is line 3

Or you can use a

----

paragraph tag like this:

  

This is line 1

This is line 2

This is line 3

Hướng dẫn slash n in html

maxshuty

8,17510 gold badges57 silver badges71 bronze badges

answered Sep 5, 2016 at 7:43

SampadSampad

1,44310 silver badges14 bronze badges

2

You could use the

 tag

  abc
  def
  ghi
abc def ghi

answered Sep 5, 2016 at 7:11

0

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

, the \n values will be replaced with
in the DOM by default

  • innerText: replaces \n with
  • 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 '

answered Dec 13, 2018 at 9:29

Hướng dẫn slash n in html

DrenaiDrenai

10.3k8 gold badges43 silver badges77 bronze badges

2

you can use

 tag :

abc
def
ghi

answered Sep 5, 2016 at 7:12

Hướng dẫn slash n in html

EhsanEhsan

12.3k3 gold badges23 silver badges43 bronze badges

0

Simple and linear:

 

my phrase is this..
the other line is this
the end is this other phrase..

answered Aug 29, 2018 at 17:23

Hướng dẫn slash n in html

Alessandro OrnanoAlessandro Ornano

33.9k11 gold badges100 silver badges129 bronze badges

2

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

The primary colors are:
- Red
- Green
- 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.

answered May 14 at 15:59

Hướng dẫn slash n in html

Yin CognytoYin Cognyto

8469 silver badges21 bronze badges