views:

10560

answers:

13

What are all the valid self-closing tags (e.g. <br/>) in XHTML (as implemented by the major browsers)?

I know that XHTML technically allows any tag to be self-closed, but I'm looking for a list of those tags supported by all major browsers. See http://dusan.fora.si/blog/self-closing-tags for examples of some problems caused by self-closing tags such as <div />.

A: 

<hr /> is another

Darren Kopp
A: 

Check w3 reference.

Oko
w3schools is not affiliated with W3C and is not authoritative on the matter.
porneL
@porneL but their SEO is so damn good. W3C could learn a thing or two
Triptych
+1  A: 

You should have a look the xHTML DTD (http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd), there all listed. Here is a quick review all the main ones :

<br />
<hr />
<img />
<input />
e-satis
+28  A: 

From the W3 Schools reference site:

<area />
<base />
<basefont />
<br />
<hr />
<input />
<img />
<link />
<meta />
ConroyP
http://www.w3schools.com/tags/default.asp I see 12 tags ending with `/>` : `"area", "base", "basefont", "br", "col", "frame", "hr", "img", "input", "link", "meta", "param"`
Mark
+1  A: 

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!

AmbroseChapel
+10  A: 

One tag to be very careful with on this topic is the <script> tag. If you have an external source file, it WILL cause problems when you self close it. Try it:

<script type="text/javascript" src="external.js" />

This will work in Firefox, but breaks in IE6 at least. I know, because I ran into this when overzealously self closing every tag I saw ;-)

Erik van Brakel
This has caught me so many times
John Sheehan
Affects all versions of MSIE:http://webbugtrack.blogspot.com/2007/08/bug-153-self-closing-script-tag-issues.html
scunliffe
+1 for this pernicious evil. Why can't they get this right?
erickson
<script> doesn’t self-close in Firefox 3.
hsivonen
Definitely wasted a few days in my career tracking this bug down.
Triptych
This won't work in any browser.
Ms2ger
Well, it used to work in Firefox when I encountered it. Seems like it doesn't work in any browser anymore. Could also only work in quirks mode perhaps?
Erik van Brakel
@erickson it works fine in Firefox if *you* will get your MIME type right.
porneL
A: 

Another self closing tag problem for IE is the title element. When IE (just tried it in IE7) sees this, it presents the user a blank page. However you "view source" and everything is there.

<title/>

I originally saw this when my XSLT generated the self closing tag.

Kevin Hakanson
+21  A: 

Every browser that supports XHTML (Firefox, Opera, Safari, IE9) supports self-closing syntax on every element.

<div/>, <script/>, <br></br> all should work just fine. If they don't, then you have HTML with inappropriately added XHTML DOCTYPE.

DOCTYPE does not change how document is interpreted. Only MIME type does.

W3C decision about ignoring DOCTYPE:

The HTML WG has discussed this issue: the intention was to allow old (HTML-only) browsers to accept XHTML 1.0 documents by following the guidelines, and serving them as text/html. Therefore, documents served as text/html should be treated as HTML and not as XHTML.

It's a very common pitfall, because W3C Validator largely ignores that rule, but browsers follow it religiously. Read Understanding HTML, XML and XHTML from WebKit blog:

In fact, the vast majority of supposedly XHTML documents on the internet are served as text/html. Which means they are not XHTML at all, but actually invalid HTML that’s getting by on the error handling of HTML parsers. All those “Valid XHTML 1.0!” links on the web are really saying “Invalid HTML 4.01!”.


To test whether you have real XHTML or invalid HTML with XHTML's DOCTYPE, put this in your document:

<span style="color:green"><span style="color:red"/> 
 If it's red, it's HTML. Green is XHTML.
</span>

It validates, and in real XHTML it works perfectly (see: 1 vs 2). If you can't believe your eyes (or don't know how to set MIME types), open your page via XHTML proxy.

Another way to check is view source in Firefox. It will highlight slashes in red when they're invalid.

In HTML5/XHTML5 this hasn't changed, and the distinction is even clearer, because you don't even have additional DOCTYPE. Content-Type is the king.

porneL
Not correct afaik, because using self-closing versions of `<script>` or `<div>` results in different rendering/interpretation.
ZeissS
@ZeissS *only* in `text/html`. In real XHTML, sent as `application/xhtml+xml` it's works just fine. Please read article I linked to (or XHTML spec Appendix C) before downvoting.
porneL
+7  A: 

The self-closing syntax works on all elements in application/xhtml+xml. It isn’t supported on any element in text/html, but the elements that are “empty” in HTML4 or “void” in HTML5 don’t take an end tag anyway, so if you put a slash on those it appears as though the self-closing syntax were supported.

hsivonen
+2  A: 

The last time I checked, the following were the empty/void elements listed in HTML5.

Valid for authors: area, base, br, col, command, embed, eventsource, hr, img, input, link, meta, param, source

Invalid for authors: basefont, bgsound, frame, spacer, wbr

Besides the few that are new in HTML5, that should give you an idea of ones that might be supported when serving XHTML as text/html. (Just test them by examining the DOM produced.)

As for XHTML served as application/xhtml+xml (which makes it XML), XML rules apply and any element can be empty (even though the XHTML DTD can't express this).

Shadow2531
+1  A: 

Hope this helps someone:

<base />
<basefont />
<frame />
<link />
<meta />

<area />
<br />
<col />
<hr />
<img />
<input />
<param />
Jeff
A: 

use < img //> if you are working with blogger templates or site feed integration

Muhammad
A: 

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:

For this, I have simply resorted to always giving it a separate closing tag, since once it's up there in the it doesn't really make your code any messier to work with anyway.

This is the big one that I very recently ran into problems with. For years I had always used self-closing 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 tag because it didn't realize this was the end of the tag. So when I made it into separate 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!

Nathan Sokalski