There are only 10 legal XHTML empty tags

I’ve always had this nagging question. Is <a name="target"/> legal XHTML? I mean, it seems like kind of waste to have to do <a name="target"></a> all the time. Same thing with <script src="file.js"></script>, but I wasn’t sure if I could just arbitrarily use the empty element shorthand.

Turns out I can’t. There’s limited list of tags in the XHTML 1.0 DTD that can use the empty element shorthand:

All elements other than those declared in the DTD as EMPTY must have an end tag. Elements that are declared in the DTD as EMPTY can have an end tag or can use empty element shorthand.

A quick search for “empty” on the XHTML 1.0 DTD returns the following list of tags that can legally use the empty element shorthand:

  • <area/>
  • <base/>
  • <br/>
  • <col/>
  • <hr/>
  • <img/>
  • <input/>
  • <link/>
  • <meta/>
  • <param/>

Note: <a/> and <script/> are not on that list.

This post first appeared on From the Belly of the Beasts, a weblog from some of the people who build O’Reilly websites.