If both <br> and <br /> are valid syntaxes, which one should I use?


#1

Question

If both <br> and <br/> are valid syntaxes, which one should I use?

Answer

There is no consensus on this and either syntax is fine to use. Whichever syntax you choose, be sure to be consistent with it.


FAQ: Learn HTML Elements - Intro to HTML - Line Breaks
#2

Ive been reading about html and xhtml, wouldnt it be better to use <br/> or <br></br> in case we need it to be read by xml readers?

HTML5 does not require empty elements to be closed. But if you want stricter validation, or if you need to make your document readable by XML parsers, you must close all HTML elements properly.
W3Schools


#3

HTML5 is essentially HTML 4.1 when served with the default MIME type, text/html. Under this MIME type the browser never parses XML so there is no need to close void elements.

It is when we explicitly serve the document with an XML MIME type, then HTML5 is equivalent to XHTML which must conform to XML syntax. That means all void elements must be closed. HTML5 came along at a time when browser quirks were being ironed out and that allowed it to remove the space before the slash that was needed in XHTML. Hence,

<br/>

and

<br />

are the same thing, and both are valid.

Generally speaking, a production site that serves XML conforming HTML5 will have its server configured to send that MIME type in the header of pages served out so that no declaration is needed in the document.

As for best practices, subjectivity should be avoided, as it tends to introduce bias. The HTML 4 standard should be applied across the board and closing void tags set aside when not explicitly required.

<br>
<hr>
<input>
<img>

says it all. This document is text/html. Closing these tags will not impress anybody.


#5

I agree that in HTML5, void elements should not contain a space and/or a forward slash at the end, as per the W3C. It’s only required in XHTML and XML.

Regarding being consistent with the way we write our tags, I’ve noticed that in Codecademy’s lessons, the image element is always closed with a space and a slash like <img /> while all the other self-closing elements in the document are written without the space and slash, e.g., <meta> or <link> or <br>.

I can see how this could be confusing to beginning students of web development or anyone learning HTML.

I like to keep things as simple as possible when writing code, so I always omit the slash and/or space when closing void elements. For example:

<meta>
<link>
<br>
<img>

By the way, the terms void element, empty element, and self-closing tag all mean the same thing.


#6

Thanks you for help…again :sweat_smile:


#7

Thank U~~:kissing_heart:


#8

hmmmm…confused a little bit…


#9

somos 2, pero bueno, a echarle ganas!!


#10

I think if XML inside your Website we should use
else it’s fine whatever you use