What is the HTML5 Document Type Declaration and Character Encoding?
Jul 08, 2025 am 01:50 AMHTML5 document type declaration and character encoding are the basis of web page development, which directly affects page display and parsing. 1. HTML5 uses a concise to declare the document type, which must be located at the top of the file, otherwise the browser may enter "weird mode". 2. It is recommended to use UTF-8 character encoding and use <meta charset="UTF-8"> to avoid garbled problems and ensure multilingual support. 3. In actual development, the complete HTML5 structure should include lang attributes, introduce meta encoding as soon as possible, optional viewport settings, and DOCTYPE must be located in the first row.
HTML5 document type declaration and character encoding are two basic but critical parts in web development. Although they look simple, they directly affect the display effect of the page and the browser's parsing method.

1. HTML5 Document Type Declaration (DOCTYPE)
In HTML5, the document type declaration is very concise, just one sentence:
<!DOCTYPE html>
The purpose of this line of code is to tell the browser that the current document is using the HTML5 standard. It must be written at the top of the HTML file and cannot have anything in front of it (including spaces or comments).

DOCTYPE in previous HTML versions (such as HTML4 or XHTML) is much more complex, for example:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
But now HTML5 simplifies this process, just a simple <!DOCTYPE html>
is needed.

Not adding DOCTYPE may cause the browser to enter "weird mode", which will affect page rendering.
2. Character Encoding
HTML5 recommends using UTF-8 as the default character encoding. The way to set character encoding is to add the following meta tags to the <head>
section:
<meta charset="UTF-8">
UTF-8 can support almost all language characters, including Chinese, Japanese, Korean, Latin letters, etc., to avoid garbled code problems.
If you forget to set character encoding, or set it incorrectly, the following phenomena may occur:
- The Chinese language on the page becomes a question mark or a garbled code
- An exception occurred in the form submission
- The browser automatically guesses that the encoding causes inconsistent display
It is recommended to always use UTF-8 and make sure that the server is also configured to send UTF-8-encoded response headers.
3. Common practices in practical applications
A complete HTML5 basic structure is usually as follows:
<!DOCTYPE html> <head> <meta charset="UTF-8">Page Title This is the content of the page.