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.
P粉1139388802023-08-22 12:03:24
Theoretically, in HTML 4, <foo /
(yes, without any >
) means <foo>
(which results in <br />
meaning <br>>
(i.e. <br>>
) and <title/hello/
means <title>hello</title>
). I use the term "theoretically" because this is a SGML rule that browsers support very poorly. Support is so sparse (I've only seen it work in emacs-w3m) that the specification advises authors to avoid using this syntax .
In XHTML, <foo />
means <foo></foo>
. This is the XML rule that applies to all XML documents. That said, XHTML is typically served as text/html
, which (at least historically) is used by browsers using a different parser than documents served as application/xhtml xml
deal with. The W3C provides compatibility guidelines
for XHTML as text/html. (Basically: use self-closing tag syntax only if the element is defined as EMPTY (and closing tags are prohibited in the HTML specification)).
In HTML5, the meaning of <foo />
depends on the type of element :