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:

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.

1 Comment

Can’t you use an ID attribute as an anchor? So rather than <a name="foo"></a> you can do <a href="..." id="foo">. I hate that SCRIPT can’t be empty, it looks so stupid to have a closing tag when you’re including external javascript files.

Care to Comment?

Or if you'd prefer to get in touch privately, please send me an email.


Email (optional)

Blog (optional)