Chiri Vulpes
Chiri Vulpes

Reputation: 488

If nothing comes after the content of the last tag, does it have to be closed?

If nothing comes after the content of the last tag, does it have to be closed? For instance, having a large page which comes down to the final bits, which would be

        </div>
    </body>
</html>

I know the closing body and html tags aren't COMPLETELY necessary, at least in plain HTML, but what about closing a <div> right at the end? Is that necessary? Like, will certain browsers break or anything?

Upvotes: 0

Views: 118

Answers (3)

Jukka K. Korpela
Jukka K. Korpela

Reputation: 201568

The closing tags </body> and </html> are never necessary except in XHTML, and omitting them has never any effect on valid documents.

The closing tag </div> is always required for any div element, but omitting it at the end of a document makes browsers imply it, so a div element is created in the DOM as if the </div> end tag was there. This is being formalized in HTML5, section The HTML syntax, though the formulation there (despite being painfully detailed) does not explicitly specify this error processing. Instead, it says that upon encountering the end of file, all nodes in the stack of open elements are “popped off”. The intended meaning is that those elements are implicitly closed and added to the DOM, in stack order.

However, the lack of a </div> tag is classified as “parse error”. This means, according to HTML5 CR, that “The error handling for parse errors is well-defined [...], but user agents, while parsing an HTML document, may abort the parser at the first parse error that they encounter for which they do not wish to apply the rules described in this specification.” This is of course self-contradictory (error handling is not well-defined if an error may or may not abort parsing, according to the choice of the browser). No browser is known to abort parsing that way. Yet, omitting </div> would still be a pointless risk.

In XHTML, when processed as real XHTML and not as masqueraded as HTML (with Content-Type: text/html as opposite to specifying an XML type), lack of </div> for any div element is a well-formedness error. This means that the processing of the document is terminated, the document is not displayed at all, and an error message is shown in its stead.

Upvotes: 1

Andrew
Andrew

Reputation: 20071

Web browsers are designed to not break even when your markup is awful. They will display as best they can, so the page will display.

Whether or not your page layout looks wonky if you don't close that last div may depend on what styles are applied to the div. But most likely, you won't notice any difference.

Upvotes: 1

user2737761
user2737761

Reputation: 115

You should always close all tags.

Upvotes: 1

Related Questions