ChatGPT解决这个技术问题 Extra ChatGPT

What are all the valid self-closing elements in XHTML (as implemented by the major browsers)?

What are all the valid self-closing elements (e.g.
) 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 />.

Doesn't this default the one of the purposes of XHTML? I thought one of the advantages of XHTML was that you could use an XML generator to generate HTML. Why would any XML generator be aware of what tags are allowed to be self-closing? Too weird.
The reason that the "lame", "incorrect" answer was accepted is because it answered the question that kamens was obviously asking. He wanted to know which elements could be self-closed when serving XHTML as text/html without causing rendering issues in browsers. A lot of pages are written in XHTML and served as text/html even though it's technically incorrect. The question could be improved with this clarification, but answering a different question (what happens when you serve as application/xml, or whether singular tags in text/html should have a closing /) isn't helpful in this instance.

G
Glorfindel

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.

For the record, the XHTML spec allows any element to be self-closing by making XHTML an XML application: [emphasis mine]

Empty-element tags may be used for any element which has no content, whether or not it is declared using the keyword EMPTY.

It's also explicitly shown in the XHTML spec:

Empty elements must either have an end tag or the start tag must end with />. For instance,
or



Not correct afaik, because using self-closing versions of <script> or <div> results in different rendering/interpretation.
@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 can you gaurantee that self closing