Why Use DOCTYPE HTML

Why Use DOCTYPE HTML
You know that without it, the browser will use quirks mode when rendering the page; you know that different browsers render different elements differently in quirks mode. So you would write a doctype like this:

Copy code
The code is as follows:

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

Fortunately, various web development tools are now powerful enough to support the insertion of template code, so you don’t need to type out this long and smelly doctype letter by letter. But if you've had enough of it, you might try this:

Copy code
The code is as follows:

<!DOCTYPE html>

Wow, very concise! The benefits are obvious: 1. You can easily write this doctype without worrying about making mistakes; 2. You save about 105 bytes of characters. For a site with tens of millions of daily PVs, it can save a considerable amount of traffic; 3. It is backward compatible. Yes, the html5 doctype is written like this, and modern browsers recognize it.

If you're like me and have always thought that not specifying a DTD will turn on the browser's weird mode , you're wrong! The correct statement should be that quirks mode will be enabled only when doctype is not defined, that is, you only need to define <!doctype html> to let the browser render the page in strict mode (standard mode) without specifying a certain type of dtd. Let's review that all browsers need two modes: quirks mode and strict mode (some people also call it standards mode). IE 6 for Windows/mac, Mozilla, Safari and Opera all implement both modes, but versions below IE 6 are always stuck in quirks mode. Here are some things you need to know about the two modes:

  1. Pages written before standardization had no doctype, so pages without a doctype are rendered in quirks mode.
  2. On the other hand, if a web developer adds a doctype, it means that he knows what he is doing. Most doctypes will turn on strict mode (standard mode) and the page will be rendered according to the standard.
  3. Any new or unknown doctype will start in strict mode (standards mode).
  4. Each browser has its own way to activate quirks mode. You can look at this list: http://hsivonen.iki.fi/doctype/

Note: You don't need to validate your page against your chosen doctype at all; the mere presence of the doctype tag is enough to enable strict mode (standards mode). If you still doubt what I'm saying, go to http://www.quirksmode.org/css/quirksmode.html#link2 to find out what you need to know. We only need a small piece of JavaScript code to get the answer, it is:

Copy code
The code is as follows:

mode=document.compatMode;

This code can be used to determine whether the current browser is in quirks mode or standards mode. The compatibility of this property is beyond doubt. If you have doubts, you can check http://www.quirksmode.org/dom/w3c_html.html#t11. You can visit http://wanz.im/demo/doctype-test.html in the browser you want to test and you can see the results. As far as I know, this does not activate quirks mode, even under IE6. If you have any new discoveries, please leave me a message.

<<:  Introduction to the use of MySQL performance stress benchmark tool sysbench

>>:  20 CSS coding tips to make you more efficient (sorted)

Recommend

Thoughts on truncation of multi-line text with a "show more" button

I just happened to encounter this small requireme...

How to enter and exit the Docker container

1 Start the Docker service First you need to know...

Installation and daemon configuration of Redis on Windows and Linux

# Installation daemon configuration for Redis on ...

JavaScript implements double-ended queue

This article example shares the specific code of ...

XHTML introductory tutorial: Web page Head and DTD

Although head and DTD will not be displayed on th...

How to add Lua module to Nginx

Install lua wget http://luajit.org/download/LuaJI...

How to transfer files between Windows and Linux

File transfer between Windows and Linux (1) Use W...

Implementation of deploying war package project using Docker

To deploy war with Docker, you must use a contain...

Installation and configuration tutorial of MySQL 8.0.16 under Win10

1. Unzip MySQL 8.0.16 The dada folder and my.ini ...

MySQL 5.7.18 winx64 installation and configuration method graphic tutorial

The installation of compressed packages has chang...

Summary of 4 ways to add users to groups in Linux

Preface Linux groups are organizational units use...

How to solve the problem that mysql cannot be closed

Solution to mysql not closing: Right-click on the...

MAC+PyCharm+Flask+Vue.js build system

Table of contents Configure node.js+nvm+npm npm s...

Analysis and description of network configuration files under Ubuntu system

I encountered a strange network problem today. I ...