HTML

what is the difference between </br> and \n ?

hi,

what is the difference between </br> and \n ? like this

https://imgur.com/a/Iqg4D

i get confused with these two, it seems they have similar function. Thanks :)

1 Answer

Steven Parker
Steven Parker
243,898 Points

The tag "<br>" is HTML code that causes a line break on the page. But "\n" is a character escape sequence that represents a newline character.

They would generally be found in different places. In an HTML file, you would see "<br>" but not "\n". But in script code, you would see "\n" used for console messages. You might also see "<br>" but only if the purpose of the script is to create or modify HTML.

thanks Steven, I have edited my question. I am following PHP course and I see </br> and \n like this https://imgur.com/a/Iqg4D

i see both used in the script

Steven Parker
Steven Parker
243,898 Points

This script code is generating HTML. The "<br />" code will cause the browser to display the following text on a new line, but the "\n" will actually add a newline into the HTML file. The latter one will not be displayed on the browser page and will only be seen by someone viewing the page source.

Using both keeps the source looking similar to the final page. You'll notice that the h2 element in your example has only the "\n" because header elements take up an entire line by default when the browser displays them.