HTML5 void elements' ending / is optional, but is it preferable?

524 views Asked by At

HTML5 allows either <br> or <br/>. Does the W3C specification or any other authority state whether one is preferable over the other? Is there a good reason (or reasons) to use one over the other if adherence to XHTML is irrelevant?

2

There are 2 answers

3
BTC On BEST ANSWER

In HTML5 compliant browsers, this makes no difference. I think you would have difficulty citing any resource which preferred <br /> over <br>, slash-trailing image tags, or even closing <path> elements. It is most likely preferable not to have the trailing slash simply because the document takes just that much longer to fetch from the webserver and for the HTML interpreter to serialize (as minuscule a time differential as this actually provides). In addition, all HTML5 compliant browsers strip these characters in runtime, which is evident when you call outerHTML on an HtmlElement.

EDIT: I also think that if there was any difference or adherance to a standard to preserve, it would be reflected on the w3c page: https://www.w3.org/wiki/HTML/Elements/br

However, I feel as though it is worth mentioning that browsers that are not compliant with HTML5 standards (mostly earlier, unsupported versions of IE) demand a trailing slash to render the element.

8
DMaster On

HTML is not XML.

In HTML, / is nonsense, for instance:

<script type="text/javascript" />
//This line is in Javascript, not HTML
alert('Hello');
</script>

I don't think / is useful at all