23

For example: <div/> instead of <div></div>. I did this and apparently the HTML5 validator passed this as valid. I was wondering it this is actually true?

PS: I'm serving page as application/xhtml+xml

Josh Lee
  • 171,072
  • 38
  • 269
  • 275
user434366
  • 539
  • 7
  • 15

3 Answers3

25

This is not valid HTML 5 (HTML does not allow shorttags, the equivalent HTML construct is a single opening div tag). It is valid XHTML 5, as it is valid XML.

The reason why you might see this pass through a validator just fine is because of what you stated:

PS: I'm serving page as application/xhtml+xml

Which means that you tell the validator that it must treat your markup as XML. In other words your page is not HTML 5 at all.

user268396
  • 11,576
  • 2
  • 31
  • 26
  • 5
    +1. By default, no elements support the self-closing syntax. Elements which have no content model / empty content model *can* be self-closed for XHTML compliance, but the browser treats them the same either way. For example, `` and `` are the same. – Rex M Oct 04 '10 at 02:42
  • In cases like this, please try to distinguish between HTML5 the spec, and the HTML (text/html) and XHTML (application/xhtml+xml) serializations contained within. Although this answer is essentially correct until the last sentence, the XHTML page is indeed HTML5. – Alohci Oct 04 '10 at 08:19
17

That syntax is allowed for a specific subset of HTML5 elements, known as void elements, and a few other cases:

Then, if the element is one of the void elements, or if the element is a foreign element, then there may be a single U+002F SOLIDUS character (/). This character has no effect on void elements, but on foreign elements it marks the start tag as self-closing.

Void elements:

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

They're not allowed for any others, including <div>.

(I'd originally answered that yes, this is valid HTML5, since it's such a common construct in XML. Rex M, and a close reading of the spec, tells me that I'm wrong)

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
0

No, technically using a self-closing <div/> tag is invalid HTML5. Using <div></div> works fine.

Kevin Ji
  • 10,479
  • 4
  • 40
  • 63