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

末鹿安然 提交于 2019-11-26 06:46:57

问题


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>

回答1:


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.




回答2:


According to standards a Paragraph should not contain other block elements (including paragraphs and headers). As indicated in other answers, to group a header with a paragraph you can use the div tag.

You can find a lot of information about this subject also on w3c's website: http://www.w3.org/TR/html401/struct/global.html#block-inline



来源:https://stackoverflow.com/questions/15656785/should-a-heading-be-inside-or-outside-a-p

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!