Should a heading be inside or outside a <p>?

JamalDols picture JamalDols · Mar 27, 2013 · Viewed 17.7k times · Source

I have a div element and within that div, we have text between p elements.

I want to add a heading. Should it go inside or outside the p?

Which is better:

<p>This is text
    <h3>This is my h3</h3>
    More text
</p>
<p>another text<p>

or

<p>This is text<p>
<h3>This is my h3</h3>
<p>another text<p>

Answer

Jukka K. Korpela picture Jukka K. Korpela · Mar 27, 2013

It is impossible to put a heading element inside a p element in HTML markup, not just formally but because browsers implicitly terminate an open p element when they encounter a heading. So the question is meaningless: an element that cannot exist in a certain context cannot have any meaning (semantics) within that context.

Instead of using p to group text and headings together, you can use the div element or, with the usual caveats, HTML5 novelties like section and article.