Is it necessary to write HEAD, BODY and HTML tags?

后端 未结 6 2090
清酒与你
清酒与你 2020-11-22 05:10

Is it necessary to write , and tags?

For example, I can make such a page:



        
6条回答
  •  醉话见心
    2020-11-22 05:51

    Contrary to @Liza Daly's note about HTML5, that spec is actually quite specific about which tags can be omitted, and when (and the rules are a bit different from HTML 4.01, mostly to clarify where ambiguous elements like comments and whitespace belong)

    The relevant reference is http://www.w3.org/TR/2011/WD-html5-20110525/syntax.html#optional-tags, and it says:

    • An html element's start tag may be omitted if the first thing inside the html element is not a comment.

    • An html element's end tag may be omitted if the html element is not immediately followed by a comment.

    • A head element's start tag may be omitted if the element is empty, or if the first thing inside the head element is an element.

    • A head element's end tag may be omitted if the head element is not immediately followed by a space character or a comment.

    • A body element's start tag may be omitted if the element is empty, or if the first thing inside the body element is not a space character or a comment, except if the first thing inside the body element is a script or style element.

    • A body element's end tag may be omitted if the body element is not immediately followed by a comment.

    So your example is valid HTML5, and would be parsed like this, with the html, head and body tags in their implied positions:

         
        
        Page Title
        
        
    
    
    
    Some html

    Note that the comment "this script will be in head" is actually parsed as part of the body, although the script itself is part of the head. According to the spec, if you want that to be different at all, then the and tags may not be omitted. (Although the corresponding and tags still can be)

提交回复
热议问题