views:

172

answers:

8

Are there any browser issues with always collapsing empty tags in html. So for example and empty head 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

+7  A: 

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.

Max Shawabkeh
Argh. Cross post. +1 for the same answer!
BradBrening
A: 

Not that I am aware of. One caveat that has bitten me in the past is self closing my script tag: <script type="text/javascript" src="somefile.js" />

This results in some interesting fail.

BradBrening
A: 

I believe some older browsers had problems with the lack of whitespacing - in particular <head/> would be interpreted as a "head/" tag, whereas <head /> will be interpreted as a "head" tag with a blank attribute "/" which is ignored.

This only affects a few browsers, AFAIK. Either is valid XHTML, but older HTML-only browsers might have trouble.

This is in fact documented in the XHTML guidelines as C.2

Steven Schlansker
...Mind explaining why the downvote?
Steven Schlansker
Some reference to your statement would have been nice, but I think I can recall the same behavior as you describe. +1 since I don't think this post should have negative score.
MyGGaN
While the spec that I cited doesn't *explicitly* say this, it does mention the interpretation of / as an attribute which is enough to reconstruct the rest of the explanation, I think. Wish I could find a more concrete reference... :)
Steven Schlansker
+3  A: 

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>.)

Williham Totland
A: 

In general an empty element can be written as a self closing tag, or opening and closing tags.

However, the HTML4 DTD specifies that the document HEAD must contain a TITLE element.

"Every HTML document must have a TITLE element in the HEAD section."

http://www.w3.org/TR/1999/REC-html401-19991224/struct/global.html#h-7.4.1

Peter
+2  A: 

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

rebus
A: 

Even considering only browser issues (i.e. disregarding validity) and narrowing the question down to the head tag alone, the answer is still yes.

Compare

<head/>
<object>Does this display?</object>

against

<head></head>
<object>Does this display?</object>

each served as text/html to any version of IE.

Does this display? will be shown only in the latter example.

Alohci
+1  A: 

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.

Ms2ger