Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there any issues with always self closing empty tags in html?

Tags:

html

browser

xml

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

like image 612
Simon Avatar asked May 08 '10 23:05

Simon


4 Answers

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.

like image 133
Max Shawabkeh Avatar answered Oct 28 '22 13:10

Max Shawabkeh


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

like image 29
Williham Totland Avatar answered Oct 28 '22 13:10

Williham Totland


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

like image 41
Davor Lucic Avatar answered Oct 28 '22 15:10

Davor Lucic


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.

like image 43
Ms2ger Avatar answered Oct 28 '22 15:10

Ms2ger