什么是文档类型?

发布于 2024-07-11 10:41:51 字数 161 浏览 7 评论 0 原文

  • 什么是 DOCTYPE?为什么要使用它?
  • 我可以使用哪些不同的 DOCTYPE?
  • 标准模式和怪癖模式之间有什么区别?使用不同设置的 DOCTYPE 可能会遇到哪些怪癖?

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

  • 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?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

趁年轻赶紧闹 2024-07-18 10:41:51

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

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

这里的关键是,Internet Explorer 中的怪异模式与 Firefox(和其他浏览器)中的怪异模式有很大不同; 这意味着,与在标准模式下呈现相比,如果触发了怪异模式,您将有一个更加困难的工作,试图确保您的页面在所有浏览器中呈现一致。

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

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

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

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

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.

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 much harder 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.

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.

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 HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
⊕婉儿 2024-07-18 10:41:51

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

HTML、XHTML 和框架集有多种不同的 DOCTYPE,每种都有两种模式:严格模式和过渡模式。 严格表示您的标记完全使用定义的标准。 有关更多详细信息,请参阅 W3C DTD 页面。

Quirksmode 基本上是浏览器战争时期的布局方法,当时标准受到的尊重和定义要少得多。 通常,有效的标准模式页面将在各种浏览器中布局更加一致,但可能缺少您需要的某些功能。 这样的功能之一是锚标记的目标属性。 Quirksmode 网站是了解这些差异的重要资源。

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

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

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.

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 DTDs page for further details.

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 Quirksmode site is a great resource for these differences.

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

<!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.

拥醉 2024-07-18 10:41:51

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

不要忘记添加文档类型

可以使用的列表如下:

推荐的 DTD 列表

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

W3C 标记验证服务

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:

Don't forget to add a doctype

Common doctypes you can use are listed here:

Recommended list of DTDs

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 Type drop-down menu in the "More Options" menu to try different doctypes out.

W3C Markup Validation Service

月朦胧 2024-07-18 10:41:51

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

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

不存在 DOCTYPE 的权威列表。 每个 HTML 规范或草案都定义了自己的 DOCTYPE 或 DOCTYPE。 选择模式时浏览器识别的 DOCTYPE 集因浏览器而异。 实际上,没有理由使用 以外的 DOCTYPE 如 HTML5 中定义,尽管 HTML5 还列出了一些“旧版 DOCTYPE”。 如果您需要标准模式(建议用于新页面),则可以使用该 DOCTYPE;如果您需要怪异模式(旧页面可能需要这种模式),则可以不使用 DOCTYPE。

“标准模式”通常是指浏览器尽可能遵循 HTML、CSS、DOM 和其他规范的操作模式。 它通常并不意味着完全一致。 “Quirks 模式”在不同的浏览器中有所不同,但通常它意味着尝试模仿 IE 5 等非常旧的浏览器的行为。目的是让旧页面保持正常运行,假设它们可能依赖于旧版浏览器中的功能和错误。旧浏览器。 请参阅说明Quirks 模式会发生什么? 请注意,有一个HTML5 中“怪异模式”的概念相当不同,且更加有限,它与名为 怪异模式生活标准

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

因此,您应该对新页面使用 并保留您在旧页面中使用的任何 DOCTYPE(如果有)。

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

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.

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.)

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).

“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.

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).

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

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.

分開簡單 2024-07-18 10:41:51

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

例如,文档类型可以定义 -tag 可以看起来像 - 它接受哪些属性,以及每个属性接受哪些值/值类型。 将其视为当前网页的词典。

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

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

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!

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.

Wikipedia has 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.

甜味拾荒者 2024-07-18 10:41:51

Doctype 告诉浏览器页面是用什么语言编写的,是 HTML 还是 XHTML。 例如,

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

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

现在您应该至少使用 HTML4 或更好的 XHTML。

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

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

<!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.

Today you should use at least HTML4 or better XHTML.

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

冷默言语 2024-07-18 10:41:51

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

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

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

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

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.

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)

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

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

誰認得朕 2024-07-18 10:41:51

在网络上,文档类型什么也不做,只是告诉浏览器您是否需要标准、几乎标准或怪异模式。

怪异模式的变化取决于浏览器:Firefox、Opera、Safari 和 Chrome 实现了一组有限的怪异,例如删除

等代码中文本下伸部分的空间。

(解决方案:td img {vertical-align:bottom; })。 另一方面,IE 则恢复为 IE5.5 中的渲染引擎。 这意味着您将无法使用自 2000 年以来实施的任何新功能。

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

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 any of the new features implemented since 2000.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文