search

Home  >  Q&A  >  body text

Does HTML5 allow non-empty self-closing tags?

<p>The W3C validator (Wikipedia) does not like the use of self-closing tags (tags ending in "<code>/></code>") on non-empty elements. (Empty elements are those that never contain any content.) Are they still valid in HTML5? </p> <p>Some examples of accepted empty elements: </p> <pre class="brush:html;toolbar:false;"><br /> <img src="" /> <input type="text" name="username" /> </pre> <p>Some examples of rejected non-empty elements: </p> <pre class="brush:html;toolbar:false;"><div id="myDiv" /> <span id="mySpan" /> <textarea id="someTextMessage" /> </pre> <sub> <b>Note: </b> <p><br /></p><p> The W3C validator actually accepts empty self-closing tags: the author initially encountered the problem because of a simple typo (using <code>></code> instead of <code>/></code> ;); However, self-closing tags are not fully valid in HTML5, and the answer elaborates on the issue of self-closing tags in different HTML versions. </p></sub>
P粉680487967P粉680487967533 days ago655

reply all(2)I'll reply

  • P粉762730205

    P粉7627302052023-08-22 12:17:28

    Self-closing divs will not be validated. This is because div is a normal element, not an empty element.

    According to the HTML5 specification, tags that cannot contain any content (called empty elements) can be self-closing*. This includes the following tags:

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

    The "/" on the above tag is completely optional, so <img/> is no different from <img>, but <img> ;</img> is invalid.

    *Note: External elements can also be self-closing, but I think that's outside the scope of this answer.

    reply
    0
  • P粉113938880

    P粉1139388802023-08-22 12:03:24

  • Cancelreply