Something funny about self-closing tags
Do you know the reason why we are so accustomed to writing self-closing tags as <Something />
and not <Something/>
, with the space in between? Is it just a style preference or is there something more? Well, it's an interesting story…
Back in the early days of web development, a new markup language was trying to establish itself as the standard for structuring documents. The old HTML lazy syntax was just an SGML application while this new standard was based on the rigid rules of XML: it was XHTML.
Now, one issue with XHTML (one of the oh-so-many) was that you had to tell the browser to use the XML parser to read it, not just the HTML one. That was easier said than done, because it wasn't just a matter of using the application/xhtml+xml
content type, since some browsers didn't know what to do with that, yet.
One of those browsers was Internet Explorer. The trick was then to serve those browsers the XHTML document relying on the fact that any browser would have been able to render HTML no matter how bad it was formatted, even if it was, indeed, XML.
The problem was the self-closing tags (mandatory in XML/XHTML), because <input/>
was not something that XML-deprived browsers could understand (it would be parsed as "input/"). The solution was then to always put a space between the tag name and the final slash, as in <br />
.
Internet Explorer would then consider the "/" as an unknown attribute, ignore it altogether and peacefully render the "br" element.
I have always found this story amusing, and we all know today that that space is just a relic of the past… Internet Explorer will hunt us until the end of the days!