Do browsers automatically insert missing HTML tags?

前端 未结 3 846
-上瘾入骨i
-上瘾入骨i 2020-12-20 05:06

I\'m working with a web page the translates content from XML to HTML and then displays it on the HTML template page. I have a single page where some elements are getting mi

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-20 05:26

    Missing tags are treated differently depending on tag. When HTML specifications allow end tag omission, e.g. for

    and , all browsers imply them and are even required to do so. The situation is different for invalid end tag omission. For example,
    may never be omitted. What browsers actually do is that they imply the closing
at the very end of the document body. This usually means that the div element is interpreted as containing much more than intended, and this may have drastic effects if there are style sheets that format the element (or scripts that process it). For text-level elements like em, processing is more complicated: an end tag is implied when the end of an enclosing block element is encountered but the text-level element is opened again at the start of the next block! This is described and other specialties are presented in section 8.2.8 An introduction to error handling and strange cases in the parser of the HTML5 spec.

All this applies to HTML syntax. When using XHTML syntax and the browser has been instructed to process the data as being of an XML content type such as application/xhtml+xml, no end tag omission is tolerated: the first omitted end tag, being a well-formedness violation, is treated as a fatal error. This means that document content is not shown at all; an error message is shown to the user instead. This is very rare situation on web pages, since there is seldom a good reason to use “real XHTML” (i.e. XHTML processed by XML rules).

The conclusion is that you should generate valid HTML and at least in some tests validate it, using a suitable validator like http://validator.w3.org.

提交回复
热议问题