10

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?

  • 1
    I guess you could argue that without the Doctype element it isn't an HTML5 document. – MetaFight Feb 5 '15 at 10:11
  • You can type hello world in a text editor, save it as HTML, and it will render in Chrome. Turns out Chrome (and many other browsers) put stuff in for you, even if it's "required." – intcreator Nov 6 '18 at 19:32
11

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

  • 1
    This is the complete answer if “is needed” is interpreted as “is required by specifications”. It could also be interpreted as referring to practical effects. In that case, the answer would be that a doctype string is needed in HTML syntax to put browsers to “standards mode”. – Jukka K. Korpela Feb 5 '15 at 12:12
  • I would like this answer better if you elaborated as to when a document is html vs xhtml. What does this mean to the browsers -- how do they decided which syntax to evaluate with? – temporary_user_name Oct 30 '18 at 12:56
6

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

  • 1
    The quoted part applies to HTML syntax only. – Jukka K. Korpela Feb 5 '15 at 12:10
  • And the question title is "Is Doctype needed for HTML5 documents" (Q.E.D). – Mawg Feb 6 '15 at 9:04
  • 1
    The HTML5 specification clearly and noticeably says that it defines two syntaxes for HTML5, one based on original HTML and one fully conforming to XML. What you quote does not answer the question, since it applies to one of the syntaxes only. – Jukka K. Korpela Feb 6 '15 at 10:56
  • Ah! Finally I understand. Thank you very much for the clarification. – Mawg Feb 6 '15 at 12:30
2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.