Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to recognize new line character (\n) in HTML

Tags:

html

I have an application in which I receive a comment string from REST Api like this

"comments":"This is a comment\nAnother comment\nOne more\nLast

I'm displaying it using p tag but it's not recognizing line breaks(\n). How to have a newline?

like image 427
Protagonist Avatar asked Mar 02 '17 05:03

Protagonist


People also ask

How do you show a new line character in HTML?

To add a line break to your HTML code, you use the <br> tag. The <br> tag does not have an end tag. You can also add additional lines between paragraphs by using the <br> tags.

Is it n or \n for new line?

Operating systems have special characters denoting the start of a new line. For example, in Linux a new line is denoted by “\n”, also called a Line Feed. In Windows, a new line is denoted using “\r\n”, sometimes called a Carriage Return and Line Feed, or CRLF.

What does \n do in HTML?

The \n character matches newline characters.

What is \n line break?

The line end (EOL), line break, line feed, line break or carriage return character is a control character, which is the end of a text line, and the following character should start in a different line. It's \r\n on the Windows system, and it's Linux on \n.


1 Answers

<pre> tag is set to recognize newlines. You can make the <p> behave like <pre> via CSS but you will loose any HTML in the <p> (if there are any).

Alternatively, you have to replace them with HTML linebreak <br> or <br/>

like image 189
erosman Avatar answered Sep 20 '22 08:09

erosman