doctype作用
Specifying the correct doctype of your web page is very important. Without it, the client (the web browser, iPhone, etc) will have no idea which version of HTML you are using to write the document, and will default to guessing how the HTML should be rendered. In many browsers, this is known as “Quirks” mode, and can result in pages displaying oddly. By specifying the doctype, we tell the client “try to render this document according to the standards of this specification”. That information is added at the very start of the page, as follows:
指定网页的正确文档类型非常重要。 没有它,客户端(Web浏览器,iPhone等)将不知道您使用哪种版本HTML编写文档,并且默认情况下会猜测应该如何呈现HTML。 在许多浏览器中,这被称为“ Quirks”模式,并且可能导致页面显示异常。 通过指定doctype,我们告诉客户“尝试根据此规范的标准来呈现此文档”。 该信息将在页面的开始处添加,如下所示:
<!DOCTYPE html>
The first line for HTML5 – the doctype
– tells the browser which version of HTML we are using on a page. You may wish to think of the doctype as a negotiation or an exchange of information between the page and the browser, much as you might, upon meeting a stranger in Switzerland, attempt words in several different languages before both parties settle on speaking English. In our case, our web page is saying “Hi. I can give you the information that follows as at least HTML 5.”
HTML5的第一行(即doctype
)告诉浏览器我们在页面上使用HTML版本。 您可能希望将doctype视为页面和浏览器之间的协商或信息交换,就像您在瑞士遇到一个陌生人时,在双方决定讲英语之前,尝试用几种不同的语言来尝试该doctype一样。 在我们的例子中,我们的网页说“嗨。 我可以为您提供至少HTML 5以下的信息。”
The modern HTML5 doctype is unusual in that it doesn’t specify a version name or number: HTML is now a “living language” that is constantly under development and change. Version numbers are only used in the formal specification.
现代HTML5文档类型很不寻常,因为它没有指定版本名称或版本号:HTML现在是一种“生活语言”,并且一直在不断发展和变化。 版本号仅在正式规范中使用。
The doctype
is required to validate the page, in order to check that the page has the right structure, and that browsers will understand it. Without a doctype, the validator has no idea what version of HTML you are using, or what rules to check your code against.
必须使用doctype
来验证页面,以便检查页面的结构正确,并且浏览器可以理解它。 没有doctype,验证器将不知道您使用的是哪个版本HTML,或检查代码所依据的规则。
doctype作用