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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 08:02:00  来源:igfitidea点击:

Should ol/ul be inside <p> or outside?

html

提问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 olelements are not legally allowed inside pelements.

简短的回答是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 olcan live inside a p. So…

要了解原因,让我们转到规范!如果您能适应 HTML 规范,它将回答您的许多问题和好奇心。您想知道ol一个p. 所以…

4.5.1 The pelement:

Categories:Flow content, Palpable content.
Content model:Phrasing content.

4.5.1p元素

类别:流内容可触知的内容
内容模型:短语内容



4.5.5 The olelement:

Categories:Flow content.
Content model: Zero or more liand script-supportingelements.

4.5.5ol元素

类别:流内容
内容模型:零个或多个li脚本支持元素。

The first part says that pelements can only contain phrasing content(which are “inline” elements like spanand strong).

第一部分说p元素只能包含短语内容(它们是“内联”元素,如spanstrong)。

The second part says ols are flow content(“block” elements like pand div). So they can'tbe used inside a p.

第二部分说ols 是流内容(像p和这样的“块”元素div)。所以它们不能p.



ols and other flow contentcan be used in in some other elements like div:

ols 和 otherflow content可用于其他一些元素,例如div

4.5.13 The divelement:

Categories:Flow content, Palpable content.
Content model:Flow content.

4.5.13div元素

类别:流内容可触知的内容
内容模型:流内容

回答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 olis 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>都是元素呈现为块。