Does HTML5 allow non-empty self-closing tags?
P粉680487967
P粉680487967 2023-08-21 18:20:11
0
2
611
<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粉680487967
P粉680487967

reply all(2)
P粉762730205

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&gt ;</img> is invalid.

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

P粉113938880
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template