Why does a stray end tag generate an empty paragraph?

That it is required is documented in HTML5. See http://w3c.github.io/html/syntax.html#the-in-body-insertion-mode and search down for An end tag whose tag name is "p" and it says:

If the stack of open elements does not have an element in button scope
with the same tag name as that of the token, then this is a parse
error; act as if a start tag with the tag name “p” had been seen, then
reprocess the current token.

Which translated into English means create a p element if the </p> tag can’t be matched with an existing <p> tag.

Why it is so, is harder to ascertain. Usually, this is because some browser in the past caused this to happen as a bug, and web pages came to rely on the behaviour, so other browsers had to implement it too.

Leave a Comment