WWW FAQs: Why do some elements end with /> in XHTML pages?

2004-05-04: Every XHTML element has to be closed so that the parser doesn't have to have special knowledge of each element in order to properly parse it. For instance, before XHTML, a naive parser might have thought that:


Meant that a br "block" had been opened, and that parser might wait around for a closing:


Which would never appear.

Another good example of an "empty" element that needs a trailing slash to be XHTML compliant is the <img /> element. Elements that do have a closing element, such as <body>...</body>, should not have a / before the >.

Legal Note: yes, you may use sample HTML, Javascript, PHP and other code presented above in your own projects. You may not reproduce large portions of the text of the article without our express permission.

Got a LiveJournal account? Keep up with the latest articles in this FAQ by adding our syndicated feed to your friends list!