HTML 5: Is it <br>, <br/>, or <br />?

ID : 370

viewed : 180

Tags : htmlhtml





Top 5 Answer for HTML 5: Is it <br>, <br/>, or <br />?

vote vote

92

Simply <br> is sufficient.

The other forms are there for compatibility with XHTML; to make it possible to write the same code as XHTML, and have it also work as HTML. Some systems that generate HTML may be based on XML generators, and thus do not have the ability to output just a bare <br> tag; if you're using such a system, it's fine to use <br/>, it's just not necessary if you don't need to do it.

Very few people actually use XHTML, however. You need to serve your content as application/xhtml+xml for it to be interpreted as XHTML, and that will not work in old versions of IE - it will also mean that any small error you make will prevent your page from being displayed in browsers that do support XHTML. So, most of what looks like XHTML on the web is actually being served, and interpreted, as HTML. See Serving XHTML as text/html Considered Harmful for some more information.

vote vote

87

I think this quote from the HTML 5 Reference Draft provides the answer:

3.2.2.2 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.

HTML Example:
A void element in the HTML syntax. This is not permitted in the XHTML syntax.

<hr> 

Example:
A void element using the HTML- and XHTML-compatible self-closing tag syntax.

<hr/> 

XHTML Example:
A void element using the XHTML-only syntax with an explicit end tag. This is not permitted for void elements in the HTML syntax.

<hr></hr> 

In other words:

  • Invalid HTML: <IMG></IMG>, <IMG/>
  • Valid HTML: <IMG>

And while HTML forbids certain closing tags, xhtml requires them:

  • Invalid xhtml: <img>
  • Valid xhtml: <img></img> or <img/>

Other elements that are forbidden from having a closing tag in HTML:

Element Valid HTML Valid xhtml
AREA <AREA> <AREA></AREA>
BASE <BASE> <BASE></BASE>
BASEFONT <BASEFONT> <BASEFONT></BASEFONT>
BR <BR> <BR></BR>
COL <COL> <COL></COL>
FRAME <FRAME> <FRAME></FRAME>
HR <HR> <HR></HR>
IMG <IMG> <IMG></IMG>
INPUT <INPUT> <INPUT></INPUT>
ISINDEX <ISINDEX> <ISINDEX></ISINDEX>
LINK <LINK> <LINK></LINK>
META <META> <META></META>
PARAM <PARAM> <PARAM></PARAM>

The fact that HTML forbids certain closing tags, while xhtml requires them is xhtml's problem. If you're writing HTML, you follow the HTML rules.

At the same time, browers gave up trying to enforce the standards, because everyone gets it wrong. It's not obvious:

  • that </BR> is forbidden
  • that </P> is optional
  • and </SPAN> is required

And then xhtml came along, with it's XML rule that every element must have a closing tag, and people just assumed that HTML was the same thing. So the standards gave up, and were later revised to throw up their hands to the reality.

But <BR> is the only true form.

vote vote

79

XML doesn't allow leaving tags open, so it makes <br> a bit worse than the other two. The other two are roughly equivalent with the second (<br/>) preferred for compatibility with older browsers. Actually, space before / is preferred for compatibility sake, but I think it only makes sense for tags that have attributes. So I'd say either <br/> or <br />, whichever pleases your aesthetics.

To sum it up: all three are valid with the first one (<br>) being a bit less "portable".

Edit: Now that we're all crazy about specs, I think it worth pointing out that according to dev.w3.org:

Start tags consist of the following parts, in exactly the following order:

  1. A "<" character.
  2. The element’s tag name.
  3. Optionally, one or more attributes, each of which must be preceded by one or more space characters.
  4. Optionally, one or more space characters.
  5. Optionally, a "/" character, which may be present only if the element is a void element.
  6. A ">" character.
vote vote

63

In HTML (up to HTML 4): use <br>

In HTML 5: <br> is preferred, but <br/> and <br /> is also acceptable

In XHTML: <br /> is preferred. Can also use <br/> or <br></br>

Notes:

  • <br></br> is not valid in HTML 5, it will be thought of as two line breaks.
  • XHTML is case sensitive, HTML is not case sensitive.
  • For backward compatibility, some old browsers would parse XHTML as HTML and fail on <br/> but not <br />

Reference:

vote vote

53

According to the spec the expected form is <br> for HTML 5 but a closing slash is permitted.

Top 3 video Explaining HTML 5: Is it <br>, <br/>, or <br />?







Related QUESTION?