View Full Version : Closing html properly

08-01-2012, 12:52 AM
Hello everyone,
In this situation, is there any reason to actually do the latter apart from modern-day standards


compared to

<br />

same for img, hr, etc.

08-01-2012, 12:58 AM
No reason other than conforming to w3c standards for XHTML, it's just closing tags, for neatness.

08-01-2012, 03:46 AM
In XHTML, yes. In [other] HTML, no.

In HTML, tags that go around content require a preceding 'open' tag and a following 'close' tag, while tags that are the content (for example, an image) have a single tag, which doesn't need a separate close tag.

In XHTML, the idea was simply to merge the open+closer tags so that it is <img /> rather than <img> without an </img> anywhere.

08-01-2012, 04:28 AM
The situation is a little more complex. Unless your XHTML page is actually being served as application/xml, it will be error corrected to HTML by the browser.

It likely isn't being served as application/xml, because if it were, it wouldn't load in IE 8 and less.

You really shouldn't use XHTML unless you are serving application/xml. If you do, you need a valid XML prologue. And as I say the page won't load in IE 8 and less.

Use HTML 4.01 or HTML 5. In 4.01, self closing tags are not allowed, in 5 they are, but are not required.

About the worst thing you can do in either HTML or XHTML is to self close a tag that requires a closing tag. The iframe tag is a good example. But any tag requiring a closing tag can act oddly if you try to get away with self closing it rather than using its required closing tag. At the same time you need to be aware that, for scripting purposes, some browser will erroneously self close some tags. This can throw off your element count (say if you're using getElementsByTagName('*') or childNodes()) in those browsers even when your code is valid.

08-01-2012, 06:52 AM
In XHTML, self closing a tag that requires a closing tag yields an error anyway, so it's within the XHTML standards to do that. I personally maintain XHTML standards on regular HTML pages, for the sake of keeping up readability and coding habits.

08-01-2012, 03:14 PM
Self closing tags on HTML pages are invalid unless it's HTML 5. But just as browsers error correct most "XHTML" to HTML, they will usually do the same with HTML written as XHTML. But there could be exceptions.

It's generally best to follow the standards of the DOCTYPE you're using.