HTML: What's the correct form of BR?
Asked Answered
A

8

29

How is correct to write the br tag in html?

<br />

or

<br>

or anything else? :))

Amphibious answered 26/11, 2010 at 20:50 Comment(4)
Depends on your doctype. Which one are you using?Goodloe
@CIRK Yea, but HTML or XHTML?Maggoty
There's no "HTML 1.0 Transitionl", so presumably they mean XHTML 1.0 Transitional. Being clearer would be good though.Hepatitis
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//ENAmphibious
N
50

It depends on the doctype you're using. In the original versions of HTML (up to HTML4), only <br> is correct. In XHTML (which is based on XML), only <br /> is correct. In HTML5, both are allowed although <br> is preferred.

Nonillion answered 26/11, 2010 at 20:53 Comment(11)
I'll never understand why they are moving away from XML requirements with HTML5 - HTML as valid XML was a very useful move.Unfreeze
@Orbling: Because HTML and XHTML are not the same. HTML5 is the true successor to HTML4 while XHTML was and continues to essentially be an XML wrapper over HTML.Nonillion
@Nonillion Yes I get that that entirely - but I thought the common consensus (and this is the division of the industry I work within, web), was that XHTML was a vast improvement from a parsing and validity perspective and that getting rid of the old way was a matter of priority, rather than bringing it back.Unfreeze
@Orbling: HTML is just as well-defined as XML is and both can be parsed strictly if desired. However, all the major browsers accept malformed (X)HTML simply because half the web would be broken otherwise.Nonillion
@Nonillion Don't I know it, another case of providing for the incompetent developer. The improvement came by using a commonly used data structure to represent HTML, negating the need for it to have it's own special parser.Unfreeze
@Orbling: That must have been the reason they introduced XHTML in the first place, but I don't see why HTML and XML -- two languages used for entirely different purposes -- need to share the same syntax. Nevertheless, HTML5 supports the XML format as well: w3.org/QA/2008/01/html5-is-html-and-xml.htmlNonillion
@Nonillion Aye, I just fear increased confusion in web standards once again. People are already often using HTML5 constructs in declared XHTML pages, feature support is hazy, confusion amongst the amateur developers is rife. They don't need to share the same syntax, it is just prudent to reuse an existing well-defined standard than make yet another. Anyhow, this is another debate. :-)Unfreeze
@Orbling: Be careful when you say "reuse an existing well-defined standard". HTML was standardized long before XML was even invented.Nonillion
@Nonillion Aye, but HTML is specialised, while XML is general. They both are variants of SGML. I think the overview section on Wikipedia for XHTML covers this all quite well (en.wikipedia.org/wiki/XHTML#Overview) - the general antipathy towards XHTML seems to run solely to the fact that many websites are coded by amateurs who write invalid code. OK, it is a primary goal of the internet to be inclusive by design, so Berners-Lee is onboard the backwards compatibility, broken compatibility bandwagaon. But as a professional, it grieves me.Unfreeze
@Orbling: Actually HTML5 is no longer based on SGML -- see the same link that I posted earlier. They finally decided that it deserves to be a language by itself, but also provide the XML variant for those who wish to use it.Nonillion
@Nonillion I said HTML and XML are variants of SGML, HTML5 is not no. What irks me on the provision of XHTML5, is they still state that HTML5 is preferred, the word in your post that originally caused this debate.Unfreeze
S
9

It does not need an end tag, so

<br>

is correct for HTML, and

<br />

for XHTML.

Stedt answered 26/11, 2010 at 20:53 Comment(0)
H
8

In HTML4.01 or earlier, it is <br>. This is case-insensitive, and whitespace between the r and the > is ignored.

In all forms of XHTML it is <br/>. This is case-sensitive, and again whitespace between the r and the /> is ignored. <br></br> is also allowed, but unusual (there are backwards-compatibility issues, it's extra typing, and allows for bugs if you introduce more content between the tags by mistake, but it does sometimes happen in XML-programattically produced code).

Since some earlier browsers that would parse XHTML as earlier HTML would choke on <br/> but not on <br /> such extra whitespace is the norm, for backwards compatibility (more backwards kludging it, but still...).

In HTML5 either are allowed (but not <br></br>).

Hepatitis answered 26/11, 2010 at 20:58 Comment(2)
Fun fact: <br></br> renders as two line breaks in IE6.Inebriety
@jbinto: Yes, as it treats the valid XHTML way to write a br element as a valid HTML way followed by a buggy HTML way that it tries to "fix", so it treats it as <br><br>.Hepatitis
H
7

@CIRK: HTML4, <br>. XHTML 1.x, <br />. <br/> is also acceptable.

Hirsch answered 26/11, 2010 at 20:53 Comment(0)
U
3

You should use <br /> because it respect the standards XHTML, HTML and in general (XML). Because every xml tag needs to close.

Unwearied answered 26/11, 2010 at 20:54 Comment(2)
HTML doesn't require it to be closed, only XHTML. HTML5 doesn't want it closed.Prudenceprudent
Actually, closing a <br> tag in HTML is incorrect, as HTML (before HTML5) is based off of SGML, which has some odd parsing rules that nobody uses.Restrain
L
2

In HTML (up to version 4) it's <br>And in XHTML it's <br /> because the tag has to be properly closed with an /

HTML5 <br> is the best option.

Lowbred answered 27/11, 2010 at 10:1 Comment(0)
B
1

it is most formal to use <br/> this is then XHTML compliant - all opening tags have corresponding end tag.

Beefwood answered 26/11, 2010 at 20:52 Comment(0)
M
1
<br/>.  

Here:

Some of the most common errors in the usage of XHTML are:
Not closing empty elements (elements without closing tags in HTML4)
Incorrect: <br>
Correct: <br />
Note that any of these is acceptable in XHTML: <br></br>, <br/>, and <br />. Older HTML-only browsers interpreting it as HTML will generally accept <br> and <br />.
Marilumarilyn answered 26/11, 2010 at 20:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.