115

I've frequently seen a space preceding the closing slash in XML and HTML tags. The XHTML line break is probably the canonical example:

<br />

instead of:

<br/>

The space seems superfluous. In fact, I think that it is superfluous.

What is the reason for writing this space?

I've read that the space solves some "backwards compatibility issues." Which backwards compatibility issues? Are those issues still relevant, or are we still adding extra spaces for the sake of, say, IE3 compatibility? Does there exist some spec with the definitive answer on this?

If not backwards compatibility, then is it a readability issue? Similar to the Great Open Curly Brace debate?

void it_goes_up_here() {

int no_you_fool_it_goes_down_there()
{

I can certainly respect differing stylistic opinions, so I'll be happy to learn that writing the space is simply a matter of taste.

Greg Mattes
  • 33,090
  • 15
  • 73
  • 105

8 Answers8

73

The answer is people wish to adhere to Appendix C of the XHTML1.0 specification. Which you only need to do if you are serving XHTML as text/html. Which most people do, because XHTML's real MIME type (application/html+xml) does not work in Internet Explorer.

No current browser cares for the space. Browsers are very tolerant of these things.

The space used to be required to ensure HTML parsers treated the trailing slash as an unrecognised attribute.

Lee Kowalkowski
  • 11,591
  • 3
  • 40
  • 46
  • 2
    Can you be more specific about "used to be?" Year and/or browser version will do, thanks! – Greg Mattes Jan 20 '09 at 20:45
  • 5
    I think that http://www.w3.org/TR/xhtml1/#C_2 is a more precise link for this answer. So, it seems that XHTML 1.0 Appendix C2 is effectively obsolete and that writing the space is purely a matter taste. – Greg Mattes Jan 24 '09 at 21:01
  • 1
    Sorry, used-to-be, means "is" - with respect to making sure a HTML parser treats the trailing slash as an unrecognised attribute, not all HTML parsers are browsers. I wouldn't like to hazard a guess which browser version choked if any, but I don't remember IE4 or Netscape 4 complaining. – Lee Kowalkowski Feb 16 '09 at 22:13
  • 4
    actually the real mime type is application/xhtml+xml. – mk12 Oct 31 '09 at 03:34
  • 1
    Oh yes, didn't look it up and I never use it, because IE still chokes! – Lee Kowalkowski Nov 11 '09 at 23:21
  • 1
    It seems it was Netscape 4 that needed the space: http://stackoverflow.com/a/305466/37147 – Jan Aagaard Nov 08 '12 at 21:46
  • 3
    @JanAagaard: I dunno, I'd remember it if so - I started web development on IE4 and Netscape 4. That answer you linked to has a comment to this effect too, it says it's actually Netscape 3. – Lee Kowalkowski Nov 09 '12 at 08:56
  • 1
    @LeeKowalkowski Actually the appendix C is marked as *informative* hence non-normative. But that's purely a cosmetic question nowadays. – Édouard Lopez Nov 08 '13 at 23:19
  • @EdouardLopez: you're right, but your average HTML author reading the specification isn't going to spot that, or understand the relevance. – Lee Kowalkowski Nov 09 '13 at 23:13
39

Netscape 4.80 showing different behaviour of <br/> and <br /> in HTML

Supporting bobince's answer with screenshot of Netscape 4.80 showing documents

data:text/html,<title>space</title>foo<br />bar

(top left, linebreak rendered) and

data:text/html,<title>no space</title>foo<br/>bar

(bottom left, linebreak ignored).


Posting as answer to show the picture

Tangentially related: in fact I had a lengthy answer identifying the cause of such misbehaviour of ancient browsers (and resulting recommendation to include space) in misunderstood SGML specs, namely SGML Null End Tag (NET) (where 1<tag/2/3 equals 1<tag>2</tag>3 so 1<tag/>2 would actually mean 1<tag>>2), but not only I was unable to find good proof and concrete version of standard, I wasn't even able to grasp proper standard-complying behaviour. So few raw links for reference:

(Unable to reproduce there now, but supports Lee Kowalkowski's statement about multiple browsers affected by this.)

myf
  • 9,874
  • 2
  • 37
  • 49
27

Are those issues still relevant or are we still adding extra spaces for the sake of, say, IE3 compatibility?

You were close - it is for Netscape 4.

It is interesting to see other rationalisations, but that's all it was meant for.

bobince
  • 528,062
  • 107
  • 651
  • 834
  • 2
    Thanks! Can you provide a reference for this? – Greg Mattes Jan 21 '09 at 15:28
  • 1
    Hmm, difficult to find primary sources this old... official W3 materials avoid mentioning any one UA, and discussion in the lists seems to take the situation as read. There probably were other UAs that also needed the space, but N4 was the last one, that caused webmasters trouble for years. – bobince Jan 21 '09 at 16:29
  • It was so your XHTML document would also render on Netscape. Specifically it was for break tags and image tags. Primary Source: I was coding for IE4 and NS3 compatibility 10 years ago. – Philihp Busby Feb 24 '15 at 22:58
5

No, the space is not required but it is necessary for some older browsers to render those tags correctly. The proper way to do it is without the extra space as this is something XHTML inherited from XML.

Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
3

In XHTML, br tags must be closed, but the space is not necessary. It's a stylistic thing. In HTML, br tags cannot be closed, so both are wrong.

Pesto
  • 23,810
  • 2
  • 71
  • 76
2

The space just makes the tags more readable. I am a big proponent of formatting for more readable code. Little things like that go a long way. Without the space the closing tag blends in with the opening tag. It takes just an instant longer for me to process it as I am quickly reading the code.

Jim Petkus
  • 4,500
  • 25
  • 19
0

What if there was a very lazy html writer out there or maybe he had a fear of quotation marks. Consider the following if you were his robot page crawler...

<img src=http://myunquotedurl.com/image.jpg />

versus

<img src=http://myunquotedurl.com/image.jpg/>

This might seem small but look what it can do if the space isn't there. The robot won't know if the slash is part of the url or part of the closing tag.

Jim True
  • 1,085
  • 12
  • 13
0

I think that the white space is a way to reinforce the idea that this tag is empty and it closes itself.

Today i don't use the white space anymore because i never had a problem with no white space.

nicruo
  • 508
  • 4
  • 13