Mink Machine

The closure of XHTML elements

If you have followed the crowd and abandoned HTML for XHTML, you may have noticed that some elements need to be closed in your markup. The reason is that XHTML is simply a reformulation of HTML in XML syntax. It’s a subtle but very significant difference between HTML and XHTML.

The MIME type of a document is the only thing that controls whether it is HTML or XHTML. Since browsers don’t fully support XML mode, full XHTML processing is not an option. This leads to markup being served as “text/html” whether you call it XHTML or not.

Unlike CSS, there is no shorthand notion in HTML. Consider the following code:

<img src="ferret.jpg" alt="ferret" />

The slash at the end closes the element. But the following is not valid:

<script src="coolstuff.js" type="text/javascript" />

Neither is this:

<div id="primary" />

The self-closing slash character is interpreted as an attribute in the markup and ignored (since it’s not a legal attribute name). This means that the self-closed div actually becomes an opening tag without a closing tag.

So why does the self-closed img element work? It is because the HTML DTD states:

<!ELEMENT IMG – O EMPTY — Embedded image –>

As you can see, it allows for empty content. On the other hand, div elements are not allowed to have empty content.

This is just the tip of the iceberg. For more information, have a look at the infamous Appendix C of the XHTML 1.0 standard.

Comments

No comments yet.

Post a comment

Your email address will not be published. Required fields are marked *

Featured stories

Good morning Montenegro

"One would almost think that all this secrecy was made on purpose to keep the hordes of tourists away. Let them all perish in the crowds of Dubrovnik while the people of Montenegro can enjoy their beautiful country on their own. Or something like that."

I still love you, New York

"Now I’m back and our roles are somewhat reversed. Like River Song and the Doctor, we meet again under different circumstances."

Greetings from Moldova

"But Chisinau is an unexpected gem in the wilderness. Even though Moldova is one of the poorest countries in Europe, the capital has its fair share of impressive buildings and post-Soviet architecture."

The dark days of Sarajevo

"The city survived on the edge of annihilation and has started to recover, but the stone walls still seem to cry out in lament of all the horrible things they have seen."

Bunker safari

"The area contains 54 excavated bunkers along with gun emplacements, scattered around the town’s most visible landmark."

Aliens in Nevada and New Mexico

"We turned around and went down another dirt road, past countless Joshua trees until our car was covered in dirt and we finally arrived at the Front Gate of Area 51."