【问题标题】:Ordered list (HTML) lower-alpha with right parentheses?带右括号的有序列表(HTML)低字母?
【发布时间】:2009-10-27 16:23:00
【问题描述】:

有序列表的默认低字母列表类型使用点“.”。有没有办法使用右括号代替 a)... b) ..etc?

【问题讨论】:

  • 也许其中一个答案可以被选为正确的?...

标签: html css


【解决方案1】:

这是一个巧妙的解决方案。 (老实说,我对此感到惊讶。) CSS 有一个名为 counters 的东西,例如,您可以在其中设置每个标题上的自动章节编号。稍作修改即可为您提供以下内容;您需要自己整理填充等。

ol {
  counter-reset: list;
}
ol > li {
  list-style: none;
}
ol > li:before {
  content: counter(list, lower-alpha) ") ";
  counter-increment: list;
}
<span>custom list style type (v1):</span>
<ol>
  <li>Number 1</li>
  <li>Number 2</li>
  <li>Number 3</li>
  <li>Number 4</li>
  <li>Number 5</li>
  <li>Number 6</li>
</ol>

适用于所有现代浏览器和 IE9+(可能还有 IE8,但可能有问题)。

更新:我添加了子选择器以防止嵌套列表拾取父样式。 trejder 在 cmets 中也有一个好处,即列表项对齐也搞砸了。 article on 456bereastreet 有一个很好的解决方案,它涉及绝对定位计数器。

ol {
    counter-reset: list;
}
ol > li {
    list-style: none;
    position: relative;
}
ol > li:before {
    counter-increment: list;
    content: counter(list, lower-alpha) ") ";
    position: absolute;
    left: -1.4em;
}
<span>custom list style type (v2):</span>
<ol>
  <li>Number 1</li>
  <li>Number 2</li>
  <li>Number 3</li>
  <li>Number 4</li>
  <li>Number 5</li>
  <li>Number 6</li>
</ol>

Here is a jsFiddle 显示结果,包括嵌套列表。

【讨论】:

  • 你是对的,这在 IE6 中不起作用。但好消息是它适用于 Firefox 3.5.3。
  • 其实最好是:ol { counter-reset: list;有多个ol时,原来的就不行了。
  • 仅供参考,要获得编号列表而不是字母顺序,只需删除 , lower-alpha。所以content 的值是counter(list) ") ";
  • 让我补充一下,这不是 100% 实数。您可以看到多行项目的差异。在 普通 列表中(使用标准项目符号或数字),每一行都有相同的缩进,因此项目符号或数字看起来就像站在文本块之前。使用上述解决方案,每个 next 行从编号下方开始,并且不会稍微插入。这并没有改变这样一个事实,这是一个非常巧妙的解决方案! :>
  • 经过测试,我意识到这不尊重ol 元素的start 属性
【解决方案2】:

在最初的问题出现 10 多年后,标准(以及在某种程度上,实现)似乎已经赶上了。

CSS 现在提供::marker 伪类,可用于实现自定义列表标记:MDN

使用::marker 自动缩进li 的内容,没有任何黑客攻击。据 MDN 称,截至 2021 年 2 月,它在 Firefox、Chrome 和 Edge 中得到支持,在 Safari 中部分支持(不适用于此用例)。

.container {
  width: 400px;
}

ol.custom-marker {
  counter-reset: list;
}

ol.custom-marker > li {
  list-style: none;
  counter-increment: list;
}

ol.custom-marker.parens-after.decimal > li::marker {
  content: counter(list) ")\a0";
}

ol.custom-marker.parens-around.lower-roman > li::marker {
  content: "(" counter(list, lower-roman) ")\a0";
}
<div class='container'>
  <ol class='custom-marker parens-after decimal'>
    <li>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Eu sem integer vitae justo eget magna fermentum. Quis varius quam quisque id diam.</li>
    <li>Another list here
      <ol class='custom-marker parens-around lower-roman'>
        <li>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Eu sem integer vitae justo eget magna fermentum. Quis varius quam quisque id diam.</li>
        <li>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Eu sem integer vitae justo eget magna fermentum. Quis varius quam quisque id diam.</li>
      </ol>
    </li>
    <li>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Eu sem integer vitae justo eget magna fermentum. Quis varius quam quisque id diam.</li>
  </ol>
</div>

content 中的\a0&amp;nbsp;,因为::marker 不支持边距或填充。

【讨论】:

  • 这是最实用的答案。列表标记应该向右对齐,并且此方法正确覆盖了它。
【解决方案3】:

将它添加到 CSS 中会产生一些有趣的结果。很接近,但没有雪茄。

li:before {
    display: inline-block;
    width: 1em; 
    position: relative;
    left: -0.5em; 
    content: ')'
}

-- 已编辑以在 cmets 中包含 Iazel 的解决方案 -----

我已经完善了您的解决方案:

li {
    position: relative;
}
li:before {
    display: inline-block;
    width: 7px;
    position: absolute;
    left: -12px;
    content: ')';
    background-color: #FFF;
    text-align: center;
}

背景和position: absolute 成功了!

【讨论】:

  • 我已经完善了您的解决方案:li { position: relative; } li:before { display: inline-block; width: 7px; position: absolute; left: -12px; content: ')'; background-color: #FFF; text-align: center; } 背景和位置:absolute 成功了! :)
【解决方案4】:

基于 DisgruntledGoat 的回答,我根据需要对其进行了扩展以支持子列表和样式。在这里分享它以防它对某人有帮助。

https://jsfiddle.net/0a8992b9/ 输出:

(i)first roman
    (a)first alpha
    (b)second alpha
    (c)third alpha
    (d)fourth alpha
(ii)second roman
(iii)third roman
    (a)first alpha
    (b)second alpha

【讨论】:

  • +1 您为 Alpha 版进行了重置。这对我帮助很大。非常感谢。如果有人没有 alpha 类,他可以使用 ol[style*="list-style-type: lower-alpha;"]
  • 有没有办法做到这一点,同时将文本保留在用于代替项目符号的字母的右侧?即没有将文本换行到字母下方(例如 a) )而是换行到特定字母点的文本开始点,因为常规
  • 看起来
  • @sabliao 将text-indent 的负值添加到li 级别
  • 【解决方案5】:

    这适用于 IE7、FF3.6、Opera 9.64 和 Chrome 6.0.4:

    <ol start="a" type="a" style="font-weight: normal;">
    <li><span style="inline-block;margin-left: -9px !important; margin-left: -15px;">) &nbsp;</span> content for line number one;</li>
    <li><span style="inline-block;margin-left: -9px !important; margin-left: -15px;">) &nbsp;</span>  content for line number two;</li>
    <li><span style="inline-block;margin-left: -9px !important; margin-left: -15px;">) &nbsp;</span>  content for line number three;</li> 
    <li><span style="inline-block;margin-left: -9px !important; margin-left: -15px;">) &nbsp;</span>  content for line number four;</li>
    <li><span style="inline-block;margin-left: -9px !important; margin-left: -15px;">) &nbsp;</span>  content for line number five;</li>
    <li><span style="inline-block;margin-left: -9px !important; margin-left: -15px;">) &nbsp;</span>  content for line number six;</li>
    </ol>
    

    这是内联的,因为它是为电子邮件编码的,但要点是跨度充当内容块并将括号拉入负左区域,使其与列表编号对齐。两个边距是为了弥补IE7和FF的差异

    希望这会有所帮助。

    【讨论】:

    • 你确定这不应该是display:inline-block; 吗?
    • 这是一个 hack,因为当尝试将括号放在自动生成的“a”、“b”等字符旁边时,它取决于字体大小。如果你打算做这样的事情,你应该使用 list-style-type:none 并自己接管渲染整个“a)”,而不是试图只渲染括号。
    【解决方案6】:

    这似乎有效:

    ol {
      counter-reset: list;
      margin: 0;
    }
    
    ol > li {
      list-style: none;
      position: relative;
    }
    
    ol > li:before {
      counter-increment: list;
      content: counter(list, lower-alpha) ") ";
      position: absolute;
      left: -1.4em;
    }
    

    【讨论】:

      【解决方案7】:

      在 Firefox 和较新版本的 Chrome/Edge/Chromium 中,您可以使用 @counter-style 定义自己的计数器样式,并使用 prefixsuffix 属性来定义计数器之前/之后的内容。 According to MDN,Safari 仍不支持此功能(截至 2021 年 10 月)。

      @counter-style my-new-list-style {
        system: extends lower-alpha;
        suffix: ') ';
      }
      
      .container ol {
        list-style: my-new-list-style;
      }
      <div class="container">
        <ol>
          <li>One.</li>
          <li>Two!</li>
          <li>Three?</li>
          <li>Four...</li>
        </ol>
      </div>

      【讨论】:

        猜你喜欢
        相关资源
        最近更新 更多
        热门标签