Html 大写或小写文档类型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7020961/
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
Uppercase or lowercase doctype?
提问by joshnh
When writing the HTML5 doctype what is the correct method?
编写 HTML5 文档类型时,正确的方法是什么?
<!DOCTYPE html>
or
或者
<!doctype html>
回答by Mathias Bynens
In HTML, the DOCTYPE is case insensitive. The following DOCTYPEs are all valid:
在 HTML 中,DOCTYPE 不区分大小写。以下 DOCTYPE 都是有效的:
<!doctype html>
<!DOCTYPE html>
<!DOCTYPE HTML>
<!DoCtYpE hTmL>
In XML serializations (i.e. XHTML) the DOCTYPE is not required, but if you use it, DOCTYPE
should be uppercase:
在 XML 序列化(即 XHTML)中,不需要 DOCTYPE,但如果使用它,DOCTYPE
则应为大写:
<!DOCTYPE html>
See The XML serialization of HTML5, aka ‘XHTML5':
请参阅HTML5 的 XML 序列化,又名“XHTML5”:
Note that if you don't uppercase
DOCTYPE
in an XHTML document, the XML parser will return a syntax error.The second part can be written in lowercase (
html
), uppercase (HTML
) or even mixed case (hTmL
) — it will still work. However, to conform to the Polyglot Markup Guidelines for HTML-Compatible XHTML Documents, it should be written in lowercase.
请注意,如果您
DOCTYPE
在 XHTML 文档中不使用大写字母,则 XML 解析器将返回一个语法错误。第二部分可以用小写 (
html
)、大写 (HTML
) 甚至混合大小写 (hTmL
)编写——它仍然有效。但是,为了符合HTML 兼容 XHTML 文档的多语言标记指南,它应该以小写形式编写。
回答by ucsarge
If anyone is still wondering in 2014, please consult this:
如果还有人在2014年还在疑惑,请咨询这个:
HTML5
HTML5
A DOCTYPE must consist of the following components, in this order:
DOCTYPE 必须按以下顺序包含以下组件:
1. A string that is an ASCII case-insensitive match for the string "<!DOCTYPE".
...
Note:despite being displayed in all caps, the spec states it is insensitive
注意:尽管以全部大写显示,但规范声明它不敏感
----------------------------------------------------------------------
-------------------------------------------------- --------------------
XHTML5
XHTML5
This specification does not define any syntax-level requirements
beyond those defined for XML proper.
XML documents may contain a DOCTYPE if desired, but this is not required
to conform to this specification. This specification does not define
a public or system identifier, nor provide a formal DTD.
Looking at the XML spec, it lists DOCTYPE in caps, but I can't find anything that states that 'all caps' is required ( for comparison, in the HTML5 spec listed above, it is displayed in the example in all caps, but the spec explicitly states that is is case-insensitive).
查看 XML 规范,它以大写形式列出了 DOCTYPE,但我找不到任何说明需要“全部大写”的内容(为了进行比较,在上面列出的 HTML5 规范中,它以全部大写形式显示在示例中,但是规范明确指出不区分大小写)。
----------------------------------------------------------------------
-------------------------------------------------- --------------------
Polyglot Markup
多语言标记
It is sometimes valuable to be able to serve HTML5 documents that are also well formed XML documents.
有时能够提供也是格式良好的 XML 文档的 HTML5 文档是有价值的。
Polyglot markup uses a document type declaration (DOCTYPE) specified by section 8.1.1 of [HTML5]. In addition, the DOCTYPE conforms to the following rules:
多语言标记使用 [HTML5] 的第 8.1.1 节指定的文档类型声明 (DOCTYPE)。此外,DOCTYPE 符合以下规则:
* The string DOCTYPE is in uppercase letters.
So, note that Ployglot Markup uses a regular HTML5 doctype, but with additions/changes. For our discussion, most notably that DOCTYPE is declared in all caps.
因此,请注意 Ployglot 标记使用常规的 HTML5 文档类型,但有添加/更改。对于我们的讨论,最值得注意的是 DOCTYPE以全部大写形式声明。
----------------------------------------------------------------------
-------------------------------------------------- --------------------
Summation
求和
View the W3's HTML vs. XHTML section
[Opinion]I wouldn't worry too much about satisfying XML compliance unless you are specifically trying to make considerations for it. For most client and JS-based server development, JSON has replaced XML.
[意见]我不会太担心满足 XML 合规性,除非您特别想为此考虑。对于大多数客户端和基于 JS 的服务器开发,JSON 已经取代了 XML。
Therefore, I can only see this really applying if you are trying to update an existing, xhtml/xml-based legacy system to co-exist with new, HTML5 functionality. If this is the case then look into the polyglot markup spec.
因此,只有当您尝试更新现有的、基于 xhtml/xml 的遗留系统以与新的 HTML5 功能共存时,我才能看到这真的适用。如果是这种情况,请查看多语言标记规范。
回答by John Mellor
According to the latest spec, you should use something that is a case-insensitive match for <!DOCTYPE html>
. So while browsers are required to support whatever case you prefer, it's reasonable to infer from this that <!DOCTYPE html>
is the canonical case.
根据最新的规范,您应该使用不区分大小写的匹配<!DOCTYPE html>
. 因此,虽然浏览器需要支持您喜欢的任何情况,但从中推断出这<!DOCTYPE html>
是规范情况是合理的。
回答by jpw
Either upper or lower case is "correct". However if you use web fonts and care about IE7, I'd recommend using <!DOCTYPE html>
because of a bug in IE7 where web fonts sometimes fail if using <!doctype html>
(e.g. in this answer).
大写或小写都是“正确的”。但是,如果您使用网络字体并关心 IE7,我建议您使用,<!DOCTYPE html>
因为 IE7 中存在一个错误,如果使用网络字体有时会失败<!doctype html>
(例如在这个答案中)。
This is why I always upper-case the doctype.
这就是为什么我总是大写文档类型。
回答by Stephen
The standard for HTML5 is that tags are case insensitive.
HTML5 的标准是标签不区分大小写。
http://www.w3schools.com/html5/tag_doctype.asp
http://www.w3schools.com/html5/tag_doctype.asp
More Technically: (http://www.w3.org/TR/html5/syntax.html)
更多技术:(http://www.w3.org/TR/html5/syntax.html)
A DOCTYPE must consist of the following components, in this order:
DOCTYPE 必须按以下顺序包含以下组件:
- A string that is an ASCII case-insensitive match for the string
<!DOCTYPE
.
- 作为字符串的 ASCII 不区分大小写匹配的字符串
<!DOCTYPE
。
回答by Clive
The question sort of implies there's only one correct answer, supplies a multiple choice of two, and asks us to pick one. I would suggest that for HTML5 both <!DOCTYPE html>
and <!doctype html>
are valid.
这个问题有点暗示只有一个正确答案,提供两个多项选择,并要求我们选择一个。我建议对于 HTML5<!DOCTYPE html>
和<!doctype html>
都是有效的。
So a HTML5-capable browser would accept the lowercase one and process the html properly.
因此,支持 HTML5 的浏览器将接受小写字母并正确处理 html。
Browsers previous and oblivious to HTML5, I've heard, even without a doctype, will attempt to process the html as best they can. And if they don't recognize the lowercase doctype will do the same. So there's no point in making it uppercase since those browsers won't be able to fully implement any HTML5 declarations anyway.
我听说浏览器以前并且不知道 HTML5,即使没有 doctype,也会尝试尽可能地处理 html。如果他们不识别小写的 doctype 也会这样做。因此将其设为大写是没有意义的,因为这些浏览器无论如何都无法完全实现任何 HTML5 声明。
回答by Ichoku Chinonso
The doctype declaration is case insensitive, and any string of ASCII that matches
doctype 声明不区分大小写,任何匹配的 ASCII 字符串