【问题标题】:HTML/CSS multilevel nested lists numbering [duplicate]HTML / CSS多级嵌套列表编号[重复]
【发布时间】:2018-08-09 15:22:16
【问题描述】:

有没有办法使用直接的 HTML 和 CSS 列表(<ul><ol>)实现以下编号?

1. Link 1
2. Link 2
3. Link 3
    3.1. Link 3.1
    3.2. Link 3.2
    3.3. Link 3.3
4. Link 4
    4.1. Link 4.1
        4.1.1 Link 4.1.1
        4.1.2 Link 4.1.2
5. Link 5

提前致谢!

【问题讨论】:

标签: html css html-lists nested-lists


【解决方案1】:

你可以使用 CSS counters:

ol {
    counter-reset: section;
    list-style-type: none;
}

li:before {
    counter-increment: section;
    content: counters(section, ".") ". Link " counters(section, ".") " ";
}

工作演示 (also on JSBin):

ol {
  counter-reset: section;
  list-style-type: none;
}

li:before {
  counter-increment: section;
  content: counters(section, ".") ". Link " counters(section, ".") " ";
}
<ol>
  <li></li>
  <li></li>
  <li>
    <ol>
      <li></li>
      <li></li>
      <li></li>
    </ol>
  </li>
  <li>
    <ol>
      <li>    
        <ol>
        <li></li>
        <li></li>
        </ol>
      </li>
    </ol>
  </li>
  <li></li>
</ol>

【讨论】:

  • 我应该注意:before:after 是伪元素,并且应该使用双冒号(::before),即使这在IE8 中不起作用。
  • 太棒了!非常感谢!
  • @Bart CSS CountersCSS2.1 的一部分,在IE8 as well 中工作。但是关于伪元素:早期版本的网络浏览器(IE8、Opera4-7)对 pseudo-elementspseudo-classes 使用单个冒号,因此我们' re 习惯使用单一颜色来支持旧浏览器。
  • 这样的解决方案似乎存在问题 - 数字与下面的文本对齐。这看起来不太好。
  • @Ivan 您可以通过使 ol 位置相对和 li:before 位置绝对 - 并根据需要定位它来创建一个解决方法。
【解决方案2】:
<style>
OL { counter-reset: item }
LI { display: block }
LI:before { content: counters(item, ".") " "; counter-increment: item }
</style>
<html>
<body>

<ol>
  <li>Link 1</li>
  <li>Link 2</li>
  <li>Link 3
<ol >
  <li>Link 3.1</li>
  <li>Link 3.2</li>
  <li>Link 3.3
<ol >
  <li>Link 3.3.1</li>
  <li>Link 3.3.2</li>
  <li>Link 3.3.3</li>
</ol>
</li>
</ol>
</li>
</ol>

</body>
</html>

【讨论】:

    猜你喜欢
    • 2018-04-05
    • 2012-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-30
    相关资源
    最近更新 更多