【问题标题】:What's the right way to put lists within a paragraph in HTML5?在 HTML5 的段落中放置列表的正确方法是什么?
【发布时间】:2012-04-06 00:07:27
【问题描述】:

我得到了为什么 <p> 元素不能包含 <ul> 元素的技术解释。不幸的是,当这是内容的结构时,这对我没有帮助。

在meta中,我都不懂

  • 语义解释,也不
  • 推荐的结构

对于我实际上想要显示带有正常内容的“内联”项目列表的情况。

This page 建议将列表与段落分开:

<p>For instance, this fantastic sentence has bullets relating to</p>
<ul>
 <li>wizards,
 <li>faster-than-light travel, and
 <li>telepathy,
</ul>
<p>and is further discussed below.</p>

或为段落使用替代标签:

<div>For instance, this fantastic sentence has bullets relating to
<ul>
 <li>wizards,
 <li>faster-than-light travel, and
 <li>telepathy,
</ul>
and is further discussed below.</div>

但我觉得这两个都是可怕的骇客,是对 HTML5 所谓的语义内容的嘲弄。

在第一种情况下,&lt;p&gt; 标签在语义上有何意义?更不用说如果我想消除列表周围的间距(然后如果我有其他列表确实是单独的内容块 - 哦!)正确设置样式的实际困难!

在第二种情况下,如果我需要 &lt;div&gt; 表示“这是一段内容”,我不妨完全放弃 &lt;p&gt;,而不必根据内容来回切换出现在段落中。

ISTM 认为最具语义意义的版本可能是这样的:

<p>For instance, this fantastic sentence has bullets relating to
 <span class="li">wizards,</span>
 <span class="li">faster-than-light travel, and</span>
 <span class="li">telepathy,</span>
and is further discussed below.</p>

将 span 样式设置为块或列表项(谁知道还有哪些其他调整)以获得正确的渲染。实际上,这听起来非常令人恼火。

对于渲染这种内容有什么好的推荐吗?

编辑

看起来,这实际上并不难设计。我不确定是否有办法获取所有浏览器默认值,但我可以复制默认列表样式(至少在 OS X Chrome 上):

span.li {
  display: list-item;
  list-style-type: disc;
  margin-left: 40px;
}

【问题讨论】:

  • 在此处跟进我自己的帖子,由于a longstanding bug,Firefox 中编号列表的样式化跨度技术失败。

标签: html html-lists


【解决方案1】:

对我来说,这是一个列表炎的例子,或者将每个事物序列都视为一个需要标记的列表。

我的第一种方法是将您拥有的内容标记为:

<p>For instance, this fantastic sentence has bullets relating to wizards, 
faster-than-light travel, and telepathy, and is further discussed below.</p>

如果您需要对这些项目应用通用样式,则适当的样式挂钩是 &lt;span&gt;,就像您在最后一个示例中所做的那样。不过,我不会使用 li 作为类名;每个文本片段是否构成一个列表项并不是很有意义。在不了解页面的整体内容以及如何在上下文中使用它的情况下选择正确的类名总是很困难的,但topic 可能是一个合适的选择。所以加价就变成了

<p>For instance, this fantastic sentence has bullets relating to <span
class="topic">wizards</span>, <span class="topic">faster-than-light 
travel</span>, and <span class="topic">telepathy</span>, and is further 
discussed below.</p>

【讨论】:

  • 好的,这是有道理的。我唯一的评论/问题是,如果您像这样标记单个项目,但使用我在上面添加的样式,则标签之间的内容的渲染会中断(例如,“wizards”和“faster-”之间的逗号than-light travel")——因为它们不是列表项的一部分,所以它们创建了一个新的段落内容行。不确定是否有另一种方式来指示适用于此的样式。
【解决方案2】:

在我看来,如果您使用ul 或ol,您实际上是在结束它之前的段落(或其他元素),除非您正在嵌套列表。如果它真的是一个句子并且这些项目是内联的(并且在你提议的情况下,我认为所有这些都是一个句子),那么我只需在 &lt;p&gt; 中列出它们,并用逗号作为分隔符:

<p>For instance, this fantastic sentence has bullets relating to:
wizards, faster-than-light travel, and telepathy,
and is further discussed below.</p>

如果你真的想要项目符号,也许标题会更合适:

<h2>For instance, this fantastic heading has bullets relating to:</h2>
<ul>
 <li>wizards,
 <li>faster-than-light travel, and
 <li>telepathy,
</ul>

【讨论】:

  • 但这是我关于语义的问题 - 这不是标题,它是一个简单的句子,其中包含我想用项目符号呈现的项目列表。 W3C 似乎声称这使它不是一个有效的句子,但是
猜你喜欢
  • 1970-01-01
  • 2015-09-05
  • 2022-07-11
  • 1970-01-01
  • 1970-01-01
  • 2019-03-09
  • 1970-01-01
  • 2022-12-20
  • 2014-03-18
相关资源
最近更新 更多