0

They are both block elements. So why can I nest a <div> inside a <div>, but can't nest a <p> inside a <p>? (Well, I can, but the second <p> just starts a new block.)

I have read about the div tag and p tag, but I don't see anything there that explains it. If it is there, then it goes over my head.

For the record, I do not want to nest a p tags, I am just trying to understand the fundamentals more.

Dramal
  • 167
  • 2
  • 10
  • 2
    Because that's how the html spec is written: http://www.w3.org/TR/2014/REC-html5-20141028/grouping-content.html#the-p-element – Marc B Feb 03 '15 at 21:04
  • http://stackoverflow.com/questions/2226562/what-is-the-difference-between-p-and-div – j08691 Feb 03 '15 at 21:05
  • The title asks a (very vague) question, the body asks a completely different “why” question. Both are non-constructive. – Jukka K. Korpela Feb 03 '15 at 23:52

2 Answers2

2

Per your links (in the Permitted content section), <p> is allowed to contain phrasing content, while <div> is allowed to contain flow content (which includes the phrasing content element types).

enter image description here

On a practical level, browsers also typically apply margin to <p>.

ceejayoz
  • 176,543
  • 40
  • 303
  • 368
1

both are block elements, but <p> tags come predefined with some margins/padding. you can inspect both elements in any modern browser and see the default properties

funny thing is, in the name of consistency, all elements have redefined by things like bootstrap/boilerplate CSS files that give all elements the same starting look and feel so we no longer have to guess which tags have what default properties. Which sort of goes against the original HTML spec for the sake of our sanity

fnostro
  • 4,531
  • 1
  • 15
  • 23
  • You've glossed over the critical part of the question, IMO, which is "So why can I nest a `
    ` inside a `
    `, but can't nest a `

    ` inside a `

    `?"

    – ceejayoz Feb 03 '15 at 21:12
  • yes - I did, because there is no real world difference that matters except that html editors will complain about the nesting. The OP already knows not to do it - so what's left? – fnostro Feb 03 '15 at 21:16
  • Well, not only will the HTML editors complain, but browsers will render it in unexpected ways. OP wants to know why, and I'm generally happier working with programmers who ask why instead of blindly going "you just can't, ok?" – ceejayoz Feb 03 '15 at 21:17
  • agreed, with one caveat, find out why on your own time. I don't need a programmer that loses a week's development time finding out why on my dime especially when they already know not to do it. No offense to OP - you are absolutely correct in learning why and SO is the perfect place to do so. Just be mindful of project deadlines – fnostro Feb 03 '15 at 21:22