What are all the valid self-closing elements (e.g. <br/>) in XHTML (as implemented by the major browsers)?
I know that XHTML technically allows any element to be self-closed, but I'm looking for a list of those elements supported by all major browsers. See http://dusan.fora.si/blog/self-closing-tags for examples of some problems caused by self-closing elements such as <div />.
From the W3 Schools reference site:
What about
<meta>
and<link>
? Why aren't they on that list?Quick rule of thumb, do not self-close any element which is intended to have content, because it will definitely cause browser problems sooner or later.
The ones which are naturally self-closing, like
<br>
and<img>
, should be obvious. The ones which aren't ... just don't self-close them!<hr /> is another
Better question would be: what tags can be self-closed even in HTML mode without affecting code? Answer: only those that have empty content (are void). According to HTML specs the following elements are void:
area, base, br, col, embed, hr, img, input, keygen, link, menuitem, meta, param, source, track, wbr
Older version of specification also listed
command
. Besides, according to various sources the following obsolete or non-standard tags are void:basefont, bgsound, frame, isindex
I'm not going to try to overelaborate on this, especially since the majority of pages that I write are either generated or the tag does have content. The only two that have ever given me trouble when making them self-closing are:
<title/>
For this, I have simply resorted to always giving it a separate closing tag, since once it's up there in the
<head></head>
it doesn't really make your code any messier to work with anyway.<script/>
This is the big one that I very recently ran into problems with. For years I had always used self-closing
<script/>
tags when the script is coming from an external source. But I very recently started recieving JavaScript error messages about a null form. After several days of research, I found that the problem was (supposedly) that the browser was never getting to the<form>
tag because it didn't realize this was the end of the<script/>
tag. So when I made it into separate<script></script>
tags, everything worked. Why different in different pages I made on the same browser, I don't know, but it was a big relief to find the solution!Hope this helps someone: