Html 什么是 DOCTYPE?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/414891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 22:57:25  来源:igfitidea点击:

What is DOCTYPE?

htmlxhtmldoctypedtd

提问by Bob

  • What is DOCTYPE and why do I want to use it?
  • What are the different DOCTYPEs I can use?
  • What is the difference between standards and quirks mode, and what are some quirks I may run into with differently set DOCTYPEs?
  • 什么是 DOCTYPE,我为什么要使用它?
  • 我可以使用哪些不同的 DOCTYPE?
  • 标准模式和怪癖模式之间有什么区别,我可能会在设置不同的 DOCTYPE 时遇到哪些怪癖?

Lastly, what is the proper DOCTYPE that I should be using?

最后,我应该使用的正确 DOCTYPE 是什么?

采纳答案by Walter Rumsby

Basically, the DOCTYPE describes the HTML that will be used in your page.

基本上,DOCTYPE 描述了将在您的页面中使用的 HTML。

Browsers also use the DOCTYPE to determine how to render a page. Not including a DOCTYPE or including an incorrect one can trigger quirks mode.

浏览器还使用 DOCTYPE 来确定如何呈现页面。不包含 DOCTYPE 或包含不正确的 DOCTYPE 会触发怪癖模式。

The kicker here is, that quirks mode in Internet Explorer is quite different from quirks mode in Firefox (and other browsers); meaning that you'll have a muchharder job, trying to ensure your page renders consistently with all browsers if the quirks mode is triggered, than you will if it is rendered in standards mode.

这里的关键是,Internet Explorer 中的 quirks 模式与 Firefox(和其他浏览器)中的 quirks 模式完全不同;这意味着如果触发 quirks 模式,您将有一个困难的工作,尝试确保您的页面在所有浏览器中呈现一致,而不是在标准模式下呈现。

Wikipedia has a more in-depth summary of the differences in rendering when using various DOCTYPEs. XHTML is enabled by certain DOCTYPEs, and there is quite a bit of debate about the use of XHTML which is covered well in XHTML?—?myths and reality.

维基百科对使用各种 DOCTYPE 时呈现差异进行了更深入的总结。XHTML 是由某些 DOCTYPE 启用的,关于 XHTML 的使用存在相当多的争论,这在 XHTML 中得到了很好的介绍?—?神话和现实

There are subtle differences between different "standards compliant" rendering DOCTYPEs, such as the HTML5 DOCTYPE (<!DOCTYPE html>, prior to HTML5, only known as the "skinny doctype" which does not trigger standardized rendering in older browsers) and other DOCTYPEs such as this one for HTML 4.01 transitional:

不同的“符合标准”的渲染 DOCTYPE 之间存在细微的差异,例如 HTML5 DOCTYPE(<!DOCTYPE html>在 HTML5 之前,仅称为“瘦文档类型”,它不会在旧浏览器中触发标准化渲染)和其他 DOCTYPE,例如用于HTML 4.01 过渡:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

回答by Rob

The DOCTYPE tells the consuming user agent (web browsers, web crawlers, validation tools) what type of document the file is. Using it ensures that the consumer correctly parses the HTML as you intended it.

DOCTYPE 告诉消费用户代理(网络浏览器、网络爬虫、验证工具)文件是什么类型的文档。使用它可以确保消费者按照您的预期正确解析 HTML。

There are several different DOCTYPES for HTML, XHTML, and Framesets and each of these has two modes Strict and Transitional. Strict says that your markup is using the defined standards exactly. See W3C DTDspage for further details.

HTML、XHTML 和 Framesets 有几种不同的 DOCTYPES,其中每一种都有两种模式 Strict 和 Transitional。Strict 表示您的标记完全使用定义的标准。有关更多详细信息,请参阅W3C DTD页面。

Quirksmode is basically the layout method from the browser wars days when the standards were much less respected and defined. Generally a standards mode page, that is valid, will layout more consistently across various browsers, but may lack certain features that you require. One such features is the anchor tag's target attribute. The Quirksmodesite is a great resource for these differences.

Quirksmode 基本上是浏览器War时代的布局方法,当时标准很少受到尊重和定义。通常,有效的标准模式页面将在各种浏览器中更加一致地布局,但可能缺少您需要的某些功能。一个这样的特征是锚标签的目标属性。在怪异模式站点是这些差异的重要资源。

One final thought is that the new HTML5 standard proposes using a very simple DOCTYPE:

最后一个想法是新的 HTML5 标准建议使用一个非常简单的 DOCTYPE:

<!DOCTYPE html>

<!DOCTYPE html>

Using this DOCTYPE is a forward compatible way to specify that your pages are in standards mode, and are HTML. This is the method that Google uses, and is reasonably easy to remember. I recommend using this DOCTYPE unless you plan to use XHTML.

使用此 DOCTYPE 是一种向前兼容的方式来指定您的页面处于标准模式,并且是 HTML。这是 Google 使用的方法,而且相当容易记住。除非您打算使用 XHTML,否则我建议使用此 DOCTYPE。

回答by Rob

A doctype defines which version of HTML/XHTML your document uses. You would want to use a doctype so that when you run your code through validators, the validators know which version of HTML/XHTML to check against. This page provides a good overview:

文档类型定义了您的文档使用的 HTML/XHTML 版本。您可能希望使用 doctype,以便当您通过验证器运行代码时,验证器知道要检查哪个版本的 HTML/XHTML。此页面提供了一个很好的概述:

Don't forget to add a doctype

不要忘记添加文档类型

Common doctypes you can use are listed here:

此处列出了您可以使用的常见文档类型:

Recommended list of DTDs

推荐的 DTD 列表

Which doctype you should go with depends on the code you're using, but to get an idea, try running your code through the W3C validator and use the Document Typedrop-down menu in the "More Options" menu to try different doctypes out.

您应该使用哪种 doctype 取决于您使用的代码,但要获得一个想法,请尝试通过 W3C 验证器运行您的代码,并使用“更多选项”菜单中的“文档类型”下拉菜单来尝试不同的 doctypes .

W3C Markup Validation Service

W3C 标记验证服务

回答by Jukka K. Korpela

In HTML (including XHTML) as used on web pages, DOCTYPE is a string that triggers one of a few browser modes (quirks mode, standards mode, almost standards mode), depending on the exact spelling of the DOCTYPE. You want to use it to select a browser mode that best suits your page.

在网页上使用的 HTML(包括 XHTML)中,DOCTYPE 是触发几种浏览器模式(怪癖模式、标准模式、几乎标准模式)之一的字符串,具体取决于 DOCTYPE 的确切拼写。您想使用它来选择最适合您的页面的浏览器模式。

Formally, in SGML and XML, a DOCTYPE declaration is a reference to a Document Type Definition (DTD), which specifies the formal syntax rules of the markup language. No browser has ever used DTDs for anything or even accessed them. However, they are used by SGML and XML markup validators such as the W3C Markup Validator, except in HTML5 mode. Therefore, the choice of DOCTYPE determines how a validator works if the document is submitted to it. However, the validator mode of operation can also be selected in its user interface. (SGML and XML processors may use DOCTYPEs in different other ways, too, but the question is apparently meant to be limited to the HTML context and to web browsers and closely related software.)

形式上,在 SGML 和 XML 中,DOCTYPE 声明是对文档类型定义 (DTD) 的引用,它指定了标记语言的正式语法规则。没有浏览器曾经将 DTD 用于任何事情,甚至没有访问过它们。但是,它们被 SGML 和 XML 标记验证器(例如W3C 标记验证器)使用,HTML5 模式除外。因此,DOCTYPE 的选择决定了文档提交给验证器时验证器的工作方式。但是,也可以在其用户界面中选择验证器的操作模式。(SGML 和 XML 处理器也可能以其他不同的方式使用 DOCTYPE,但这个问题显然仅限于 HTML 上下文以及 Web 浏览器和密切相关的软件。)

There is no authoritative list of DOCTYPEs. Each HTML specification or draft defines its own DOCTYPE, or DOCTYPEs. The set of DOCTYPEs recognized by browsers when selecting mode varies by browser. In practice, there is no reason to use a DOCTYPE other than <DOCTYPE html>as defined in HTML5, though HTML5 also lists a few “legacy DOCTYPEs”. You can use that DOCTYPE if you want standards mode (recommended for new pages) and use no DOCTYPE if you want quirks mode (which you may need for legacy pages).

没有权威的 DOCTYPE 列表。每个 HTML 规范或草案都定义了自己的 DOCTYPE 或 DOCTYPE。浏览器在选择模式时识别的 DOCTYPE 集因浏览器而异。在实践中,除了<DOCTYPE html>HTML5 中定义的之外没有理由使用 DOCTYPE ,尽管 HTML5 也列出了一些“遗留的 DOCTYPE”。如果您想要标准模式(推荐用于新页面),您可以使用该 DOCTYPE,如果您想要怪癖模式(您可能需要旧页面),则不要使用 DOCTYPE。

“Standards mode” generally means the mode of operation where a browser follows HTML, CSS, DOM and other specifications the best it can. It does not usually mean full conformance. “Quirks mode” is different in different browsers, but generally it means an attempt at imitating the behavior of very old browsers like IE 5. The purpose is to keep old pages working, under the assumption that they may rely on features and bugs in the old browsers. See the description What happens in Quirks Mode?Note that there is a rather different, more limited concept of “quirks mode” in HTML5, which closely resembles the document called Quirks Mode Living Standard.

“标准模式”通常是指浏览器尽可能遵循 HTML、CSS、DOM 和其他规范的操作模式。它通常并不意味着完全一致。“怪癖模式”在不同的浏览器中是不同的,但通常它意味着尝试模仿非常旧的浏览器(如 IE 5)的行为。目的是保持旧页面的工作,假设它们可能依赖于旧浏览器。请参阅描述在 Quirks 模式下会发生什么?请注意,HTML5 中有一个相当不同的、更有限的“怪癖模式”概念,它与名为Quirks Mode Living Standard的文档非常相似。

A typical issue is that element widths are calculated differently in quirks mode and in standards mode. This means that the layout of a page may be more or less changed or even totally messed up, if a page designed to work in quirks mode is viewed in standards mode (or vice versa).

一个典型的问题是元素宽度在 quirks 模式和标准模式下的计算方式不同。这意味着,如果在标准模式下查看设计为在 quirks 模式下工作的页面(反之亦然),页面的布局可能会或多或少地发生变化,甚至完全混乱。

So you should use <!DOCTYPE html>for new pages and keep whatever DOCTYPE (if any) you have been using for old pages.

因此,您应该将其<!DOCTYPE html>用于新页面并保留用于旧页面的任何 DOCTYPE(如果有)。

However, quirks mode means, in some browsers, that many new features of CSS are not supported. This means that if you want to enhance an old page with some CSS3 feature, it may well be necessary to switch to a DOCTYPE that triggers standards mode. In such a case, you need to review and test the page to see whether it will run in standards mode.

但是,怪癖模式意味着,在某些浏览器中,不支持 CSS 的许多新功能。这意味着如果您想使用某些 CSS3 功能增强旧页面,则很可能需要切换到触发标准模式的 DOCTYPE。在这种情况下,您需要查看和测试页面以查看它是否会在标准模式下运行。

回答by Georg Sch?lly

Doctypes tell the browser in what language the page is written in, be it HTML or XHTML. For example,

文档类型告诉浏览器页面是用什么语言编写的,无论是 HTML 还是 XHTML。例如,

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">

tell the browser to render the page as HTML4 strict. Older browsers used to render pages incorrectly and therefore newer browsers simulate errors of the older browsers when they find an old doctype.

告诉浏览器将页面呈现为HTML4 strict. 较旧的浏览器过去常常错误地呈现页面,因此较新的浏览器在发现旧的 doctype 时会模拟旧浏览器的错误。

Today you should use at least HTML4 or better XHTML.

今天你至少应该使用 HTML4 或更好的 XHTML。

A blog entry about doctypes is Fix Your Site With the Right DOCTYPE!(from A List Apart).

关于 doctypes 的博客条目是使用正确的 DOCTYPE 修复您的站点!(来自A List Apart)。

回答by Ms2ger

On the web, a doctype does nothing but tell the brower if you want standards, almost standards, or quirks mode.

在网络上,文档类型只会告诉浏览器您是否需要标准、几乎标准或怪癖模式。

What changes in quirks mode depends on the browser: Firefox, Opera, Safari, and Chrome implement a limited set of quirks, like removing the space for text descenders in code like <table><tr><td><img></td></tr></table>(solution: td img { vertical-align:bottom; }). IE, on the other hand, reverts to the rendering engine in IE5.5. That means that you won't be able to use anyof the new features implemented since 2000.

怪癖模式的哪些变化取决于浏览器:Firefox、Opera、Safari 和 Chrome 实现了一组有限的怪癖,例如删除代码中的文本下行空间<table><tr><td><img></td></tr></table>(解决方案:)td img { vertical-align:bottom; }。另一方面,IE 恢复到 IE5.5 中的渲染引擎。这意味着您将无法使用自 2000 年以来实施的任何新功能。

To trigger standards mode, I suggest using the HTML5 doctype, <doctype html>, as it is the easiest to remember.

要触发标准模式,我建议使用 HTML5 文档类型<doctype html>,因为它最容易记住。

回答by Henrik Paul

A doctype is a document that describes how the contents of a xhtml-like document can look like (like a webpage). Note: this defines only the syntax of said page, the rendering of the page is NOT defined by the DTD!

doctype 是一个文档,它描述了类似 xhtml 的文档的内容的外观(如网页)。注意:这仅定义了所述页面的语法,页面的呈现不是由 DTD 定义的!

For example, a doctype could define how the <table>-tag can look like - which attributes it accepts, and which values/valuetypes are accepted for each attribute. Think of it as a lexicon for your current webpage.

例如,文档类型可以定义<table>-tag 的外观——它接受哪些属性,以及每个属性接受哪些值/值类型。将其视为您当前网页的词典。

Wikipediahas an informative page on the various Doctypes that are in common use. Mind you - there's nothing stopping you from creating your own doctype. The chances are, however, that the browser probably doesn't know how to render your document.

维基百科有一个关于各种常用文档类型的信息页面。请注意 - 没有什么可以阻止您创建自己的文档类型。然而,浏览器可能不知道如何呈现您的文档。

Which DTD to use depends on what you are going to write. XHTML has a whole different DTD than HTML, for example.

使用哪种 DTD 取决于您要编写的内容。例如,XHTML 具有与 HTML 完全不同的 DTD。

回答by ólafur Waage

First of all there is no one doctype you should be using, but most designers try to make it work within XHTML 1.0 Strict.

首先,您不应该使用任何一种文档类型,但大多数设计人员都试图使其在 XHTML 1.0 Strict 中工作。

A doctype is nothing more than a declaration of what tags you can use within your html (though the browsers can use more or less than what is defined) You can actually open up the doctype file and start reading (XHTML 1.0 Strict)

doctype 只不过是您可以在 html 中使用哪些标签的声明(尽管浏览器可以使用比定义的更多或更少的标签)您实际上可以打开 doctype 文件并开始阅读(XHTML 1.0 Strict

If you do not specify a doctype, the browser will try its best to guess but not always hits the correct type.

如果您不指定 doctype,浏览器将尽力猜测但并不总是命中正确的类型。

Quirks mode is just a technique used by browsers to be backwards compatible, a great example of quirks mode is how IE renders boxes

怪癖模式只是浏览器用来向后兼容的一种技术,怪癖模式的一个很好的例子是IE 渲染框的方式