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>
.
3
Answers
There is no official specification for designing your page layout. It is advised to use tags according to their semantic meaning to improve search results (SEO) and accessibility of a web page.
HTML standard is maintained by W3C and their specification for HTML elements can be found here. You could also reference RFC 1866 and 2854 if you are in need for more specifications.
The HTML Living Standard specifications maintained by the WHATWG states that the content model of the
<p>
element is limited to phrasing content.An element’s content model is a description of what content must be included as children and descendants of the element.
Phrasing content is the text of the document, as well as elements that mark up that text at the intra-paragraph level. Note that the
<div>
element is not among the elements categorized as phrasing content—therefore they cannot be validly placed within a paragraph.Content model of
p
does not allowdiv
Relevant statement in HTML specs states that The
p
element 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
div
nested 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
p
element" directive.