---
title: "HTML doctype"
author: "quqtalk"
date: "2014-05-17T15:32:04Z"
tags: []
categories: ["Web前端"]
slug: "html-doctype"
draft: false
lastmod: "2014-05-17T17:07:07Z"
id: 2067646
url: "https://quqtalk.iteye.com/blog/2067646"
---

**1.doctype的概念？**

html的doctype声明需要放在html的最开始，doctype的解释请参考“HTML &lt;!DOCTYPE&gt; 标签”。

**2.为什么要用doctype？**

doctype出现的目的是为了让浏览器知道是用“怪异模式（Quirks Mode）”还是“标准/严格模式（Standards Mode）”渲染html。实际上除了“怪异模式”和“严格模式”在其他浏览器中还有一种“类严格模式（Almost Standards Mode）”。各个浏览器会根据doctype的不同值开启不同的模式。详细信息可以参考“Activating Browser Modes with Doctype”。

之所以出现不同模式的原因是在IE和Netscape 兼容W3C标准之前，对CSS的实现有很大的差异，在IE和Netscape向W3C标准靠拢后，为了向前兼容，就出现了不同的模式。可以让web开发人员选择使用哪种模式作为标准。

不同的模式会影响浏览器在CSS布局、CSS解析和脚本（Script）这3方面产生的行为。

**3.doctype该怎么写？**

HTML5不在基于SGML，所以强烈建议将html的doctype声明为：“&lt;!DOCTYPE html&gt;”。这会让浏览器开启标准/严格模式。

**4.doctype嗅探(doctype sniffing)**

Doctype sniffing only applies to documents served as text/html. Documents sent as XML always activate the Standards layout mode. This includes documents sent as application/xhtml+xml. The consequence is that XHTML 1.0 Transitional documents are rendered in the Almost Standards mode when served as text/html under pretext of the Appendix C but in the Standards Mode when served asapplication/xhtml+xml.

**5.HTML不再基于SGML？**

The HTML5 standard specifies [two serializations](http://www.w3.org/QA/2008/01/html5-is-html-and-xml.html) of HTML5: "html" and "xml". "xml" is a valid XML serialization (which in turn is a subset of SGML). "html" is not based on any specific serialization standard anymore, [it has its own complete serialization](http://www.w3.org/html/wg/drafts/html/master/syntax.html#syntax). Herein lies the difference: HTML4 has a "sgml" serialization and "xml" serialization (called XHTML 1.0)

Of course HTML5 is for a large part based on HTML4, which is based on SGML and XHTML, which is based on HTML4 and XML.

Also see the [history section](http://www.w3.org/html/wg/drafts/html/master/introduction.html#history-0) of the HTML5 specification

 

参考：

TML &lt;!DOCTYPE&gt; 标签：

http://www.w3school.com.cn/tags/tag\_doctype.aspa

为什么使用&lt;!DOCTYPE HTML&gt;：

“http://i.wanz.im/2010/05/28/why\_doctype\_html/”

Activating Browser Modes with Doctype：

https://hsivonen.fi/doctype/

HTML,XML,XHTML,HTML5和SGML简介：

http://lastplace.diandian.com/2012-10-04/sgml-the-sons

Quirks mode and strict mode：

http://www.quirksmode.org/css/quirksmode.html#link1

什么是“怪异模式”，什么是“标准模式”：

https://developer.mozilla.org/zh-CN/docs/Mozilla\_Web\_Developer\_FAQ