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

前端 未结 13 1610
夕颜
夕颜 2020-11-22 05:16

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

相关标签:
13条回答
  • 2020-11-22 06:02

    You should have a look the xHTML DTDs, they're all listed. Here is a quick review all the main ones:

    <br />
    <hr />
    <img />
    <input />
    
    0 讨论(0)
  • 2020-11-22 06:02

    They're called "void" elements in HTML 5. They're listed in the official W3 spec.

    A void element is an element whose content model never allows it to have contents under any circumstances.

    As of April 2013, they are:

    area, base, br, col, command, embed, hr, img, input, keygen, link, meta, param, source, track, wbr

    As of December 2018 (HTML 5.2), they are:

    area, base, br, col, embed, hr, img, input, link, meta, param, source, track, wbr

    0 讨论(0)
  • 2020-11-22 06:02

    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:

    <title/>

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

    <script/>

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

    0 讨论(0)
  • 2020-11-22 06:03

    Better question would be: what tags can be self-closed even in HTML mode without affecting code? Answer: only those that have empty content (are void). According to HTML specs the following elements are void:

    area, base, br, col, embed, hr, img, input, keygen, link, menuitem, meta, param, source, track, wbr

    Older version of specification also listed command. Besides, according to various sources the following obsolete or non-standard tags are void:

    basefont, bgsound, frame, isindex

    0 讨论(0)
  • 2020-11-22 06:12

    Hope this helps someone:

    <base />
    <basefont />
    <frame />
    <link />
    <meta />
    
    <area />
    <br />
    <col />
    <hr />
    <img />
    <input />
    <param />
    
    0 讨论(0)
  • 2020-11-22 06:14

    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!

    0 讨论(0)
提交回复
热议问题