Learn more about DOCTYPE (document type)

DOCTYPE (Document Type Declaration) is a definition in web development that tells the browser which HTML or XML version a web page is using. Its main role is to ensure that browsers can correctly interpret and render HTML or XML documents. In this technical blog, we will learn more about the role of DOCTYPE and related information.

1. What is DOCTYPE

Use DOCTYPE to mark the HTML document version. DOCTYPE is usually the first line of code in the HTML document, that is, the content is <!DOCTYPE html>. It tells the browser which DTD (Document Type Definition) to use to ensure that web pages are rendered correctly.

2. The role of DOCTYPE

Using DOCTYPE to define the type of web page document can achieve the following functions:

  • Facilitate the browser's rendering of documents : The browser needs to know exactly the type of document to be rendered in order to use the correct rendering method and algorithm to parse and render the page.
  • Follow the W3C recommended standard: In addition to telling the browser which DTD to use, DOCTYPE can also tell the browser whether the current document complies with the W3C recommended standard, which will promote the browser to follow the standard parsing and rendering methods.
  • Solve document compatibility issues: Since different browsers may use different parsing methods to interpret web pages, the correct DOCTYPE can tell browsers how to display pages correctly and prevent cross-browser compatibility issues.

3. DOCTYPE type

There are many types of DOCTYPE, and different types correspond to different web pages. There is only one DOCTYPE statement in HTML5, because HTML5 no longer uses DTD but uses XML schema definition file. There are also different DTDs used in different types:

  • HTML5:
    <!DOCTYPE html>

  • HTML 4.01 Strict:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

  • HTML 4.01 Transitional:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

  • HTML 4.01 Frameset:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">

  • XHTML 1.0 Strict:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

    4. How to choose DOCTYPE

        When choosing a DOCTYPE, the following principles need to be followed:

  • Try to use the latest DOCTYPE declaration, such as HTML5 <!DOCTYPE html>, so as to ensure that the browser parses the document according to the latest HTML specification.
  • For legacy websites and documents, consider using legacy HTML 4.01 and XHTML DTD declarations.
  • For documents using the powerful XML language, the corresponding DTD declaration should be used.

Summarize

DOCTYPE (Document Type Declaration) is a tag that defines the type of a web page and is used to communicate the DTD version of an HTML document. It can provide important information for browsers to render web pages, avoid compatibility issues between different browsers, and ensure that web pages comply with W3C standards. It is recommended that developers use the latest version of the HTML DOCTYPE declaration and follow the W3C specification when developing web applications.

おすすめ

転載: blog.csdn.net/weixin_61719769/article/details/129741922