简体   繁体   English

<!doctype html>做什么?

[英]What does <!doctype html> do?

What does this tag do? 这个标签做什么?

<!doctype html>

What I can see is that with this tag on top my html behaves in another way than without. 我能看到的是,这个标签位于顶部,我的html表现得与其他方式不同。 How do I know if I need this tag? 我怎么知道我是否需要这个标签?

It's an integral part of HTML as defined in the specification : 它是规范中定义HTML的组成部分:

8.1.1 The DOCTYPE 8.1.1 DOCTYPE

A DOCTYPE is a required preamble. DOCTYPE是必需的前导码。

DOCTYPEs are required for legacy reasons. 遗留原因需要DOCTYPE。 When omitted, browsers tend to use a different rendering mode that is incompatible with some specifications. 省略时,浏览器倾向于使用与某些规范不兼容的其他渲染模式。 Including the DOCTYPE in a document ensures that the browser makes a best-effort attempt at following the relevant specifications. 在文档中包含DOCTYPE可确保浏览器尽最大努力遵循相关规范。


Edit to add: 编辑添加:

What does this seatbelt do? 这个安全带做什么?

安全带图像

What I can see is that, with this seatbelt on, my car behaves the same way as without. 我能看到的是,在这个安全带打开的情况下,我的车的行为与没有安全带一样。 How do I know if I need this seatbelt? 我怎么知道我是否需要这个安全带?

You wont know if you'll need it until something goes wrong and you don't have it. 你不会知道你是否需要它,直到出现问题并且你没有它。

DOCTYPE Declaration is the abbreviation for Document Type Declaration (DTD).

The DOCTYPE Declaration (DTD or Document Type Declaration) does a couple of things: DOCTYPE声明(DTD或文档类型声明)做了两件事:

  1. When performing HTML validation testing on a web page it tells the HTML (HyperText Markup Language) validator which version of (X)HTML standard the web page coding is supposed to comply with. 当在网页上执行HTML验证测试时,它告诉HTML(超文本标记语言)验证器网页编码应符合哪个(X)HTML标准版本。 When you validate your web page the HTML validator checks the coding against the applicable standard then reports which portions of the coding do not pass HTML validation (are not compliant). 当您验证网页时,HTML验证程序会根据适用的标准检查编码,然后报告编码的哪些部分未通过HTML验证(不符合要求)。

  2. It tells the browser how to render the page in standards compliant mode. 它告诉浏览器如何在符合标准的模式下呈现页面。

If the web page coding does not include a DOCTYPE Declaration (DTD or Document Type Declaration) or it is done incorrectly: 如果网页编码不包含DOCTYPE声明(DTD或文档类型声明)或未正确完成:

  1. You will not be able to use a HTML (HyperText Markup Language) Validator to check the page coding. 您将无法使用HTML(超文本标记语言)验证程序来检查页面编码。 HTML validation requires the DOCTYPE declaration. HTML验证需要DOCTYPE声明。

  2. The browser rendering the webpage will process the coding in Quirks Mode. 渲染网页的浏览器将以Quirks模式处理编码。

  3. The stylesheet may not be implemented as planned. 样式表可能未按计划实施。

Which DOCTYPE should you use? 你应该使用哪种DOCTYPE?

If you are a beginner to HTML (HyperText Markup Language) then I would suggest you use the HTML 4.01 Transitional declaration. 如果您是HTML(超文本标记语言)的初学者,那么我建议您使用HTML 4.01 Transitional声明。 It is much more forgiving for the beginner when performing HTML validation . 在执行HTML验证时,初学者会更加宽容。 You would also use this declaration if you know that your audience will not have a browser that supports CSS (Cascading Style Sheets). 如果您知道您的受众群体没有支持CSS(层叠样式表)的浏览器,您也可以使用此声明。

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

This DOCTYPE declaration still allows elements and presentation attributes that should be in the stylesheet. 此DOCTYPE声明仍允许应该在样式表中的元素和表示属性。


If you want to learn to code in preparation for the future but still not ready for XHTML then you would use the Strict declaration. 如果您想学习编写代码以备将来但尚未准备好使用XHTML,那么您将使用Strict声明。

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

With this declaration all presentation attributes are moved to the stylesheet. 使用此声明,所有表示属性都将移动到样式表。

A complete list of DOCTYPE Declarations (DTDs) is available at List of valid DTDs you can use in your document. DOCTYPE声明(DTD)的完整列表可在您可以在文档中使用的有效DTD列表中找到

The doctype declaration should be the very first thing in an HTML document, before the tag. 在标记之前,doctype声明应该是HTML文档中的第一件事。

The doctype declaration is not an HTML tag; doctype声明不是HTML标记; it is an instruction to the web browser about what version of the markup language the page is written in. 它是Web浏览器关于页面编写的标记语言版本的指令。

The doctype declaration refers to a Document Type Definition (DTD). doctype声明引用文档类型定义(DTD)。 The DTD specifies the rules for the markup language, so that the browsers render the content correctly. DTD指定标记语言的规则,以便浏览器正确呈现内容。

Quote from here: http://www.w3schools.com/tags/tag_doctype.asp 引自这里: http//www.w3schools.com/tags/tag_doctype.asp

:) :)

A Doctype declaration triggers standards mode in your browser and should always be used. Doctype声明会在浏览器中触发标准模式,并且应始终使用。 Quirks mode should always be avoided. 应始终避免使用怪癖模式。

Browser War and 2 Modes 浏览器大战和2种模式

To understand the significance of DOCTYPE let us dig into some history starting from the days of browser wars. 为了理解DOCTYPE的重要性,让我们从浏览器大战的日子开始深入研究一些历史。 During those days web pages were written in two different versions to support both Internet Explorer & Netscape Navigator . 在那些日子里,网页以两种不同的版本编写,以支持Internet ExplorerNetscape Navigator

But then W3C took over defining Web Standards. 但随后W3C接管了Web标准的定义。 But sadly, browsers with new standards were not able to render web pages built for legacy browsers. 但遗憾的是,具有新标准的浏览器无法呈现为旧版浏览器构建的网页。 So, to support the website built for legacy browsers QUIRK MODE was introduced. 因此,为了支持为旧版浏览器构建的网站,引入了QUIRK MODE In which the browser assumes you've written old-fashioned, invalid markup and code per the depressing industry norms of the late 1990s. 其中浏览器假设你已经根据20世纪90年代后期令人沮丧的行业规范编写了老式的,无效的标记和代码。

To differentiate those website from the new ones, DOCTYPE was added, which gave the browser a signal that this web page should be rendered in STANDARD MODE . 为了区分这些网站和新网站,添加了DOCTYPE ,这给浏览器一个信号,即该网页应该以STANDARD MODE呈现。

HTML5 && HTML 4.01 HTML5 && HTML 4.01

Prior to HTML5, the html was SGML based which required a Document Type Defination to establish rules and grammer for markup. 在HTML5之前,html是基于SGML的,它需要文档类型定义来为标记建立规则和语法。 Thus, doctype had other information about the Document Type Defination (DTD). 因此,doctype具有关于文档类型定义(DTD)的其他信息。

However, with introduction of HTML5 the DOCTYPE is the only thing needed to enable the STANDARD MODE . 但是,通过引入HTML5, DOCTYPE是启用STANDARD MODE所需的唯一选择。

为简单<!DOCTYPE HTML><!DOCTYPE HTML>声明代码是HTML。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM