Html 为什么我需要文档类型?(它有什么作用)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6076432/
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
Why do I need a doctype? (What does it do)
提问by Freesn?w
Possible Duplicate:
HTML: What is the functionality of !DOCTYPE
可能的重复:
HTML: !DOCTYPE 的功能是什么
I recently asked a question here and the solution was a simple:
我最近在这里问了一个问题,解决方案很简单:
You need to add a doctype to the page. This should fix the issue for you.
您需要向页面添加文档类型。这应该为您解决问题。
Now, my pages work fine in every browser without the doctype (except IE). Does IE need a doctype (is this an IE only thing) and do other browsers just assume it ORor is it doing something I'm not seeing.
现在,我的页面在没有 doctype 的每个浏览器中都可以正常工作(IE 除外)。IE是否需要一个DOCTYPE(这是一个IE唯一的事情),做其他浏览器只是假设它OR或者是做一些我没有看到。
What are its functions and how does it work?
它的功能是什么?它是如何工作的?
采纳答案by Kevin Peno
All browsers need the doctype. Without the DOCTYPE
you are forcing the browsers to render in Quirks Mode.
所有浏览器都需要文档类型。如果没有,DOCTYPE
您将强制浏览器以Quirks Mode呈现。
However, DOCTYPE
was only partially used by the browsers in determining dialect and parsing, even though that was the purpose. This is why HTML5 has reduced the DOCTYPE
to simply:
然而,DOCTYPE
浏览器仅部分使用它来确定方言和解析,即使这是目的。这就是 HTML5 将 简化DOCTYPE
为简单的原因:
<!DOCTYPE html>
<!DOCTYPE html>
2.2. The DOCTYPE
The HTML syntax of HTML5 requires a DOCTYPE to be specified to ensure that the browser renders the page in standards mode. The DOCTYPE has no other purpose and is therefore optional for XML. Documents with an XML media type are always handled in standards mode. [DOCTYPE]
The DOCTYPE declaration is
<!DOCTYPE html>
and is case-insensitive in the HTML syntax. DOCTYPEs from earlier versions of HTML were longer because the HTML language was SGML-based and therefore required a reference to a DTD. With HTML5 this is no longer the case and the DOCTYPE is only needed to enable standards mode for documents written using the HTML syntax. Browsers already do this for<!DOCTYPE html>
.
2.2. 文档类型
HTML5 的 HTML 语法要求指定 DOCTYPE 以确保浏览器以标准模式呈现页面。DOCTYPE 没有其他用途,因此对于 XML 是可选的。具有 XML 媒体类型的文档始终在标准模式下处理。[文档类型]
DOCTYPE 声明
<!DOCTYPE html>
在 HTML 语法中不区分大小写。早期版本的 HTML 中的 DOCTYPE 更长,因为 HTML 语言是基于 SGML 的,因此需要对 DTD 的引用。在 HTML5 中,情况不再如此,DOCTYPE 只需要为使用 HTML 语法编写的文档启用标准模式。浏览器已经为<!DOCTYPE html>
.
回答by Will Martin
The Doctype does two things.
Doctype 做两件事。
- It identifies which dialect of HTML you're using.
- It controls whether the browsers uses "standards" or "quirks" mode to render the document.
- 它标识您正在使用哪种 HTML 方言。
- 它控制浏览器是使用“标准”还是“怪癖”模式来呈现文档。
If there is no doctype, or there's an unrecognized one, then it uses "quirks" mode and interprets the document as best it can. If there IS a doctype, and it recognizes it, then it follows the standards. The results of the rendering can vary depending on how it interprets the document.
如果没有文档类型,或者存在无法识别的文档类型,那么它会使用“怪癖”模式并尽可能地解释文档。如果有一个文档类型,并且它识别它,那么它遵循标准。渲染的结果可能会有所不同,具体取决于它如何解释文档。
回答by onteria_
Why?
Why specify a doctype? Because it defines which version of (X)HTML your document is actually using, and this is a critical piece of information needed by some tools processing the document.
For example, specifying the doctype of your document allows you to use tools such as the Markup Validator to check the syntax of your (X)HTML. Such tools won't be able to work if they do not know what kind of document you are using.
But the most important thing is that with most families of browsers, a doctype declaration will make a lot of guessing unnecessary, and will thus trigger a "standard" rendering mode.
为什么?
为什么要指定文档类型?因为它定义了您的文档实际使用的 (X)HTML 的哪个版本,并且这是一些处理文档的工具所需的关键信息。
例如,指定文档的 doctype 允许您使用诸如标记验证器之类的工具来检查 (X)HTML 的语法。如果这些工具不知道您使用的是哪种文档,它们将无法工作。
但最重要的是,对于大多数浏览器系列,doctype 声明将使大量猜测变得不必要,从而触发“标准”渲染模式。
回答by Stuart Burrows
You should have a DOCTYPE for ANY browser. It tells the browser how to interpret the html and css. This is why html4 and html5 have different definitions (as does xhtml). All very important for validation.
你应该有一个适用于任何浏览器的 DOCTYPE。它告诉浏览器如何解释 html 和 css。这就是 html4 和 html5 具有不同定义的原因(xhtml 也是如此)。所有这些对于验证都非常重要。
What IE will do is put the document into what it calls 'quirks mode' which basically ignores a whole heap of rules for how CSS should (by modern definitions) behave. Here is a good summary of the issue. It harks back to the bad old days of non-standardised CSS support
IE 会做的是将文档放入它所谓的“怪癖模式”,这基本上忽略了关于 CSS 应该如何(根据现代定义)行为的一大堆规则。这是该问题的一个很好的总结。它可以追溯到非标准化 CSS 支持的糟糕过去
回答by scrappedcola
Browsers need at the least to render in what is known as standards mode. See John Resig's article on the html 5 doctype: http://ejohn.org/blog/html5-doctype/. Now if you want your browser to not use standards and render like its 1990 go ahead and not add anything and you will see floats and other now standard items not work correctly. If you want to have your page render/validate in accordance to a particular standard then you would want to add more to the doc type but it is not necessary.
浏览器至少需要以所谓的标准模式呈现。请参阅 John Resig 关于 html 5 doctype 的文章:http: //ejohn.org/blog/html5-doctype/。现在,如果您希望您的浏览器不使用标准并像 1990 年那样渲染,请继续操作而不添加任何内容,您将看到浮动和其他现在的标准项目无法正常工作。如果您希望根据特定标准呈现/验证您的页面,那么您需要向文档类型添加更多内容,但这不是必需的。
回答by Ken Pespisa
From W3Schools.com:
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.
doctype 声明不是 HTML 标签;它是对 Web 浏览器的关于页面所用标记语言版本的指令。
There are a handful of different doctypes, and changing them can drastically change how your page renders.
有一些不同的文档类型,更改它们可以极大地改变页面的呈现方式。
回答by George Cummins
From W3Schools, a doctype is "an instruction to the web browser about what version of the markup language the page is written in." (http://www.w3schools.com/tags/tag_doctype.asp)
在 W3Schools 中,文档类型是“向 Web 浏览器发送的关于页面所用标记语言版本的指令”。(http://www.w3schools.com/tags/tag_doctype.asp)
If you do not include the doctype, the browser may assume you are using a different language than you really are, causing it to be rendered incorrectly.
如果您不包含 doctype,浏览器可能会假定您使用的语言与您实际使用的语言不同,从而导致它被错误地呈现。
回答by breezy
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 指定了标记语言的规则,以便浏览器正确呈现内容。