I recently realised that I had some old posts on my blog with some line break tags (
<br/>) in them, however what I had been using in my recent posts was self-closing line breaks without the slash (
<br>). So I wondered, which one is the most correct to use?
What I would like to condense this down to is that
<br> is not valid XML as there is no closing tag and it is not self closing. You may ask, what does this have to do with HTML? Well XHTML and some parsers require the HTML to be valid XML. Most times this is not an issue and you could use the valid HTML tag
<br> just fine. However if you need to parse your HTML as XML you should be using the self-closing tag
Other than that: whether to make it self closing (
<br />) or not (
<br>) is a preference, when it is self-closing it is valid XML and XHTML - but it is just as valid HTML without the self-closing slash.
If you choose not to make it self-closing, do not add a closing tag and never add a value to it.
<br> is a void element in HTML, it cannot have any values:
18.104.22.168 Void Elements
The term void elements is used to designate elements that must be empty. These requirements only apply to the HTML syntax. In XHTML, all such elements are treated as normal elements, but must be marked up as empty elements.
These elements are forbidden from containing any content at all. In HTML, these elements have a start tag only. The self-closing tag syntax may be used. The end tag must be omitted because the element is automatically closed by the parser.
A void element in the HTML syntax. This is not permitted in the XHTML syntax.
A void element using the HTML- and XHTML-compatible self-closing tag syntax.
A void element using the XHTML-only syntax with an explicit end tag. This is not permitted for void elements in the HTML syntax.
Such elements include, among others, br, hr, link and meta.
That is it!
I hope you enjoyed this post on the line break (br) tag, please let me know in the comments if you did :)