83

From http://webdesign.about.com/od/htmltags/p/aadivtag.htm

In HTML 4, the DIV element cannot be inside another block-level element, like a P element. However, in HTML5 the DIV element can be found inside and can contain other flow content elements, like P and DIV.

I have something like this inside a form

<p> <label...> <input...> </p>

but when Rails auto-generates an error_explanation div wrapping the input, the one paragraph turns into two, and I see this in Firebug:

<p> <label...> </p> <div...> <input...> </div> <p> </p>

Also, if I just add a simple

<p> <div> test </div> </p>

the same issue occurs (JSFiddle) and it gets rendered in the DOM as

<p> </p> <div> test </div> <p> </p>

Why?


I later e-mailed the author of the article and she made the appropriate changes.

1
  • The link is broken: "404 Not Found. Not Found. The requested URL was not found on this server." Commented Mar 22, 2022 at 18:41

4 Answers 4

139

From the fine specification:

p – paragraph

[...]

Permitted contents

Phrasing content

And what is this Phrasing content?

Phrasing content:

Consists of phrasing elements intermixed with normal character data.

Normal character data is just that: unmarked up text. Phrasing elements are:

a or em or strong ... [a bunch of other elements none of which are div]

So, <p><div></div></p> is not valid HTML. Per the tag omission rules listed in the spec, the <p> tag is automatically closed by the <div> tag, which leaves the </p> tag without a matching <p>. The browser is well within its rights to attempt to correct it by adding an open <p> tag after the <div>:

<p></p><div></div><p></p>

You can't put a <div> inside a <p> and get consistent results from various browsers. Provide the browsers with valid HTML and they will behave better.

You can put <div> inside a <div> though so if you replace your <p> with <div class="p"> and style it appropriately, you can get what you want.

Your reference at about.com disagrees with the specification at w3.org. Your reference is misleading you.

5
  • How about br tags? Is it considered better practice to use DIV?
    – Steve
    Commented May 26, 2012 at 8:14
  • @Steve: Depends on the circumstances. I'd use a container to group elements and a <br> when I need a line-break within a group. Commented May 26, 2012 at 17:20
  • 3
    Note that HTML5 also requires the behavior of a </p> end tag resulting in an empty paragraph (although it likely only does so as a result of existing browser interop). See stackoverflow.com/questions/11570902/…
    – BoltClock
    Commented Mar 6, 2015 at 3:23
  • @BoltClock: Mark Amery added the "Per the tag omission rules listed in the spec, the <p> tag is automatically closed by the <div> tag, which leaves the </p> tag without a matching <p>" note, do we need more clarification? Maybe that whole paragraph could use a rewrite. Commented Mar 20, 2015 at 5:52
  • I think it looks OK. My comment was intended to clarify that not only is a browser well within its rights to correct the unmatched end tag, but it's required to do so. Perhaps it could be incorporated into the paragraph, but I don't think it needs a rewrite.
    – BoltClock
    Commented Mar 20, 2015 at 6:11
24

It is not permitted to put block elements inside a p-tag.

However, in practice, this is not actually entirely true. The actual display value is in fact entirely irrelevant; the only thing that is considered is what the default display value is for the tag, e.g. "block" for divs and "inline" for spans. Changing the display value will still make the browser prematurely close the p-tag.

However, this also works the other way around. You can style a span-tag to behave exactly like a div-tag, but it will still be accepted inside the p environment.

So instead of doing this:

<p>
   <div>test</div>
</p>

You can do this:

<p>
   <span style="display:block">test</span>
</p>

Just remember that the browser validates the content of the p environment recursively, so even something like this:

<p>
   <span style="display:block">
       <div>test</div>
   </span>
</p>

Will result in the following:

<p>
   <span style="display:block"></span>
</p>
<div>test</div>
<p>
</p>
2
  • 4
    You say <p><span>...</span></p> is still technically invalid HTML but it isn't. <span> is a phrasing element which is alowed inside of <p> and changing its display value does not invalidate the HTML. Here's another article which discusses this
    – Mandible79
    Commented Oct 20, 2014 at 11:57
  • Setting a span to display:block and putting it inside a p may well be an unidiomatic way to use HTML/CSS (although Jukka makes an intriguing case at stackoverflow.com/a/18933182/1709587 for why it may sometimes be justified). It is, however, entirely valid HTML. When the HTML spec talks about elements' permitted content, it cares only about tag types - not their styling.
    – Mark Amery
    Commented Feb 14, 2015 at 17:35
9

The webdesign.about.com page is simply wrong; they probably misunderstood the HTML5 drafts. Allowing DIV inside P would cause great confusion; I don’t think it has even ever been considered during HTML5 development.

If you try to use DIV inside P, the DIV start tag will implicitly close the P element. This probably explains the things you’ve seen.

To avoid the problem, do not use P if the content contains, or may contain, a DIV element. Use DIV instead.

2
  • How about br tags? Is it considered better practice to use DIV?
    – Steve
    Commented May 26, 2012 at 8:13
  • 1
    @Steve, yes, what about br tags? If you mean whether they may appear inside a p element, then yes, they can. The practical problem with br is that it does not turn a line to an element, making styling and scripting potentially problematic. Commented May 26, 2012 at 11:30
1

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.

<P> won't allow other element inside it. only <span> and <strong> element allowed.In <p></p> we can add only text related tags. refer this link to know more

Not the answer you're looking for? Browse other questions tagged or ask your own question.