I am doing a task, where I need to correct given HTML file with some content and write a reason for this correction.
And there are lines of code that I know must be corrected, but I can't find any official documentation, which would clearly state that such nesting is wrong.
<p class = "par">
<div>...</div>
</p>
So my question is where could I find an official statement in official specification or other document, that clearly stated that?
I have found some articles, mentioning that clearly, but I need something more "official". At most what I could find is:
Tag omission in text/html: A p element's end tag can be omitted if the p element is immediately followed by an address, article, aside, blockquote, details, div, dl, fieldset, figcaption, figure, footer, form, h1, h2, h3, h4, h5, h6, header, hgroup, hr, main, menu, nav, ol, p, pre, search, section, table, or ul element, or if there is no more content in the parent element and the parent element is an HTML element that is not an a, audio, del, ins, map, noscript, or video element, or an autonomous custom element.
As you could see, it doesn't clearly stated anything about not allowing <div> to be nested inside <p>.
Content model of
pdoes not allowdivRelevant statement in HTML specs states that The
pelement Content model is "Phrasing content".The "Phrasing content" is defined set of elements that does not include
div.<p><div></div></p>does not in fact "work"Another quite hefty argument revealing flaws in such HTML code could be pointing out what it effectively produces and that it most probably is not what any author really intends:
Resulting DOM structure does not produce
divnested inside paragraph, but it produces paragraph followed by div and then second empty paragraph.Various proofs:
Hixie's DOM viewer
Feeding Ian Hickson's "Live DOM Viewer" with minimalistic document
reveals resulting structure like so:
Body element has three direct children: paragraph with class "par", div, and second paragraph without class.
Validator
Alternatively, it is always advisable to consult W3C Markup validator for errors. In this case it will point out the "stray" closing tag:
Local demo
Or you may check it yourself with devtools DOM inspector or make simple demonstration with CSS "probes":
Why
Why is that so is specified in "Parsing HTML documents" in these two rules:
1. Loosely paraphrased: Any "block-level" element's start tag implicitly closes opened
p.2. Loosely paraphrased: "stray"
</p>in HTML always produces empty paragraph without attributes.Both rules can be found in backreferences of "close a
pelement" directive.