26

I've always wondered whether whitespace before a DOCTYPE matters, but I can't seem to find any definitive information on the web. People say not to do it, but I can't see what effect it's likely to have, or even if the spec says it must be like that. I can't figure out if it triggers compatibility mode in IE or anything annoying like that.

The reason is, in my (crappy asp.net) code, I would much prefer to write:

<%@ Page language="c#" Codepage="65001" AutoEventWireup="true" %>
<%@ OutputCache Location="None" VaryByParam="none" %>
<!doctype html>

than:

<%@ Page language="c#" Codepage="65001" AutoEventWireup="true" %><%@ OutputCache Location="None" VaryByParam="none" %><!doctype html>

Because the first is neater. But the result of the top one is a few blank lines in your rendered html.

Not a huge issue. I've just always wondered if it matters :)

1
  • I have a similar situation: <metal:page define-macro="master"> <tal:doctype tal:replace="structure string:&lt;!DOCTYPE html&gt;" />
    – Falk
    Feb 2, 2016 at 8:47

6 Answers 6

27

Yes! comments and spaces may come first.

Here's the relevant section from the official HTML Specification

3
8

There are occasional browsers that have problems with whitespace before the doctype -- the classic example was IE6, which was said to go into quirks mode at the sight of it. Turns out that this is only true for some very specific types of whitespace (non-breakable spaces, &nbsp; for example). That said, IE6 compatibility is not high on most peoples' priority lists.

4

To maximize the odds of avoiding Quirks Mode, put the doctype declaration at the very start of the HTML document. You won’t find any official statement on this, since Quirks Mode is about violating standards and about actual browser behavior. It is part of the very idea of Quirks Mode that it is largely undocumented; authors are not supposed to trust on it. It’s meant to make legacy pages behave tolerably (as they used to), not something that you should use for new pages.

Modern browsers generally allow an empty line before the doctype, or a comment, or an XML declaration, without going to Quirks Mode. But just don’t put anything there, unless you have a compelling reason to do so.

3

Note that the HTML specification linked to by Ray is wrong. While whitespace is allowed before the DOCTYPE, the specification says that also comments may come before the DOCTYPE. But that does not work in Internet Explorer 9 and older.

2
  • 8
    I must question hard the logic behind "Versions of browser X do not support it" and "The specification is wrong". Particularly as "the HTML specification linked to by Ray" is the w3.org specification... Jan 10, 2013 at 15:08
  • 3
    @KristianGlass: Maybe I should have worded it differently than "wrong". Maybe "not useful for a web developer" is better. But it does not change, that if you just trust the spec, you can end up creating something which does not work in a browser you may need to support. Jan 11, 2013 at 15:48
1

No, the Doctype doesn't have to be the first line of the file but it does have to come before the <html> tag. That is the only limit you have, so your first method to do it is fine.

The declaration must be the very first thing in your HTML document, before the tag.

Gotten from: w3 schools (doctypes)

1
0

Actually, the practical advice is to limit initial text that includes a <meta charset="UTF-8"> statement to 1024 bytes. This is due to the need of some browsers to use a limited prescan to determine the character encoding quickly if a character encoding header is missing.

See W3C and Sivonen for details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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