81

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Steve
  • 4,446
  • 7
  • 35
  • 50

4 Answers4

137

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mu is too short
  • 426,620
  • 70
  • 833
  • 800
  • How about br tags? Is it considered better practice to use DIV? – Steve May 26 '12 at 08:14
  • @Steve: Depends on the circumstances. I'd use a container to group elements and a `
    ` when I need a line-break within a group.
    – mu is too short May 26 '12 at 17:20
  • 3
    Note that HTML5 also *requires* the behavior of a `` end tag resulting in an empty paragraph (although it likely only does so as a result of existing browser interop). See http://stackoverflow.com/questions/11570902/why-does-a-stray-p-end-tag-generate-an-empty-paragraph – BoltClock Mar 06 '15 at 03:23
  • @BoltClock: Mark Amery added the "Per the tag omission rules listed in the spec, the `

    ` tag is automatically closed by the `

    ` tag, which leaves the `` tag without a matching `

    `" note, do we need more clarification? Maybe that whole paragraph could use a rewrite.

    – mu is too short Mar 20 '15 at 05: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 Mar 20 '15 at 06:11
23

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>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Daniel Perván
  • 1,685
  • 13
  • 11
  • 4
    You say `

    ...

    ` is still technically invalid HTML but it isn't. `` is a phrasing element which is alowed inside of `

    ` and changing its display value does not invalidate the HTML. [Here's another article which discusses this](http://stackoverflow.com/questions/18930438/putting-a-block-level-span-element-inside-a-p-element)

    – Mandible79 Oct 20 '14 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 http://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 Feb 14 '15 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.

Jukka K. Korpela
  • 195,524
  • 37
  • 270
  • 390
  • How about br tags? Is it considered better practice to use DIV? – Steve May 26 '12 at 08: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. – Jukka K. Korpela May 26 '12 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

Clay H
  • 651
  • 9
  • 21
Suresh Patil
  • 121
  • 1
  • 5