As far as I know, this is right:
<div>
<p>some words</p>
</div>
But this is wrong:
<p>
<div>some words</div>
</p>
The first one can pass the w3c validator (xhtml 1.0), but the second can't. I know that nobody will write code like the second one. I just want know why. And what about other tags' containment relationship?
because div tag has higher precedence then p tag. p tag represents for paragraph tag whether div tag represents document tag. you can write many paragraphs in a document tag but you can't write a document in a paragraph. Same a DOC file.
After the X HTML the conventions has be changed not its a mixture of conventions of XML and HTML so that is why the second approach is wrong and w3c validator accepts the things correct that are according to the standards and conventions .
An authoritative place to look for allowed containment relations is the HTML spec. See, for example, http://www.w3.org/TR/html4/sgml/dtd.html. It specifies which elements are block elements and which are inline. For those lists, search for the section marked "HTML content models".
For the P element, it specifies the following, which indicates that P elements are only allowed to contain inline elements.
This is consistent with http://www.w3.org/TR/html401/struct/text.html#h-9.3.1, which says that the P element "cannot contain block-level elements (including P itself)."
In short, it is impossible to place a
<div>
element inside a<p>
in the DOM because the opening<div>
tag will automatically close the<p>
element.According to HTML5, the content model of
div
elements is flow contentThat includes
p
elements, which can only be used where flow content is expected.Therefore,
div
elements can containp
elements.However, the content model of
p
elements is Phrasing contentThat doesn't include
div
elements, which can only be used where flow content is expected.Therefore,
p
elements can't containdiv
elements.Since the end tag of
p
elements can be omitted when thep
element is immediately followed by adiv
element (among others), the followingis parsed as
and the last
</p>
is an error.