Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't the <p> tag contain a <div> tag inside it?

Tags:

html

xhtml

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?

like image 764
Henry H Miao Avatar asked Dec 06 '11 09:12

Henry H Miao


People also ask

Can a div be inside p tag?

The <div> tag can NOT be inside <p> tag, because the paragraph will be broken at the point, where the <div> tag is entered. To apply styles inside a paragraph use <span> tag, which is used with inline elements.

Can you have P tag inside of P tag?

No, a paragraph element may not contain other block elements. A paragraph tag is intended for a block of text. If your elements is a part of the text (and not block elements), it would be semantically correct, otherwise not.


Video Answer


1 Answers

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.

<!ELEMENT P - O (%inline;)*            -- paragraph --> 

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)."

like image 94
Colin Campbell Avatar answered Sep 20 '22 23:09

Colin Campbell