views:

7995

answers:

6

I've tried checking other answers, but I'm still confused--especially after seeing W3schools HTML 5 reference.

I thought HTML 4.01 was supposed to "allow" single-tags to just be <img> and <br>. Then XHTML came along with <img /> and <br /> (where someone said that the space is there for older browsers).

Now I'm wondering how I'm supposed to format my code when practicing HTML 5.

<!DOCTYPE HTML>

Is it <br>, <br/> or <br />?

+6  A: 

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 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 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.
Michael Krelin - hacker
HTML is not actually XML, just pretty close to it.
tloach
Yes, it's not. But `/` is a common denominator here. HTML5 specifically allows the use of `/`: "Optionally, a "/" character, which may be present only if the element is a void element".
Michael Krelin - hacker
HTML is an actual markup language but XML isn't. You use XML to create languages such as RSS. Am I right? :P
helloworlder
You mean the XML *specification* is similar to the HTML specification?
helloworlder
helloworlder, I'd say that HTML5 specification is close to being XML-conformant.
Michael Krelin - hacker
Ah, okay. Sorry to nitpick
helloworlder
+16  A: 

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

Brian Campbell
"Considered Harmful" essays considered harmful! - http://meyerweb.com/eric/comment/chech.html
Michael Krelin - hacker
But having a valid xml doesn't imply *serving* xhtml, anyway. It may be useful for all kinds of local preprocessing.
Michael Krelin - hacker
Yes, but you have to be very careful processing HTML as XML. They are different languages, and only a subset of each is compatible. For instance, in XML, `<br/>` is the same as `<br></br>`, but the latter is not valid HTML.
Brian Campbell
Brian, the idea was that you *can* have your html be well-formed xml, that's it. Of course when transforming one into another you have to make sure it's still valid, but is it worth mentioning? :)
Michael Krelin - hacker
Oh, of course. It is useful to be able to have documents that are also well-formed XML; I'm not denying that. But it's not necessary, and you need to put more effort in than just self-closing tags to make it valid, and have the same interpretation in XHTML and HTML.
Brian Campbell
Neither I deny it's being unnecessary ;-) And yes, there are more efforts, but those are beyond the scope of present narration ;-)
Michael Krelin - hacker
XHTML is still useful for creating "clean" HTML (properly nested elements, case sensitive, stricter syntax, etc.). The complaints about it being "harmful" are massively overblown IMO, and, contrary to the "Considered Harmful"'s recommendation, I'd use XHTML over the unfinished HTML5 spec any day (well, until the HTML 5 spec is finalised and supported anyway).
Django Reinhardt
+1 for great answer, IMHO, XHTML is useful only to make coders crazy finding performance-inefficient ways around the great and fast Javascript document.write and innerHTML functions that can not be used anymore in XHTML.
Marco Demajo
+2  A: 

According to the spec the expected form is <br> for HTML 5.

tvanfosson
+14  A: 

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

Some elements, however, are forbidden from containing any content at all. These are known as void elements. In HTML, the above syntax cannot be used for void elements. For such elements, the end tag must be omitted because the element is automatically closed by the parser. Such elements include, among others, br, hr, link and meta

HTML Example:

<link type="text/css" rel="stylesheet" href="style.css">

In XHTML, the XML syntactic requirements dictate that this must be made explicit using either an explicit end tag, as above, or the empty element syntax. This is achieved by inserting a slash at the end of the start tag immediately before the right angle bracket.

Example:

<link type="text/css" href="style.css"/>

Authors may optionally choose to use this same syntax for void elements in the HTML syntax as well. Some authors also choose to include whitespace before the slash, however this is not necessary. (Using whitespace in that fashion is a convention inherited from the compatibility guidelines in XHTML 1.0, Appendix C.)

Daan
So, the answer is to code preferably without slash and space, but having the slash (with or without the space)--is optional?
Eikern
That's how I read it, yes.
Daan
@Eikem Yes, that's right.
Brian Campbell
I love the way the specification isn't particularly specific (on this point) "meh, do what you want!"
Matt Ellen
Well, yeah. If it doesn’t matter, then it doesn’t matter.
Paul D. Waite
Since it's optional, I like more the `/>` because it is good for the readability.
BrunoLM
A: 

If your interested in comparability I'd stick with <br /> ifnot <br> is fine

MindStalker
<br /> is actually *less* compatible than <br>.
Ms2ger
@ms2ger. How so? What browser, in what mode, does not process <br /> as a line break?
Alohci
+1  A: 

XML requires all tags to have a corresponding closing tag. So there is a special short-hand syntax for tags without inner contents.

HTML5 is not XML, so it should not pose such a requirement. Neither is HTML 4.01.

For instance, in HTML5 specs, all examples with br tag use <br> syntax, not <br/>.

UPD Actually, <br/> is permitted in HTML5. 9.1.2.1, 7.

jetxee