Are there any browser issues with always collapsing empty tags in html. So for example an empty head tag can be written like this
<head></head>
but is can also be written like this
<head/>
Will the second case cause issues in any scenerio?
Thanks
Self-closing <script>
tags can mess up some browsers really badly. I remember my whole page disappearing into thin air in IE after I self-closed a script tag - everything after it was read as a script.
Assuming that you are serving your XHTML as XML, no. <head></head>
is entirely equivalent to <head />
. In fact, an XML parser won't even bother to tell you which one you have.
(There is, however, an issue in that the <head>
tag must contain a <title>
.)
You shouldn't use minimized form for head in XHTML.
http://www.w3.org/TR/xhtml1/#guidelines
About empty elements:
http://www.w3.org/TR/xhtml1/#C_3
Given an empty instance of an element whose content model is not EMPTY (for example, an empty title or paragraph) do not use the minimized form (e.g. use
<p> </p>
and not<p />
).
In other words, paragraph should always be closed in XHTML, in HTML you could go with only opening tag. But if the element is supposed to have content it should be properly opened and closed.
For example line break has EMPTY content model and can be written as <br />
(same goes for <hr />
) but not <div />
.
Also see this SO question.
Empty Elements (XHTML)
Shorthand markup in HTML
Self-closing tags don't exist in HTML. The /
is always ignored, that is, <foo/>
and <foo>
are equivalent. For elements such as br
, that's fine, because you want <br>
. However, <script src="..." />
means the same as <script src="...">
, which is a problem (as noted in other answers). <head/>
is less of a problem, because the </head>
end tag is optional anyway.
In XML, on the other hand, self-closing tags do what you want. However, you probably aren't using XML, even if you've got an XHTML doctype. Unless you send your documents with a text/xml
, application/xml
or application/xhtml+xml
MIME type (or any other XML MIME type), particularly if you send them as text/html
, they will not be treated as XML.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With