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 🙂
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Yes! comments and spaces may come first.
Here’s the relevant section from the official HTML Specification
Method 2
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, for example). That said, IE6 compatibility is not high on most peoples’ priority lists.
Method 3
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.
Method 4
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.
Method 5
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)
Method 6
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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0