Speaking of the structure of HTML, many people can explain it clearly. Generally speaking, the answer may be like this:
A DOCTYPE, an html, which contains head and body elements.
Of course this cannot be said to be incorrect, but if you ask what a minimum HTML source file must contain, I am afraid that few people can answer it correctly.
Let’s answer this question first. The content required for a simplest HTML5 source code file is as follows:
<!DOCTYPE html>
Yes, that’s it, one character is not much, one There are a lot of characters. Except for the case, which can be changed at will, no other content can be changed.
So what are the rules that cause a simplest source code file to have a doctype statement? According to the standard, an HTML document consists of the following content (strictly in order):
A BOM tag, and this BOM tag must be U+FEFF.
0-n spaces or comments.
DOCTYPE statement.
0-n spaces or comments.
An HTML element.
0-n spaces or comments.
There are some differences from HTML4 here. The simplest source code file of an HTML4 is like this:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <title>这里是标题</title>
The difference between the two is obvious:
HTML5 changes DOCTYPE to a simpler <!DOCTYPE html>
, which is already well known.
There is an additional <title>
tag in HTML4.
The key point here is the <title>
tag. Regarding this tag, the HTML4.01 standard says this:
Every HTML document must have a TITLE element in the HEAD section.
That is to say, HTML4 requires that the <title>
tag must exist.
In the HTML5 standard, it says this:
There must be no more than one title element per document.
HTML5 only sets an upper limit on the number of <title>
tags, but does not specify a lower limit. In other words, a document without <title>
is considered a legal document. .
For DOCTYPE, there are 6 types of DOCTYPE set in HTML4, and DOCTYPE is divided into 3 types in HTML5. This will be explained in detail in subsequent chapters.
Let’s go back and look at the document composition. Apart from elements that don’t make much sense, the composed list also indicates that there is an HTML element, but there is no such thing in the simplest source code. This is because in the HTML specification, there has always been the concept of "implicit tags". Regarding implicit tags, it can be roughly explained as follows:
Some elements, when certain prerequisites are met, Its start tag or end tag can be omitted in the source code. In this case, the omitted tag is called an "implicit tag".
It should be noted that the omission here refers to the omission of in the source code, but in the final DOM tree, this tag exists, so it is called Implicit tag for . Therefore, the simplest source code structure above, after generating the DOM tree, its real structure is like this:
<!DOCTYPE html>
XML, so in order to indicate that this is an HTML document, there must be a namespace whose value is www.w3.org/1999/xhtml.
text/html,
text/xml,
application/xml,
application/xml+html are better choices.
, which is the beginning and end of
The label cannot be omitted.
The above is the detailed content of HTML5 standard learning-detailed explanation of document structure. For more information, please follow other related articles on the PHP Chinese website!