Uppercase or lowercase doctype?

53,685

Solution 1

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.

Solution 2

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.

Solution 3

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.

Solution 4

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.

Solution 5

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.
Share:
53,685

Related videos on Youtube

joshnh
Author by

joshnh

Learner, kerner, and pancake turner.

Updated on July 08, 2022

Comments

  • joshnh
    joshnh almost 2 years

    When writing the HTML5 doctype what is the correct method?

    <!DOCTYPE html>
    

    or

    <!doctype html>
    
    • Shadow
      Shadow over 12 years
      It will be the first one usually though any can be used <!DOCTYPE html>
    • joshnh
      joshnh over 12 years
      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.
    • Leo Galleguillos
      Leo Galleguillos over 9 years
      Google.com uses a lowercase <!doctype html>
    • Zaz
      Zaz about 7 years
      @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.
    • William Entriken
      William Entriken almost 5 years
      What about for SVG files?
  • joshnh
    joshnh over 12 years
    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.
  • Stephen
    Stephen over 12 years
    This is pretty widely known - here's the w3.org link: w3.org/TR/html5/syntax.html
  • Ichiro Furusato
    Ichiro Furusato about 12 years
    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.
  • Ichiro Furusato
    Ichiro Furusato about 12 years
    ...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.
  • Mathias Bynens
    Mathias Bynens about 12 years
    @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/…
  • chharvey
    chharvey about 12 years
    @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.
  • Mathias Bynens
    Mathias Bynens about 12 years
    @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.”
  • Ichiro Furusato
    Ichiro Furusato about 12 years
    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.
  • Mr Lister
    Mr Lister about 12 years
    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.
  • Mathias Bynens
    Mathias Bynens almost 12 years
    @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.”
  • joshnh
    joshnh over 11 years
    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.
  • John Mellor
    John Mellor over 11 years
    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...
  • joshnh
    joshnh over 11 years
    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.
  • John Mellor
    John Mellor over 11 years
    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 :)
  • joshnh
    joshnh over 11 years
    I wasn't discussing what I preferred, I was bringing into question your assumption, but you seem to have missed that.
  • Bobby Jack
    Bobby Jack about 11 years
    @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)
  • Admin
    Admin almost 11 years
    @joshnh 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?
  • Timo
    Timo over 10 years
    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?
  • Ryan Williams
    Ryan Williams over 10 years
    I think @joshnh 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?
  • Mathias Bynens
    Mathias Bynens over 10 years
    @TimoLehto The HTML5 spec was written based on existing behavior in browsers. So, this post does answer your question.
  • Palec
    Palec over 10 years
    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/…
  • joshnh
    joshnh about 10 years
    I guess what I'd like to know is why the spec uses capitals while stating that the case doesn't matter.
  • Aisah Hamzah
    Aisah Hamzah over 9 years
    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.
  • Simon Logic
    Simon Logic about 2 years
    mozilla don't think so )