Recently I had a job interview and one of the questions was “Is doctype needed for HTML 5 documents?”. I’ve answered “no” but have a feeling that I might be wrong. From the w3 it looks like it is absolutely required, but if I type a simple HTML like
<html>
<body>
<input type="color" disabled/>
</body>
</html>
save it as HTML and try to open in Chrome – it would work perfectly well, with a new color input (disabled). That input is HTML5 as well as attribute.
So the question is – do I need to specify doctype or not? What would be the correct answer for the interview?
3
The correct answer would be “It depends”.
A DOCTYPE is optional for HTML5 documents in the XHTML syntax (i.e., XHTML5 documents):
XML documents may contain a DOCTYPE if desired, but this is not required to conform to this specification.
A DOCTYPE is required for HTML5 documents in the HTML syntax:
Documents must consist of the following parts, in the given order:
[…]
3. A DOCTYPE.
Exceptions
-
HTML5 documents in the
srcdoc
attribute of aniframe
element don’t need a DOCTYPE:2. Optionally, a DOCTYPE.
3
I, too, would have answer “no” (wrongly, as it happens).
However, http://www.w3.org/TR/html5/syntax.html#the-doctype says :
8.1.1 The DOCTYPE
A DOCTYPE is a required preamble.
DOCTYPEs are required for legacy reasons. When omitted, browsers tend
to use a different rendering mode that is incompatible with some
specifications. Including the DOCTYPE in a document ensures that the
browser makes a best-effort attempt at following the relevant
specifications.
BTW, G.I.Y.F
4
When you want to write a w3c standard-conformant HTML5 document, then <!DOCTYPE html>
is required.
But in practice you don’t always need a doctype because any common web browser will render your document regardless. However, some browsers will then use a legacy mode in which some features are not interpreted correctly and instead in a way an earlier version of the browser handled it (most of them layout-related).
Which answer is right for this interview? That depends on the mentality of the person who interviewed you. In a job interview it doesn’t matter if you are right or wrong. It only matters what impression you make on the interviewer.
2