Home  >  Article  >  Web Front-end  >  HTML5 standards learning-DOCTYPE header analysis

HTML5 standards learning-DOCTYPE header analysis

黄舟
黄舟Original
2017-03-27 15:36:261988browse

The previous article mainly talked about the composition of HTML document , while superficially touching it In order to understand the concept of "tag omission", this article will conceptually introduce the first important element that appears in HTML documents - DOCTYPE

The so-called DOCTYPE is originally the concept of XML. A specific syntax is used as a kind of metadata to describe the elements allowed to appear in XML documents, as well as the composition of each element, nesting rules, etc. A more detailed result can be obtained in WIKI

But in HTML, DOCTYPE has some different effects, one of which is the famous function of triggering the browser's standards mode. That is, if there is no DOCTYPE, the browser will enter a weird mode called Quirks mode. State, in this mode, the browser's box model, style parsing, layout, etc. are different from those specified by the standard.

It should be noted that the so-called HTML standard and DOM standard. etc., only stipulates the concepts and behaviors in standard mode. As mentioned in the document structure, DOCTYPE is an absolutely indispensable part of an HTML document, so there is no concept of "Quirks mode" at all. Because the standard does not make any provisions for Quirks mode, different browsers handle Quirks mode differently, and it is even more difficult to apply Quirks mode.

HTML4 DOCTYPE

In the HTML4 standard, DOCTYPE is included in the chapter "HTML Version Information". In this chapter, the standard specifies 3 types of DOCTYPE:

  • ##Strict mode:

    a73094d7f9503c885bdc52b97b2daeb7#.

    #Transitional mode:
  • 68bdba55cd1c82042981c90600ad544f
  • .

    Frame mode:
  • 72107840b6b7c2b41e586f86d4a42878
  • .

    In the HTML4 standard, the dtd file corresponding to each DOCTYPE is specified by a legal URL and can be downloaded through the Internet. The browser can obtain the specific content of the dtd according to the URL and parse the document according to the provisions of the content.
Reality is different

HTML4, like XML, is a fairly ideal standard. However, reality is often not so ideal. Imagine the following HTML document:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" 
                      "http://www.w3.org/TR/html4/frameset.dtd">
<html>
    <head>
        <title>I&#39;m not a frameset</title>
    </head>
    <body>
        <p>So what?</p>
    </body>
</html>

This document uses a DOCTYPE in frame mode, but its text does not use any

04a0d55efbbfd646a993fbc01f262c57

elements. , corresponding to the use of standard HTML structures that should be specified by strict mode or transition mode. So what can the browser do in this situation?

Refuse to render this page? No, browsers don’t dare to do this. In the fierce market competition, if some pages cannot be rendered because of this, they can only watch the market share being injected into other fields. So the browser can at most report a weak warning in protest, but it still has to parse the document and render it correctly.

This is the so-called "fault tolerance" of the browser. In fact, no matter what your DOCTYPE is, the browser will parse a document with the greatest compatibility and make the greatest efforts to display the document. Meets developer expectations. This feature of the browser has gradually made the standard setters realize that DOCTYPE does not seem to be very important. Therefore, in

HTML5

, DOCTYPE has undergone a major change...

HTML5's DOCTYPE

has arrived in HTML5. I believe most people already know this change, that is HTML5 simplifies the declaration of DOCTYPE, only

8b05045a5be5764f313ed5b9168a17e6

is required.

As mentioned above, in the HTML4 era, standard setters have realized that DOCTYPE is not very helpful for browser rendering, except for boring w3cvalidator

, DOCTYPE seems to only trigger browser compatibility mode. So the standards working group adopted a very practical attitude and tested the simplest DOCTYPE that triggers the standard mode for all courses, and finally came to this conclusion.

But the story will not be that simple. The Standards Working Group is not a rogue who just completes such a simple action and abandons it. In fact, they still conscientiously consider backward compatibility, scalability, etc. In the end, the DOCTYPE chapter used a lot of words to describe the series of things, and got a very detailed result.

In the relevant chapters of the HTML5 reference manual, DOCTYPE is divided into three categories:

Normal DOCTYPE - normal doctype
  • Ordinary DOCTYPE is the simplest form we have seen, that is,
  • 8b05045a5be5764f313ed5b9168a17e6
  • . Its real composition is like this:

  • A piece of text , that is,
    c347aa6bf36210a552ca385274e35ace.
  • DOCTYPE no longer recommended - deprecated doctype

  • The so-called several DOCTYPEs in the HTML4 era, their composition is as follows:

    In the standard, there is a strict correspondence between Public ID and System ID. If the specified System ID cannot have a Public ID, the 8th optional content above cannot exist. HTML5 completely abandons the transitional and framework DOCTYPE in HTML4, and integrates the DOCTYPE declaration of XHTML, resulting in the following 6 combinations:

    • ##< ;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0//EN">

    • #eefd56458efca3ec090b3c2aa4356e1a

    • 9f766437f7459de87b729e6981ed3417

    • ##33e627fcde04b0c82f73bc7786d2b555
    • < ;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    • ##b0458232f716a92a5e5a8edaf384a8e4
    • 1 or more spaces.
    1. A pair of quotation marks or single quotation marks (must match before and after), put a System ID corresponding to the previous Public ID in the quotation marks.

    2. A piece of text, namely
    6c4b62b6b3d74f66c13b64fc210d204b.
  • Legacy tool DOCTYPE - legacy tool compatible doctype
  • As the name suggests, DOCTYPE is completely prepared for compatibility with the historical heritage of long ago. , there is no way to even verify what kind of "tool" will create this kind of DOCTYPE... The composition of legacy tool-type DOCTYPE is as follows:

  • For example, this DOCTYPE falls into this category:
  • < ;!doctype HTML system "about:legacy-compat">

    , basically there is nothing worth changing except the case.

    A piece of text, namely
  • c24cd350edae38201c3a1c0caaf7e044.
  • Realistic details

  • Regarding the role of DOCTYPE, in real browsing, it only plays the role of triggering the standard mode of the browser. Although according to the standard, in an HTML document, there can be other elements before DOCTYPE, such as a U+FFEF BOM, a few comments , and a little space, but in the current state, it is not so ideal:

    For IE6-9, if there is a comment before DOCTYPE, it will enter Quirks mode.

    • For IE6, if there is an XML declaration before DOCTYPE, it will enter Quirks mode.

    • After I finished writing, I discovered that the problem was all in IE... The issue about DOCTYPE has been entangled at this point. The next chapter mainly talks about the issue of encoding declaration.

    The above is the detailed content of HTML5 standards learning-DOCTYPE header analysis. For more information, please follow other related articles on the PHP Chinese website!

    Statement:
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn