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

Roaming the cobblestones of Istanbul

Roaming the cobblestones of Istanbul

"Inside the church there are still traces of rune inscriptions made during the Viking age by Varangians, an elite guard made up of Scandinavian immigrant warriors."
The lakes of Lombardy

The lakes of Lombardy

"The town of Como, largest settlement around Lake Como, attracts a lot of people with big wallets. But I preferred to take the funicular up the mountain to the Brunate region, where you get a much better view of the lake."
A journey through Iran

A journey through Iran

"I woke up freezing on a Persian rug with aching back. Behind a corner I saw the damned rooster that kept me awake during many hours."
Alone in Kyoto

Alone in Kyoto

"I tried my best to sneak across the building, but the floor revealed me each time. I suppose I would make a lousy ninja."
Exploring Chernobyl

Exploring Chernobyl

"The soundtrack of the day was Radioactivity by Kraftwerk, as we rolled into Chernobyl this morning."
Chilling in Chile

Chilling in Chile

"My weary feet has reached Valparaiso. After almost a month of traveling across the South American continent I realize that this is as far south as I will come, due to weather conditions."

Instagram

Seville
Venice
Gothenburg
Fever Ray
Rome
Basel
Singapore
Kuala Lumpur