Original post address: http://moonless.net/demo/optional-tags/
The end tag of Some HTML elements is optional, such:Body,Colgroup,Dd,DT,Head,Html,Li,Optgroup,Option,P,Tbody,TD,Tfoot,Th,Thead,TrAnd so on
These tags do not need to end because in most cases, their end tags are implicitly declared through another tag in the document. For example, in most web pages, a paragraph<P>The following section is usually followed by another section or other block-level labels. the browser ends the current section based on the start label of the next section.
And Image<Colgroup>Such labels can contain or do not contain<Col>Such a specific tag. If<Colgroup>Not included<Col>, Even if you do not write</Colgroup>It will not cause any errors (the number of columns can be defined by the span attribute ).
√ Without the end tag, the page loading speed can be improved.
The biggest advantage of not writing the end tags is that because these tags actually occupy characters, omitting them can make the page size smaller. If you are looking for ways to increase the page loading speed, it is a good start to process these optional end labels. For pages with many paragraphs and tables, this can save a lot of cost.
√ Do not write the end label to avoid the appearance of textnode before and after the element.
According to the white-space processing modelWhite space charactersWhen the Dom is generated, it is ignored or merged according to the rules. The latter will beWhite space charactersMerge into a space, which brings a lot of trouble.
For exampleDisplayIsInline | inline-blockThen, it will be painful to process the space in the process.
× Possible problems caused by not writing the end tag
Here are several important topics:
-
The write end label is used to constrain the structure.
-
Although the web page with the ending tag omitted can be quickly loaded, it may become your maintenance burden immediately.
For example, it may be painful to add a new row or column to a table without the end label. Although it can be solved by inserting indentation, it also offsets the work efficiency improved by not writing the end tag.
-
CSS and JS depend on complete elements
-
Although most browsers can display and render elements whose labels are not closed, they can take the initiative to close the labels to identify JS and CSS behavior.
If you are using a very precise layout with no end tag, it also means that you have no control over precision and robustness-you have the browser determine the end position of the tag, but sometimes it's great, sometimes it's awesome ......
-
The end tag cannot be omitted sometimes.
-
In HTML5,
</P>Labels can be omitted when they are directly followed by about 25 elements. But HTML5 contains more than 100 elements. Can you remember every situation where the end tag can be omitted? It may be easier to ignore them.
Optional HTML tags and their constraints
Note 1:The following table, unless otherwise stated,White space charactersInDocument,Before and after nodesBoth exist or ignore
2:"White space characters"Refers to space (U + 0020), tab (U + 0009), line feed (U + 000a), carriage return (U + 000d), and page feed (U + 000c ).
3:In the following table,Element"Indicates the DOM node with nodetype = 1)
Note 1:Omitting the start tag of an element does not mean that the start tag does not exist. It still exists implicitly. For example, an HTML document always has a root HTML element.<HTML>Does not appear in the document .)
NOTE 2:If the start tag contains any attribute definition, the start tag cannot be omitted.
| Save tags |
Omitted premise |
<HTML> |
- Its content cannot start with annotations.
|
</Html> |
- Current
HtmlAnnotations cannot appear after an element.
|
<Head> |
- It is empty
- Its content starts with an element.
|
</Head> |
- Current
HeadElement nextsibling cannot be a blank character or comment
|
<Body> |
- It is empty
- Its content cannot be blank characters, comments,
ScriptElement,StyleElement as the start
|
</Body> |
- Current
BodyAnnotations cannot appear after an element.
|
</LI> |
- In the current
LiThe element following the element isLi
- Current
LiThe element is the last child element of the parent element.
|
</DT> |
- In the current
DTThe element following the element isDTOrDd
|
|
- the elements following the current
DD element are DD or dt
- the current
DD element is the last child element of the parent element
|
|
- the elements following the current
P element are address , Article , and aside. , BLOCKQUOTE , dir , Div , and DL , fieldset , footer , form , H1 , and h2 , H3 , H4 , H5 , and H6 >, header , hgroup , HR , menu , and nav , ol , P , pre , and section , ul
Note 5: table is also in the specification, but in fact, its default display is table , and errors are also closed in Some browsers. Therefore, we generally consider that a constraint should be added, that is, the default display label should be block
- the current
P element is the last child element of the parent element, and the parent element cannot be A
|
</RT> |
- In the current
RTThe element following the element isRTOrRP
- Current
RTThe element is the last child element of the parent element.
|
</Rp> |
- In the current
RPThe element following the element isRTOrRP
- Current
RPThe element is the last child element of the parent element.
|
</Optgroup> |
- In the current
OptgroupThe element following the element isOptgroup
- Current
OptgroupThe element is the last child element of the parent element.
|
</Option> |
- In the current
OptionThe element following the element isOptionOrOptgroup
- Current
OptionThe element is the last child element of the parent element.
|
<Colgroup> |
- It cannot be empty
- Its content uses
ColElement is used as the start, and the elements before it cannot omit the end tag.Optgroup.
|
</Colgroup> |
- Current
OptgroupElement nextsibling cannot be a blank character or comment
|
</Thead> |
- In the current
TheadThe element following the element isTbodyOrTfoot
|
<Tbody> |
- It cannot be empty
- Its content starts with the tr element, and the element before it cannot omit the end tag.
Tbody,Thead,Tfoot.
|
</Tbody> |
- In the current
TbodyThe element following the element isTbodyOrTfoot
- Current
TbodyThe element is the last child element of the parent element.
|
</Tfoot> |
- In the current
TfootThe element following the element isTbody
- Current
TfootThe element is the last child element of the parent element.
|
</Tr> |
- In the current
TrThe element following the element isTr
- Current
TrThe element is the last child element of the parent element.
|
</TD> |
- In the current
TDThe element following the element isTDOrTh
- Current
TDThe element is the last child element of the parent element.
|
</Th> |
- In the current
ThThe element following the element isTDOrTh
- Current
ThThe element is the last child element of the parent element.
|
References:
Http://webdesign.about.com/od/htmltags/qt/optional-html-end-tags-when-to-include-them.htm
Http://www.w3.org/TR/html5/syntax.html#optional-tags
Http://www.w3.org/TR/html5/syntax.html#raw-text-elements