The HTML document type declaration, also known as DOCTYPE
, is the first line of code required in every HTML or XHTML document. The DOCTYPE
declaration is an instruction to the web browser about what version of HTML the page is written in. This ensures that the web page is parsed the same way by different web browsers.
HTML文档类型声明,也称为DOCTYPE
,是每个HTML或XHTML文档中所需的第一行代码。 DOCTYPE
声明是向Web浏览器发出的有关该页面写入HTML版本的指令。这可确保不同的Web浏览器以相同的方式解析该Web页面。
In HTML 4.01, the DOCTYPE
declaration refers to a document type definition (DTD). A DTD defines the structure and the legal elements of an XML document. Because HTML 4.01 was based on the Standard Generalised Markup Language (SGML), referring to a DTD in the DOCTYPE
declaration was necessary.
在HTML 4.01中, DOCTYPE
声明引用文档类型定义(DTD)。 DTD定义XML文档的结构和法律元素。 因为HTML 4.01是基于标准通用标记语言(SGML)的,所以必须在DOCTYPE
声明中引用DTD。
Additionally, doctypes for HTML 4.01 required the declaration of either strict
, transitional
, or frameset
DTD, each with a different use case as outlined below.
此外,HTML 4.01的doctype需要声明strict
, transitional
或frameset
DTD,每种声明具有不同的用例,如下所述。
Strict DTD: Used for web pages that exclude attributes and elements that W3C expects to phase out as CSS support grows
严格DTD :用于不包含W3C期望随着CSS支持的增长而逐步淘汰的属性和元素的网页
Transitional DTD: Used for web pages that include attributes and elements that W3C expects to phase out as CSS support grows
过渡DTD :用于包含 W3C期望随着CSS支持的增长而逐步淘汰的属性和元素的网页
Frameset DTD: Used for web pages with frames
框架集DTD :用于带有框架的网页
In contrast, the declaration of HTML5 DOCTYPE
is much simpler: it no longer requires a reference to DTDs as it is no longer based on SGML. See the examples below for a comparison between HTML 4.01 and HTML5 DOCTYPE
s.
相比之下,HTML5 DOCTYPE
的声明要简单得多:由于不再基于SGML,因此不再需要引用DTD。 请参见以下示例,以比较HTML 4.01和HTML5 DOCTYPE
。
例子 (Examples)
Doctype syntax for HTML5 and beyond:
HTML5及更高版本的Doctype语法:
<!DOCTYPE html>
Doctype syntax for strict HTML 4.01:
严格HTML 4.01的Doctype语法:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
Doctype syntax for transitional HTML 4.01:
过渡HTML 4.01的doctype语法:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
Doctype syntax for frameset HTML 4.01:
框架集HTML 4.01的Doctype语法:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">
历史 (History)
During the formative years of HTML, web standards were not agreed upon yet. Browser vendors would build new features in whatever way they wanted. There was little concern for competing browsers.
在HTML的形成年代,尚未达成Web标准的共识。 浏览器供应商将以他们想要的任何方式构建新功能。 竞争的浏览器几乎没有关注。
The result was that web developers had to choose a browser to develop their sites for. This meant that sites would not render well in unsupported browsers. This situation could not continue.
结果是,Web开发人员必须选择浏览器才能为其开发网站。 这意味着网站无法在不受支持的浏览器中很好地呈现。 这种情况无法持续。
The W3C (World Wide Web Consortium) wrote a set of web standards to handle this situation. All browser vendors and web developers should adhere to these standards. This would ensure that websites would render well across browsers.
W3C(万维网联盟)编写了一套Web标准来处理这种情况。 所有浏览器供应商和Web开发人员都应遵守这些标准。 这样可以确保网站在所有浏览器中都能很好地呈现。
The changes required by the standards were quite different from some existing practices. Adhering to them would break existing non standards compliant websites.
标准要求的更改与某些现有实践完全不同。 坚持使用它们会破坏现有的不符合标准的网站。
To handle this problem, vendors began programming rendering modes in to their browsers. Web developers would need to add a doctype declaration to the top of an HTML document. The doctype declaration would tell the browser which rendering mode to use for that document.
为了解决此问题,供应商开始在其浏览器中编写渲染模式。 Web开发人员需要在HTML文档的顶部添加doctype声明。 doctype声明将告诉浏览器该文档使用哪种呈现模式。
Three separate rendering modes were generally available across browsers.
跨浏览器通常可以使用三种单独的渲染模式。
Full standards mode renders pages according to the W3C web standards.
完全标准模式将根据W3C Web标准呈现页面。
Quirks mode renders pages in a non standards compliant way.
“怪癖”模式以不符合标准的方式呈现页面。
Almost standards mode is close to full standards mode, but features support for a small number of quirks.
几乎标准模式接近于全标准模式,但是支持少量怪癖。
In the modern age of HTML5, web standards are fully implemented in all major browsers. Web sites are generally developed in a standards compliant way. Because of this the HTML5 doctype declaration only exists to tell the browser to render the document in full standards mode.
在HTML5的现代时代,Web标准已在所有主流浏览器中完全实现。 网站通常以符合标准的方式开发。 因此,HTML5 doctype声明仅用于告诉浏览器以完全标准模式呈现文档。
用法 (Usage)
The Doctype Declaration must be the very first line of code in an HTML document, aside from comments, which can go before it if needed. For modern HTML5 documents the doctype declaration should be as follows:
除注释外,文档类型声明必须是HTML文档中的第一行代码,必要时可以在注释之前添加注释。 对于现代HTML5文档,doctype声明应如下所示:
<!DOCTYPE html>
<!DOCTYPE html>
更多信息: (More Information:)
While no longer in general use, there are several other doctype declaration types from previous versions of HTML. There are also specific versions for XML documents. To read more about these, and to see code examples for each, take a look at the Wikipedia article.
尽管不再通用,但以前HTML版本中还有其他几种doctype声明类型。 还有针对XML文档的特定版本。 要阅读有关这些内容的更多信息,并查看每个示例的代码示例,请参阅Wikipedia文章 。
A quick explanation of “Quirks Mode” and “Standards Mode”
“ Quirks模式”和“ Standards模式”的简要说明
翻译自: https://www.freecodecamp.org/news/what-is-the-doctype-declaration-in-html/