<!doctype html> 有什么作用?

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

What does <!doctype html> do?

htmldoctype

提问by Niklas

What does this tag do?

这个标签有什么作用?

<!doctype html>

<!doctype html>

What I can see is that with this tag on top my html behaves in another way than without. How do I know if I need this tag?

我能看到的是,在顶部使用此标签时,我的 html 的行为方式与没有时不同。我怎么知道我是否需要这个标签?

回答by zzzzBov

It's an integral part of HTML as defined in the specification:

它是规范中定义HTML的组成部分:

8.1.1 The DOCTYPE

A DOCTYPE is a required preamble.

DOCTYPEs are required for legacy reasons. 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.

8.1.1 文档类型

DOCTYPE 是必需的前导码。

出于遗留原因,需要 DOCTYPE。省略时,浏览器倾向于使用与某些规范不兼容的不同渲染模式。在文档中包含 DOCTYPE 可确保浏览器尽最大努力遵循相关规范。



Edit to add:

编辑添加:

What does this seatbelt do?

Seatbelt image

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.

在出现问题并且您没有它之前,您不会知道是否需要它。

回答by Random Guy

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

  2. It tells the browser how to render the page in standards compliant mode.

  1. 在网页上执行 HTML 验证测试时,它会告诉 HTML(超文本标记语言)验证器网页编码应该符合哪个版本的 (X)HTML 标准。当您验证网页时,HTML 验证器会根据适用的标准检查编码,然后报告编码的哪些部分未通过 HTML 验证(不合规)。

  2. 它告诉浏览器如何以符合标准的模式呈现页面。

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 validation requires the DOCTYPE declaration.

  2. The browser rendering the webpage will process the coding in Quirks Mode.

  3. The stylesheet may not be implemented as planned.

  1. 您将无法使用 HTML(超文本标记语言)验证器来检查页面编码。HTML 验证需要 DOCTYPE 声明。

  2. 渲染网页的浏览器将在 Quirks 模式下处理编码。

  3. 样式表可能不会按计划实施。

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 Transitionaldeclaration. It is much more forgiving for the beginner when performing HTML validation. You would also use this declaration if you know that your audience will not have a browser that supports CSS (Cascading Style Sheets).

如果您是 HTML(超文本标记语言)的初学者,那么我建议您使用HTML 4.01 Transitional声明。在执行HTML 验证时,对于初学者来说更宽容。如果您知道您的受众没有支持 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 DTDsyou can use in your document.

您可以在文档中使用的有效 DTD 列表中提供了完整的 DOCTYPE 声明 (DTD)列表

回答by RylandAlmanza

The doctype declaration should be the very first thing in an HTML document, before the tag.

The doctype declaration is not an HTML tag; it is an instruction to the web browser about what version of the markup language the page is written in.

The doctype declaration refers to a Document Type Definition (DTD). The DTD specifies the rules for the markup language, so that the browsers render the content correctly.

doctype 声明应该是 HTML 文档中的第一件事,在标签之前。

doctype 声明不是 HTML 标签;它是对 Web 浏览器的关于页面所用标记语言版本的指令。

doctype 声明是指文档类型定义 (DTD)。DTD 指定了标记语言的规则,以便浏览器正确呈现内容。

Quote from here: http://www.w3schools.com/tags/tag_doctype.asp

从这里引用:http: //www.w3schools.com/tags/tag_doctype.asp

:)

:)

回答by Nefeli

A Doctype declaration triggers standards mode in your browser and should always be used. Quirks mode should always be avoided.

Doctype 声明会触发浏览器中的标准模式,应始终使用。应始终避免使用 Quirks 模式。

回答by Afsaar Ahmad

Browser War and 2 Modes

浏览器War和 2 种模式

To understand the significance of DOCTYPElet us dig into some history starting from the days of browser wars. During those days web pages were written in two different versions to support both Internet Explorer& Netscape Navigator.

为了理解它的重要性,DOCTYPE让我们从浏览器War时代开始挖掘一些历史。在那些日子里,网页是用两种不同的版本编写的,以支持Internet Explorer& Netscape Navigator

But then W3Ctook over defining Web Standards. 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 MODEwas introduced. In which the browser assumes you've written old-fashioned, invalid markup and code per the depressing industry norms of the late 1990s.

但随后W3C接手了定义 Web 标准的工作。但遗憾的是,具有新标准的浏览器无法呈现为传统浏览器构建的网页。因此,引入了支持为旧浏览器构建的网站QUIRK MODE。浏览器假定您按照 1990 年代后期令人沮丧的行业规范编写了老式的、无效的标记和代码。

To differentiate those website from the new ones, DOCTYPEwas 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. Thus, doctype had other information about the Document Type Defination (DTD).

在 HTML5 之前,html 是基于 SGML 的,它需要文档类型定义来建立标记规则和语法。因此,doctype 有关于文档类型定义 (DTD) 的其他信息。

However, with introduction of HTML5 the DOCTYPEis the only thing needed to enable the STANDARD MODE.

但是,随着 HTML5 的引入,这DOCTYPE是启用STANDARD MODE.

回答by Komali

To make it simple: <!DOCTYPE HTML>states that the code is HTML.

为了简单起见:<!DOCTYPE HTML>声明代码是 HTML。