Html 嵌套 <p> 不起作用而嵌套 <div> 会吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12015804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Nesting <p> won't work while nesting <div> will?
提问by nonopolarity
I usually won't nest <p>
like this:
我通常不会这样嵌套<p>
:
<p>The following:
<p>one</p>
<p>two</p>
</p>
and I will nest like that using <div>
instead. But today I used <p>
but it seems that both Emacs and Google Chrome both would consider the outer <p>
closed as soon as it see a new <p>
is started. (the DOCTYPE
is HTML 4.01 Strict).
我会像那样嵌套使用<div>
。但是今天我使用了<p>
但似乎 Emacs 和 Google Chrome 都会在<p>
看到新<p>
的启动后立即认为外部关闭。(这DOCTYPE
是 HTML 4.01 Strict)。
I thought <p>
is no more than a <div>
but just with some pre-defined margin and padding, but is it true that <p>
cannot be nested? If so, what rule says that it cannot?
我认为<p>
只不过是一个,<div>
但只是带有一些预定义的边距和填充,但是<p>
不能嵌套是真的吗?如果是这样,什么规则说它不能?
回答by
Because a paragraph is a paragraph .. and that's how HTML is defined (and HTML is notXML).
因为一个段落就是一个段落 .. 这就是 HTML 的定义方式(HTML不是XML)。
Any <p>
(or otherblock-level element) will implicitly closeany open <p>
.
Any <p>
(或其他块级元素)将隐式关闭任何 open <p>
。
Per 9.3.1 Paragraphs: the P elementof the HTML 4.01 specification:
根据9.3.1 段落:HTML 4.01 规范的 P 元素:
The P element represents a paragraph. It cannotcontain block-level elements (including P itself).
P 元素代表一个段落。它不能包含块级元素(包括 P 本身)。
Note that this is how the HTML is parsedand that even a <div>
would have implicitly closed the paragraph!
请注意,这就是HTML 的解析方式,即使是 a<div>
也会隐式关闭段落!
However, a <span>
with display:block;
would nothave closed the <p>
as a <span>
is nota block-level element.
然而,<span>
与display:block;
将不关闭了<p>
作为<span>
是不是一个块级元素。
That is, the CSS is irrelevantat this stage of the HTML processing and the CSS is irrelevantto the DOM/parser when determining if an element is a block-level element or not. Consider the case when CSS is applied dynamically or through a not-yet-loaded-stylesheet: the applied CSS does not alter the DOM.
也就是说,CSS是不相关的,在这个阶段的HTML处理和CSS是不相关的确定,如果一个元素是一个块级元素或不时对DOM /解析器。考虑动态应用 CSS 或通过尚未加载的样式表应用 CSS 的情况:应用的 CSS 不会改变 DOM。
While the HTML5 (working-draft) specification does not include the language above in the HTML4 specification, it does go on to define a paragraph as a containerfor phasing contentand further has a section on paragraphs.
虽然 HTML5(工作草案)规范没有在 HTML4 规范中包含上述语言,但它确实继续将段落定义为分阶段内容的容器,并进一步有一个关于段落的部分。
The accepted answer to List of HTML5 elements that can be nested inside P element?says that <p>
elements cannot nest in HTML5. The key phrase from the documentation is: "Runs of phrasing content [which does not include <p>
elements] form paragraphs". Furthermore, HTML5, trying to be backwards-compatible in many aspects, has a rationale on "Restrictions on content models and on attribute values":
可以嵌套在 P 元素中的 HTML5 元素列表的公认答案?说<p>
元素不能嵌套在 HTML5 中。文档中的关键短语是:“短语内容[不包括<p>
元素]形成段落的运行”。此外,HTML5 试图在许多方面向后兼容,在“内容模型和属性值的限制”上有一个基本原理:
Certain elements are parsed in somewhat eccentric ways (typically for historical reasons), and their content model restrictions are intended to avoid exposing the author to these issues.
某些元素的解析方式有些古怪(通常是出于历史原因),它们的内容模型限制旨在避免将作者暴露在这些问题中。
This behavior is referenced from a HTML5 WG wiki entry on flow content:
此行为引用自关于流内容的 HTML5 WG wiki 条目:
HTML5's restrictions on nesting of p elements and on what p elements may contain, are due to, quote: “peculiarities of the parser” that causes p to be auto-closed..
HTML5 对 p 元素的嵌套以及 p 元素可能包含的内容的限制是由于引用:“解析器的特殊性”导致 p 自动关闭..
回答by Antimony
From the HTML 4.01 specification section 9.3.1
The P element represents a paragraph. It cannot contain block-level elements (including P itself).
P 元素代表一个段落。它不能包含块级元素(包括 P 本身)。