html 中有哪些不同的文档类型,它们是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/255470/
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:38:38  来源:igfitidea点击:

What are the different doctypes in html and what do they mean?

htmlxhtmldoctype

提问by Chris Conway

As the title describes, what are the different doctypes available and what do they mean? I notice that the layout looks a little different in IE7 when I switch from

正如标题所描述的,有哪些不同的文档类型可用,它们是什么意思?我注意到当我从 IE7 切换时,布局看起来有点不同

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >

to

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

Are there any others and what are the effects or ramifications?

是否还有其他影响或后果?

Thanks!

谢谢!

回答by keparo

A Doctype, or Document Type Declarationassociates the document with a Document Type Definition.

一个文档类型文档类型声明关联与文档的文档类型定义

The Document Type Definitionis a standard for an XML document. There are many DTDs, for both XML and XHTML documents. XML itself doesn't have much of a schema or a very specific set of rules, apart from the requirement that everything be well-formed. You can think of a DTD as a more specific schema for the document.

文档类型定义是一个XML文档的标准。有许多 DTD,用于 XML 和 XHTML 文档。除了要求所有内容都格式正确之外,XML 本身没有太多的模式或非常具体的规则集。您可以将 DTD 视为文档的更具体模式

Rendering Modes

渲染模式

Due to the standards movement, most modern browsers actually have different rendering modes (standards mode, for rendering your document and css according to more recent web standards, and quirks mode, wherein the browser brings back some rendering ideas from the early days of the web). These modes are instituted for purposes of backward-compatibility. The vast landscape of web pages which were created in the first era of the web are rendered according to the rules of their time, while newer documents can appeal to the new wave of standards. As time goes on and new formats are imagined, a corresponding DTD could potentially be created.

由于标准运动,大多数现代浏览器实际上都有不同的渲染模式(标准模式,用于根据更新的 Web 标准渲染您的文档和 css,以及quirks 模式,其中浏览器带回了早期 Web 的一些渲染想法)。建立这些模式是为了向后兼容。在 Web 的第一个时代创建的广阔的网页景观是根据他们那个时代的规则呈现的,而较新的文档可以吸引新的标准浪潮。随着时间的推移和新格式的出现,可能会创建相应的 DTD。

Browser Discrepancies

浏览器差异

In an ideal world, a page that is being loaded by a browser would read the Doctype at the top and use it to look up a Document Type Definition. It would then use the schema of that DTD as the basis for reading the rest of the document. Doctypes, then, would be essential for validating markup documents. The DTD would provide the standard against which your document is to be validated.

在理想情况下,浏览器正在加载的页面会读取顶部的 Doctype 并使用它来查找文档类型定义。然后它将使用该 DTD 的模式作为阅读文档其余部分的基础。那么,文档类型对于验证标记文档是必不可少的。DTD 将提供验证您的文档所依据的标准。

Unfortunately, it's not an ideal world. Browsers don't necessarily behave consistently here, and if they do, the consistent behavior isn't quite in line with the original vision for Doctypes. Although parsing is done independently of the Doctype, major browsers will at least examine the Doctype to determine the rendering mode. If your Doctype is absent or is incomplete, the browser will likely be rendering in quirks mode. For well-written, modern documents to appear correctly, the browser should be rendering in standards mode. Mozilla, Safari, and some recent versions of Opera actually implement an Almost Standardsmode, which is dedicated entirely to transitional pages.

不幸的是,这不是一个理想的世界。浏览器在这里不一定表现一致,如果他们这样做,一致的行为就不太符合 Doctypes 的原始愿景。虽然解析是独立于 Doctype 完成的,但主流浏览器至少会检查 Doctype 以确定渲染模式。如果您的 Doctype 不存在或不完整,浏览器可能会以quirks mode呈现。为了使编写良好的现代文档正确显示,浏览器应以标准模式呈现。Mozilla、Safari 和一些最新版本的 Opera 实际上实现了几乎标准模式,该模式完全专用于过渡页面。

When you change the Doctype and notice changes in the way a page is displayed, it is because the browser may be applying a slightly different set of rules when it tries to parse the document. As a consequence, the resulting page may be a little bit different, depending on whether all of its parts conform to the DTD, or at least, depending on the browser, that your data validates within the rendering mode that the doctype suggests.

当您更改 Doctype 并注意到页面显示方式发生变化时,这是因为浏览器在尝试解析文档时可能应用了一组略有不同的规则。因此,结果页面可能略有不同,这取决于其所有部分是否符合 DTD,或者至少取决于浏览器,您的数据在 doctype 建议的呈现模式内验证。

Choosing a Doctype

选择文档类型

In pursuit of standards-compliance, strict Doctypes should be used whenever possible.

为了追求标准合规性,应尽可能使用严​​格的 Doctypes。

When writing in XHTML, this Doctype is common:

XHTML编写时,这种 Doctype 很常见:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

When writing in HTML 4.1, this one is common instead:

在用HTML 4.1编写时,这个是常见的:

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

Some other common doctypes for XHTML and HTML 4 are listed here, for completeness:

为完整起见,此处列出了 XHTML 和 HTML 4 的其他一些常见文档类型:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">

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

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

Debate on Strict versus Transitional Doctypes

关于严格与过渡文档类型的争论

Standards evangelists have called for web developers to stop using the Transitional Doctype on new pages and instead use Strict. Again, this is a case where the theory and the practice have some difficulties being reconciled. The original hope of the transitional Doctype was to provide a halfway house for transitioning legacy websites toward standards-compliance. With transitional doctypes, the restriction on elements and attributes is literally "less strict", so developers would be able to get their work running under standards mode sooner, and phase out the outstanding differences over time.

标准布道者呼吁 Web 开发人员停止在新页面上使用 Transitional Doctype,而是使用 Strict。同样,这是理论和实践难以调和的情况。过渡 Doctype 的最初希望是为将遗留网站过渡到标准合规性提供中途。使用过渡文档类型,对元素和属性的限制实际上“不那么严格”,因此开发人员将能够更快地在标准模式下运行他们的工作,并随着时间的推移逐步淘汰突出的差异。

Controversy exists because it isn't always quite so simple for a developer change the Doctype in an enterprise environment. Freelance developers and makers of small- or medium- sized websites may often have an easier time determining their Doctype and making this transition. In an enterprise production environment for a highly-demanded web-based service, there are inherently more complicated dependencies on legacy systems and 3rd party code products, which themselves may be on a roadmap for removal or redesign, but the execution of such changes must be done methodically and incrementally.

之所以存在争议,是因为开发人员在企业环境中更改 Doctype 并不总是那么简单。中小型网站的自由开发者和制作者通常可以更轻松地确定他们的 Doctype 并进行这种转换。在高度需求的基于 Web 的服务的企业生产环境中,对遗留系统和第 3 方代码产品的固有依赖性更复杂,这些产品本身可能在移除或重新设计的路线图上,但必须执行此类更改有条不紊地、循序渐进地完成。

Helpful Tools

有用的工具

The W3C (World Wide Web Consortium) is a group which plays an active role in defining these kinds of standards. They maintain a helpful online tool at http://validator.w3.org/for verifying and validating documents against their standards. There are many other 3rd party tools and browser extensionswith similar functionality.

W3C(万维网联盟)是一个在定义此类标准方面发挥积极作用的组织。他们在http://validator.w3.org/维护了一个有用的在线工具,用于根据他们的标准验证和验证文件。还有许多其他具有类似功能的第 3 方工具和浏览器扩展

回答by cic

Browsers don't carewhat doctype you use (well, almost true), they use it for one thing and one thing only: to decide which render modeto use. See e.g. the Fxor Opera documentationfor real-world examples on what algorithms is used to decide which mode to use (I guess there is some documentation for IE buried somewhere in MSDN too ... [This may be the correct page](http://msdn.microsoft.com/en-us/library/ms535242(VS.85).aspx), I don't know, sorry).

浏览器不在乎你使用什么 doctype(嗯,几乎是真的),他们只用它做一件事:决定使用哪种渲染模式。有关使用哪些算法来决定使用哪种模式的实际示例,请参见FxOpera 文档(我猜在 MSDN 中也有一些有关 IE 的文档...... [这可能是正确的页面](http ://msdn.microsoft.com/en-us/library/ms535242(VS.85).aspx),我不知道,抱歉)。

There are however two major modes in most browsers (some browsers have an almost standards modetoo):

然而,大多数浏览器有两种主要模式(有些浏览器也有几乎标准的模式):

  • quirks mode(used when no "correct" doctype is found, "correct" from the browsers point of view): try to render the document as some old version of IE would do (one of the most important differences, i.e. affects rendering the most, is that some browsers exploits the IE box model bugin this mode),
  • and standard mode(used when the browser found a doctype it considers correct): try to do as the standards says.
  • quirks 模式(当没有找到“正确”的文档类型时使用,从浏览器的角度来看“正确”):尝试像某些旧版本的 IE 一样渲染文档(最重要的区别之一,即影响渲染最多,是不是有些浏览器利用了这种模式下的IE盒子模型bug),
  • 标准模式(当浏览器发现它认为正确的文档类型时使用):尝试按照标准进行操作。

You can use (the non-standard) document.compatModeproperty in previous mentioned browsers to check which mode that was used to render the current document.

您可以document.compatMode在前面提到的浏览器中使用(非标准)属性来检查用于呈现当前文档的模式。

(Note on XHTML: I assumed that you serve you documents as HTML (text/html), if you serve you document as XHTML (probably application/xhtml+xml) most browser jumps into standard mode directly and don't care about the doctype at all AFAIK.)

(关于 XHTML 的注意事项:我假设您将文档作为 HTML ( text/html) 提供,如果您将文档作为 XHTML (可能application/xhtml+xml)提供,大多数浏览器会直接跳转到标准模式并且根本不关心文档类型 AFAIK。)

BTW: the recommendation (or, what looked like a recommendation) in the other answer is broken, the transitionalDTD should not be used on new documents. Always use strict (the term "strict" is kind of misleading, should be "default" or something else non-scary), period:

顺便说一句:另一个答案中的建议(或看起来像建议)被破坏,不应在新文档上使用过渡DTD。始终使用严格(术语“严格”有点误导,应该是“默认”或其他不可怕的东西),句号:

Authors should use the Strict DTD when possible, but may use the Transitional DTD when support for presentation attribute and elements is required. -- HTML 4.01: 22 Transitional Document Type Definition.

We recommend that authors write documents that conform to the strict DTD rather than the other DTDs defined by this specification. -- HTML 4.01: 4 Conformance: requirements and recommendations

如果可能,作者应该使用 Strict DTD,但在需要支持表示属性和元素时可以使用 Transitional DTD。- HTML 4.01:22 过渡文档类型定义

我们建议作者编写符合严格 DTD 而不是本规范定义的其他 DTD 的文档。-- HTML 4.01: 4 一致性:要求和建议

And there are many blog post about this, e.g. no more Transitional DOCTYPEs, please(from 2006, but some, obviously, still have problems with this :).

并且有很多关于此的博客文章,例如,请不要再使用 Transitional DOCTYPEs(从 2006 年开始,但显然有些人仍然对此有问题:)。

This post started out with pointing out that browsers don't care what you choose, and then developed into a rant about how to choose the correct DTD, interesting ... But if you are going to spend(/waste?) time and energy to choose a DTD you might as well choose the correct one (from a HTML 4.01 standard perspective that is).

这篇文章一开始指出浏览器不关心你选择什么,然后发展成关于如何选择正确的 DTD 的咆哮,有趣......但如果你要花费(/浪费?)时间和精力要选择 DTD,您不妨选择正确的 DTD(从 HTML 4.01 标准的角度来看)。

Or, you can ignore all this and use the following instead, soonanyway:

或者,你可以忽略这一切,改用以下,很快反正

<!doctype html>

(This answerto "any reason not to start using the HTML 5 doctype?" was kind of related to the last part.)

这个“有什么理由不开始使用 HTML 5 文档类型?”的答案与最后一部分有关。)

回答by Noah Goodrich

Here is the official explanation of the various DTD's from the W3C:

以下是 W3C 对各种 DTD 的官方解释:

http://www.w3.org/QA/2002/04/valid-dtd-list.html

http://www.w3.org/QA/2002/04/valid-dtd-list.html

You might also find the following beneficial:

您可能还会发现以下好处:

http://www.freedivs.com/tutorials/Choosing%20a%20DOCTYPE/

http://www.freedivs.com/tutorials/Choosing%20a%20DOCTYPE/

回答by JacquesB

There is a lot of misinformation around doctypes. The confusion stems from the fact that doctypes originally was intended for one purpose (to identify the DTD, ie. the HTML version used), but in real-world browsers are used for a completely unrelated purpose.

关于文档类型有很多错误信息。混淆源于这样一个事实,即 doctypes 最初用于一个目的(识别 DTD,即使用的 HTML 版本),但在现实世界的浏览器中用于完全不相关的目的。

Doctype declarations are only used for one thingin todays browsers, that is switching between quirksrendering mode and standardsrendering mode for CSS. So basically it is a CSS-thing, not a HTML-thing.

Doctype 声明在当今的浏览器中仅用于一件事,即在CSS 的quirks渲染模式和标准渲染模式之间切换。所以基本上它是一个 CSS 的东西,而不是一个 HTML 的东西。

Quirks mode rendering is backwards compatible with some old rendering bugs in older browsers, and is mostly useful for legacy content you dont want to fix. New content should always use standards mode, since it renders more correct and consistently among browsers. (There is still rendering differences between browsers when using standards mode, but there are much worse in quirks mode.)

Quirks 模式渲染向后兼容旧浏览器中的一些旧渲染错误,并且主要用于您不想修复的旧内容。新内容应始终使用标准模式,因为它在浏览器之间呈现更正确和一致。(使用标准模式时浏览器之间仍然存在渲染差异,但在 quirks 模式下更糟糕。)

It does notmake any difference whether you choose a HTML or XHTML docytype, neither will it make any difference if you choose strict or transitional doctype. The rendering mode is basically selected like this:

它并没有任何区别,你是否选择HTML或XHTML docytype,但也不会做出任何区别,如果你选择严格或过渡性的doctype。渲染模式基本上是这样选择的:

  • If the document don't have any doctype, quirksmode is selected.
  • If the document have an unrecognizeddoctype, standardsmode is selected. This means you can write a random doctype like <!DOCTYPE Chris>and it will work perfectly fine.
  • Official W3C doctypes withoutthe correct url (the second string in the tag) selects quirksmode. All other doctypes selects standards mode. (Edit: of course it is more complex than that, and it even differs between browsers which of the recognized doctypes triggers quirks mode. Se hsivonens overview, linked from another answer.)
  • 如果文档没有任何文档类型,则选择quirks模式。
  • 如果文档具有无法识别的文档类型,则选择标准模式。这意味着你可以编写一个随机的文档类型<!DOCTYPE Chris>,它会工作得很好。
  • 没有正确 url(标签中的第二个字符串)的官方 W3C doctypes选择quirks模式。所有其他文档类型选择标准模式。(编辑:当然它比这更复杂,它甚至在浏览器之间有所不同,哪些已识别的文档类型触发了怪癖模式。Se hsivonens 概述,从另一个答案链接。)

Historically doctypes were intended to declare which version and subset of HTML were used. HTML4 defines several versions, where "transitional" allows a number of elements ans attributes that (like FONT) is not allowed in "strict". A browser could theoretically process "strict" documents different than "transitional"-document. However no browser actually does this.

从历史上看,文档类型旨在声明使用的 HTML 版本和子集。HTML4 定义了几个版本,其中“过渡”允许许多元素和属性(如 FONT)在“严格”中不允许。理论上,浏览器可以处理不同于“过渡”文档的“严格”文档。然而,实际上没有浏览器会这样做

Edit: scunliffe points out that IE8 will have yet another rendering mode, "IE8 standards" mode. However AFAIK this mode is not triggered by a doctype but by a meta-tag.

编辑:scunliffe 指出 IE8 将有另一种渲染模式,“IE8 标准”模式。然而,AFAIK 这种模式不是由文档类型触发,而是由元标签触发。

回答by dicroce

Basically, the doctype determines how crazy IE is going to be.

基本上,文档类型决定了 IE 的疯狂程度。

If you don't set it to XHTML, or "strict" you'll be living in a world of hurt when it comes to IE (even if you set it, you'll still be hating on IE, but it does make it a lot better).

如果您不将其设置为 XHTML 或“严格”,那么在 IE 方面您将生活在一个受伤害的世界中(即使您设置了它,您仍然会讨厌 IE,但它确实做到了好了很多)。