Uppercase or lowercase doctype?
Asked Answered
R

7

224

When writing the HTML5 doctype what is the correct method?

<!DOCTYPE html>

or

<!doctype html>
Retrenchment answered 11/8, 2011 at 4:49 Comment(5)
It will be the first one usually though any can be used <!DOCTYPE html>Bombe
Managed to answer my own question but thought I would leave it in case others had a similar thought. I personally prefer lowercase; nothing else in HTML5 is uppercase.Retrenchment
Google.com uses a lowercase <!doctype html>Callie
@HelloWorld: That's because lower case letters compress better because they're more common. For sites that get millions of hits a day, this can make a difference.Reta
What about for SVG files?Wanderjahr
R
214

In HTML, the DOCTYPE is case insensitive. The following DOCTYPEs are all valid:

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

<!DOCTYPE html>

See The XML serialization of HTML5, aka ‘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.

Ralina answered 2/2, 2012 at 8:26 Comment(10)
No, this is incorrect. In SGML the document type declaration is production 110, in XML it is production 28. In both cases it is declared as the fixed string "DOCTYPE" (which is not a tag, it is a keyword). So in the canonical definitions in the respective SGML and XML standards it is always uppercase. If you find software that permits a lowercase "doctype" that software is not in compliance with the standards. Browsers are designed to be "liberal in what they accept", but a compliant XML processor should flag this as a validation error.Does
...and I should point out that X/HTML5 is incorrect on this as well. As specified, XHTML5 is simply not valid XML markup, nor can it be. There are numerous errors in the X/HTML5 specification. I don't take it very seriously. The fact that they had to create a "polyglot markup" (a completely new concept) clearly says they've punted.Does
@Ichiro I’m talking about HTML, not SGML. No browser ever implemented SGML. If they did, <title/foo/ would be equivalent to <title>foo</title>. Only a few HTML validators use SGML. The HTML5 spec is the first one to clarify this: whatwg.org/specs/web-apps/current-work/multipage/…Ralina
@Mathias Bynens: "... to conform to the Polygot Markup Guidelines for HTML-Compatible XHTML Documents, it should be written in lowercase." Don't you mean "uppercase"? That's the first bullet point in your citation.Photomap
@TestSubject528491 That paragraph is about the second part of the DOCTYPE (html). Here’s the full sentence: “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.”Ralina
Hi Mathias, I don't quite get your point. All versions of HTML prior to XHTML were specified as SGML markup languages, where the DOCTYPE declaration is required to be uppercase (what browsers do is a separate issue). And not to quibble, but your point about markup minimisation would only be true if specified in the SGML Declaration; for HTML your examples are not equivalent markup. I stand by my comments about HTML5: there are numerous errors that indicate its authors fail to understand (or wilfully choose to ignore) the SGML and XML standards.Does
@IchiroFurusato You might want to read up on HTML5. Quote: “However, unlike previous versions of HTML, the HTML[5] serialization is no longer considered an application of SGML, but instead defines its own syntax. While the syntax is inspired by SGML, it is being defined in a way that more closely resembles the way browsers actually handle HTML in the real world, particularly in regards to error handling.”Ralina
We are talking about the world of internet here. Since when has any browser ever given a flying buck what the standard says about anything so why should I? I want to know if there has ever been a browser version that would not have understood the lowercase doctype or mixed case doCTypE declaration, only the legacyish all capital DOCTYPE version. Anyone know anything about that?Boykin
@TimoLehto The HTML5 spec was written based on existing behavior in browsers. So, this post does answer your question.Ralina
In XHTML5, the DOCTYPE must either be missing or case-sensitively match <!DOCTYPE html>. Case sensitivity is forced by XML even on the root element name. en.wikipedia.org/wiki/…Asberry
T
45

If anyone is still wondering in 2014, please consult this:

HTML5

W3 HTML5 Spec - Doctype

A DOCTYPE must consist of the following components, in this order:

  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

W3 HTML5 - XHTML

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


Polyglot Markup

W3 Polyglot Markup - Intro

It is sometimes valuable to be able to serve HTML5 documents that are also well formed XML documents.

W3 Polyglot Markup - Doctype

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:

  • The string DOCTYPE is in uppercase letters.
    ...

So, note that Polyglot Markup uses a regular HTML5 doctype, but with additions/changes. For our discussion, most notably that DOCTYPE is declared in all caps.


Summary

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.

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.

Trinitrobenzene answered 30/5, 2014 at 2:58 Comment(1)
mozilla don't think so )Whizbang
N
20

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.

November answered 15/8, 2012 at 11:36 Comment(8)
The specification pretty clearly states that doctype must contain a string that is the case-insensitive equivalent of '<!DOCTYPE', therefore implying that '<!dOcTyPe' is just as correct.Retrenchment
As I said, the spec requires browsers to support any case, so any case will work. But the question-asker is clearly looking for a standard, canonical form, so in the absence of strong reasons to choose one over the other, you may as well use the example case from the spec...November
Firstly, I am the person who asked the question. Secondly, what exactly is it that you are looking at the suggests to you that uppercase is more correct than lowercase?! Your answer doesn't make sense; you say that it is case-insensitive, so therefore uppercase is the more correct answer.Retrenchment
The spec consistently writes it as <!DOCTYPE html>, therefore it would be reasonable to infer that the author of the spec has a slight preference for that particular capitalization. I'm going to use the same capitalization as the spec, since it's nice for these things to be consistent, but if you prefer <!dOcTyPe then be my guest :)November
I wasn't discussing what I preferred, I was bringing into question your assumption, but you seem to have missed that.Retrenchment
@Retrenchment I don't understand your confusion. 1) The DOCTYPE declaration is required to be case-insensitive. 2) The HTML5 spec uses the capitalization "DOCTYPE html" in its examples. All this answer seems to be saying is that one capitalization is consistent with the literature, even if all are valid. Why is that so outrageous?Neume
I think @Retrenchment misunderstood what John was trying to say. FWIW, I agree with John's sentiment that in the absence of any particular reason to format a certain way, going with what the specification asks that browsers check against is preferable. John was never trying to imply that it's more correct, only that it's something to base your preference on — otherwise it's a completely arbitrary choice, right?Holp
I guess what I'd like to know is why the spec uses capitals while stating that the case doesn't matter.Retrenchment
H
7

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

This is why I always upper-case the doctype.

Hesta answered 14/4, 2014 at 13:51 Comment(0)
D
6

The standard for HTML5 is that tags are case insensitive.

http://www.w3schools.com/html5/tag_doctype.asp

More Technically: (http://www.w3.org/TR/html5/syntax.html)

A DOCTYPE must consist of the following components, in this order:

  1. A string that is an ASCII case-insensitive match for the string <!DOCTYPE.
Discoverer answered 11/8, 2011 at 4:51 Comment(5)
I don't trust W3Schools 100% they are not affiliated with the W3C and although most of their information is good, some of it is not.Retrenchment
This is pretty widely known - here's the w3.org link: w3.org/TR/html5/syntax.htmlDiscoverer
W3Schools don't have more or fewer errors than other websites. (Completely random example) Take the information with a grain of salt, consult the w3.org pages when in doubt, and you'll be fine. The real fools are those who believe that W3Schools is an official body or that their certificates mean anything.Statampere
@MrLister: I don't think it's fair to call people fools because they think there is a link between something named "W3Schools" and something named "W3C". The two are similar enough in name and domain to cause confusion, and the assumption that the former is somehow endorsed by, or related to, the latter. That is patently W3Schools' intention, which is one of the reasons they anger so many people who understand the issue. The fact that a lot of their information is inaccurate compounds the problem (i.e. it might be slightly forgivable if they actually provided a reasonable service)Colonnade
The link to tag_doctype.asp is invalid (404) and this question is about the doctype declaration, which is not a tag... Anyway, the conclusion is correct: in HTML5, the keyword doctype can be in upper, lower or mixed case.Jaquith
E
3

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.

So a HTML5-capable browser would accept the lowercase one and process the html properly.

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.

Ea answered 4/6, 2015 at 13:21 Comment(0)
I
1

The doctype declaration is case insensitive, and any string of ASCII that matches

Html5 standard

Ictus answered 15/8, 2019 at 20:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.