在 html 的段落中嵌套列表

发布于 2024-09-08 07:10:45 字数 467 浏览 9 评论 0原文

似乎(严格)html不允许在

中嵌套任何非内联元素,但是我应该如何渲染包含列表的段落(经常发生的事情在自然文本中)。我见过三个答案,但似乎都不满意:

  • 列表不应出现在段落内。 (我不打算进行文化辩论,但我当然希望 html 不会成为决定写作风格的地方。)
  • 将文本分成一个段落,然后是一个列表,然后是第二个段落后文。这看起来很糟糕,因为现在我有

    块,它们是段落的部分,这对于试图朝着更语义化的方向移动的标记来说似乎很糟糕。

  • 使用一些
    的虚假段落 - 这看起来是绕过整个事情并放弃使用具有正确文本语义的标记的糟糕方法。

还有其他方法可以做到这一点在语义上是正确且有效的吗?

It seems that (strict) html doesn't allow nesting any non-inline elements inside a <p>, but then how am I supposed to render a paragraph that contains a list (something that occurs often in natural texts). I've seen three answers that all seem unsatisfying:

  • Lists should not happen inside paragraphs. (I'm not going to go into a cultural debate, but I certainly hope that html is not going to become the place to dictate writing style.)
  • Separate the text into a paragraph, then a list, and then a second paragraph with the post-text. This looks bad because now I have <p> chunks that are parts of paragraphs, and that seems bad for a markup that is trying to to move in a more semantic direction.
  • Fake paragraphs using some <div class="mypara"> — which looks like a bad way to bypass the whole thing and give up on using the markup with the proper semantics for text.

Is there some other way to do this that is semantically correct and valid?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

风筝有风,海豚有海 2024-09-15 07:10:45

截至最新工作草案,HTML 5 中的段落被定义为只能包含短语元素的流程元素。列表也被定义为流程元素,并且不能包含在段落中。无论您认为段落的定义应该是什么,这都是 HTML 中该术语的正式定义,我认为它不太可能改变。

除了您提到的两种可能性之外,您还可以考虑两种可能性:

  • 如果适用,请考虑使用比段落更广泛的流程元素,例如 sectionnav页眉页脚文章
  • 使用混合方法:使用您选择的 div 包装 p – ol – p 序列,该 div 对集合应用常见格式。

div 而言,HTML 5 规范明确建议它应该是“最后的手段”元素,因为它不像其他 HTML 元素那样具有语义含义,并且可能不会作为替代用户代理中的用户友好型。根据这个建议,如果语义对您很重要,我不会以牺牲 p 为代价而使用 div 来表示正文。然而,它有助于确保多个段落的使用不会在视觉上变得过于混乱。

Paragraphs in HTML 5, as of the latest working draft, are defined as flow elements which can contain phrasing elements only. Lists are also defined as flow elements, and may not be enclosed in paragraphs. Whatever you think the definition of paragraph should be, this is the formal definition of the term in HTML, and I think it's unlikely to change.

There are two possibilities that you might consider besides the two you've mentioned:

  • Consider reaching for a more broad-reaching flow element than paragraph if it applies, such as section, nav, header, footer, or article.
  • Use a hybrid approach: wrap the p – ol – p sequence with a div of your choosing that applies common formatting to the set.

As far as div goes, the HTML 5 spec clearly recommends that it should be a "last resort" element because it doesn't bear semantic meaning in the way that other HTML elements do, and may not be as user-friendly in alternative user agents. Going by this advice, I wouldn't use div at the cost of p for body text if semantics are important to you. However, it can be useful in making sure that the use of multiple paragraphs does not get too visually choppy.

走走停停 2024-09-15 07:10:45

这一切背后是否有视觉风格的原因?换句话说,当您有类似以下内容时,它的视觉呈现方式是否不理想?

<p>Some paragraph text.</p>
<ul>
    <li>First list item</li>
    <li>Second list item</li>
    <li>Third list item</li>
</ul>
<p>Another paragraph.</p>

如果问题是 P 之后和 UL 之前的余量太多,那么您可以尝试像这样的相邻同级选择器来补偿:

p + ul { margin-top: -1em; }

Is there a visual style reason behind all this? In other words when you have something like the following, is it visually rendering in a way that you do not find ideal?

<p>Some paragraph text.</p>
<ul>
    <li>First list item</li>
    <li>Second list item</li>
    <li>Third list item</li>
</ul>
<p>Another paragraph.</p>

If the problem is too much margin after the P and before the UL, then you can try an adjacent sibling selector like this to compensate:

p + ul { margin-top: -1em; }
楠木可依 2024-09-15 07:10:45

我不认为 html

标签是为了控制实际写作风格的习惯用法。这是一种以有序方式显示文本块的简单方法。我不认为它是为了模拟印刷材料。您必须为此使用 Div。

I don't think the html <p> tag is meant to control the idioms of actual writing styles. It is simpy a way to display a chunk of text in an orderly manner. I don't believe it is meant to simulate printed material. You will have to use a Div for this one.

感性不性感 2024-09-15 07:10:45

你的问题的答案显然是否定的,因为规格说明你不能这样做。和你一样,我喜欢维护语义,所以我通常选择你的第二个选项(段落、列表、另一段)。然而,在我的脑海中,我想不出任何列表实际上不会破坏文本流的文本,所以看起来在列表之后开始一个新段落不会有什么坏处。

The answer to your question is obviously no, because the specs say you can't do it. Like you, I like to maintain semantics, so I usually go with your 2nd option (paragraph, list, another paragraph). However, off the top of my head, I can't think of any text where the list doesn't actually break the flow of text, so it seems that starting a new paragraph after a list can't hurt.

热情消退 2024-09-15 07:10:45

我想这取决于你认为什么是“段落”。对我来说很明显,HTML 设计者决不认为列表是段落的一部分。虽然我认为这两种解释都可以争论,但我认为该规范是正确的。

虽然列表并不代表思路的突破,但它确实代表了声音或思维方法的改变,我认为这值得一个新的语义容器。

同样,段落代表一篇文章中的逻辑分区。列表表示一系列逻辑分区,虽然可以想象一个逻辑分区可能会被划分为更多分区,但简单地创建一个新分区更有意义(特别是对于语音/方法中如此大的中断)。

语义在很大程度上可能是主观的。那很好。然而,在这种情况下,我认为 HTML 规范正确地表示了语义。

I suppose it depends on what you consider to be a "paragraph". It is obvious to me that the designers of HTML in no way consider a list to be part of a paragraph. And while I think an argument could be made for either interpretation, I'm of the opinion that the spec is correct.

While a list doesn't represent a break in line of thought, it certainly represents a change in voice or method of thought, which I believe merits a new semantic container.

In the same vein, a paragraph represents a logical partition in a piece of writing. A list represents a sequence of logical partitions, and while it is imaginable that one logical partition may be then divided into more partitions, it makes more sense to simply create a new partition (especially for such a large break in the voice/method).

Semantics can be largely subjective. And that's fine. However, in this case, I'm of the opinion that the HTML spec correctly represents the semantics.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文