巢在html中的段落中列出 [英] Nest lists in paragraphs in html

查看:91
本文介绍了巢在html中的段落中列出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看起来(严格的)html不允许在< p> 中嵌套任何非内联元素,但是我该如何呈现段落包含一个列表(在自然文本中经常出现的东西)。我见过三个答案,看起来都不令人满意:


  • 列表不应该出现在段落内。 (我不打算进入文化辩论,但我当然希望html不会成为写作风格的地方。)

  • 将文本分成段落,然后是一个列表,然后是带有后文的第二段。这看起来很糟糕,因为现在我有段落的 c>< p> 块,这对于标记尝试使用一些< div class =mypara> - 这看起来像一个假的段落坏的方法来绕过整个事情,并放弃使用标记正确的文本语义。



是否有其他方式做到这一点在语义上是正确的和有效的?

解决方案

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



有两种可能性,你可能会考虑除了你提到的两个:


  • 如果适用的话,考虑到达比段落更广泛的流元素,例如部分 nav 标头 footer article 。

  • 使用混合方法:将
  • ul>

    div 而言,HTML 5规范明确建议它应该是最后一招元素,因为它不像其他HTML元素那样具有语义含义,并且在替代用户代理中可能不像用户友好。按照这个建议,如果语义非常重要,我不会使用 div ,代价是 p 您。然而,它可以用于确保多段落的使用不会太过于视觉上的不连贯。


    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?

    解决方案

    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.

    这篇关于巢在html中的段落中列出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆