Html ol/ul 应该在 <p> 里面还是外面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5681481/
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
Should ol/ul be inside <p> or outside?
提问by dynamic
Which is standard compliant between these two?
这两者之间哪个符合标准?
<p>Text text text ...
<ol>
<li>First element</li>
</ol>
</p>
<p>
Other text text ...
</p>
OR
或者
<p>
Text text text ...
</p>
<ol>
<li>First element</li>
</ol>
<p>
Other text text ...
</p>
回答by s4y
The short answer is that ol
elements are not legally allowed inside p
elements.
简短的回答是ol
元素在法律上是不允许的p
。
To see why, let's go to the spec! If you can get comfortable with the HTML spec, it will answer many of your questions and curiosities. You want to know if an ol
can live inside a p
. So…
要了解原因,让我们转到规范!如果您能适应 HTML 规范,它将回答您的许多问题和好奇心。您想知道ol
一个p
. 所以…
Categories:Flow content, Palpable content.
Content model:Phrasing content.
Categories:Flow content.
Content model: Zero or more liand script-supportingelements.
The first part says that p
elements can only contain phrasing content(which are “inline” elements like span
and strong
).
第一部分说p
元素只能包含短语内容(它们是“内联”元素,如span
和strong
)。
The second part says ol
s are flow content(“block” elements like p
and div
). So they can'tbe used inside a p
.
第二部分说ol
s 是流内容(像p
和这样的“块”元素div
)。所以它们不能在p
.
ol
s and other flow content
can be used in in some other elements like div
:
ol
s 和 otherflow content
可用于其他一些元素,例如div
:
Categories:Flow content, Palpable content.
Content model:Flow content.
回答by Quentin
The second. The first is invalid.
第二。第一个无效。
- A paragraph cannot contain a list.
- A list cannot contain a paragraph unless that paragraph is contained entirely within a single list item.
- 段落不能包含列表。
- 除非该段落完全包含在单个列表项中,否则列表不能包含段落。
A browser will handle it like so:
浏览器会像这样处理它:
<p>tetxtextextete
<!-- Start of paragraph -->
<ol>
<!-- Start of ordered list. Paragraphs cannot contain lists. Insert </p> -->
<li>first element</li></ol>
<!-- A list item element. End of list -->
</p>
<!-- End of paragraph, but not inside paragraph, discard this tag to recover from the error -->
<p>other textetxet</p>
<!-- Another paragraph -->
回答by hidden
GO here http://validator.w3.org/upload your html file and it will tell you what is valid and what is not.
去这里http://validator.w3.org/上传你的 html 文件,它会告诉你什么是有效的,什么是无效的。
回答by Kris Ivanov
actually you should only put in-line elements inside the p
, so in your case ol
is better outside
实际上,您应该只将内联元素放在 内p
,因此在您的情况下ol
最好放在外面
回答by David
<p>tetxetextex</p>
<ol><li>first element</li></ol>
<p>other textetxeettx</p>
Because both <p>
and <ol>
are element rendered as block.
因为<p>
和<ol>
都是元素呈现为块。