Does my HTML5 doctype have to be on the VERY first line?

别来无恙 提交于 2019-12-18 05:28:06

问题


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:


If you want the official answer, go to the W3C's official documents and read about the syntax there.

Comments and spaces may come first.

Here's the link: http://www.w3.org/TR/html-markup/documents.html#conformant-documents.




回答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, &nbsp; for example). That said, IE6 compatibility is not high on most peoples' priority lists.




回答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.




回答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.




回答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)



来源:https://stackoverflow.com/questions/10924865/does-my-html5-doctype-have-to-be-on-the-very-first-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!