This is one of those things that you read once, say "aha!" and then forget. Exactly my case.
Why is the line-break tag in xhtml preferentially written with a space <br />
and not in the also ok format <br/>
? I remember the reason was interesting, and as you can imagine it's not easy to find with google.
For sure it's not an issue of xml well-formedness. From W3C
[44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>'
Empty-element tags may be used for any element which has no content, whether
or not it is declared using the keyword EMPTY. For interoperability, the
empty-element tag should be used, and should only be used, for elements which
are declared EMPTY.
Examples of empty elements:
<IMG align="left" src="http://www.w3.org/Icons/WWW/w3c_home" />
<br></br>
<br/>
So the space at the end is optional.
Both are correct. But I would use
<br />
just to keep my code consistent... because I would never writeinstead of
just to save a byte... and the second version is imho better readable. But that's just a matter of taste. Do it as you like, but do it consistent :-)
w3c specifies this as the grammar:
That means open bracket, a name, a number of (space and attribute) tokens, an optional space, a slash, and an end tag. According to this, both are correct.
<br />
is valid (old) HTML, while<br/>
is not. If you are serving your XHTML as XML, it doesn't matter. If you are serving it astext/html
, then it needs to be valid HTML in addition to being valid XHTML. (Why serve XHTML as HTML? Because IE doesn't understand XHTML as XML, and because no major browser will start rendering XHTML mid-way through downloading the text, but they will do that to HTML. My blog appears to load slowly not because the site is slow, but because the browser won't start rendering the page until everything has been fetched. I hate browsers.)<br>
. You aren't using XML anyway.Both
<br/>
and<br />
are correct. The reason that<br />
came about in the first place was to support older browsers that didn't understand the new<br/>
syntax. It's really kind of a hack where the/
is interpreted as an attribute with no value and ignored.Either will work just fine. Assuming you are asking for evangelical reasons, I prefer <br/>