html - CSS 嵌套有序列表计数器操作

标签 html css menu html-lists css-counter

我想使用特定数字开始嵌套有序列表,同时保持嵌套列表项的正常编号(基于第一个起始数字)。我不会让人们走上错误的道路并展示我迄今为止尝试过的众多脚本版本,而是要展示我想要的最终状态并询问如何到达那里。

toc.html 这是一个目录,看起来像(使用嵌套的有序列表):

Table of Contents

1 Introduction
2 Assembly
   2.1 Preparation
      2.1.1 Space
      2.1.2 Tools
      2.1.3 Parts
   2.2 Assembly
      2.2.1 Build It
3 Use
   3.1 Defaults
   3.2 Customizations
      3.2.1 Safety
      3.2.1 Insanity
4 Trouble-Shooting
5 Reference

assembly.html 页面上,即 2 Assembly 从目录中指向的内容,我希望它看起来像这样:

Blah blah blah, fake latin goes here.

2 Assembly
   2.1 Preparation
      2.1.1 Space
      2.1.2 Tools
      2.1.3 Parts
   2.2 Assembly
      2.2.1 Build It

Body of this page, along with more fake latin, goes here.

use.html 上,我希望它看起来像:

Blah blah blah, fake latin goes here.

3 Use
   3.1 Defaults
   3.2 Customizations
      3.2.1 Safety
      3.2.1 Insanity

Body of this page, along with more fake latin, goes here.

如何仅使用 HTML 和 CSS 来实现此目的?我不需要 javascript 或 HTML 和 CSS 以外的任何内容。我的文件结构是:

/css/main.css
/toc.html
/assembly.html
/use.html
/trouble-shooting.html
/reference.html

最佳答案

li 元素声明为 block 元素。然后,您可以利用 conter-reset 属性并通过伪元素重新添加计数器:

ol {
  counter-reset: item;
}

li {
  display: block;
}

li:before {
  content: counters(item, ".") " ";
  counter-increment: item;
}
<ol>
  <li>Introduction</li>
  <li>Assembly
    <ol>
      <li>Preparation
        <ol>
          <li>Space</li>
          <li>Tools</li>
          <li>Parts</li>
        </ol>
      </li>
      <li>Assembly
        <ol>
          <li>Build It</li>
        </ol>
      </li>
    </ol>
  </li>
  <li>Use
    <ol>
      <li>Defaults</li>
      <li>Customizations
        <ol>
          <li>Safety</li>
          <li>Insanety</li>
        </ol>
      </li>
    </ol>
  </li>
  <li>Trouble-Shooting</li>
  <li>Reference</li>
</ol>

关于html - CSS 嵌套有序列表计数器操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71289169/

相关文章:

javascript - 如何在 Javascript 中插入带有 HTML 标记的 PHP 代码

html - 叠加边框图像

css - 使输入字段透明

html - 滚动时如何使链接的整个背景发生变化而不仅仅是文本?

python - 菜单要加多少?

php - 更改从数据库获取日期的格式

javascript - 如何使函数 (f1) 运行单独的函数 (f2),但 f2 使用 "this"参数?

html - 如何转义 HTML 实体?

html - 模糊图像的单面

wpf - WPF 应用程序中的菜单/工具栏颜色