ol嵌套必须用li包裹子列表,否则会导致语义断裂、编号错乱和样式失效;浏览器只认li为ol的合法子元素,跳过li直接嵌套ol会使外层ol自动闭合。

ol 嵌套必须用 li 包裹子列表,否则语义断裂、编号错乱、样式失效——这是绝大多数人写崩嵌套列表的根源。
为什么 ol 里直接放 ol 会失效
浏览器解析 ol 时只认 li 为合法子元素;如果在 ol 内部跳过 li 直接写另一个 ol,外层 ol 会自动闭合,导致后续内容脱离列表上下文。
常见错误现象:
- 编号从 1 重新开始(而不是延续父级)
- 子列表项顶到左边界,无缩进
- 屏幕阅读器跳过子列表,或播报顺序错乱
正确做法:每个嵌套层级都必须由 li 承载,子 ol 是 li 的子元素,不是父 ol 的兄弟。
li 内部嵌套 ol 的标准结构
以下结构能保证编号连续、缩进自然、语义完整:
立即学习“前端免费学习笔记(深入)”;
<ol start="34">
<li>Only live players playing in a set may be inside the boundaries of their half.</li>
<li>If any part of a live player touches a boundary line they are rendered out.</li>
<li>If any part of a live player touches a surface, object, or person outside of the boundary line on their team’s half of the court they are rendered out.
<ol type="a">
<li>That player may pass any balls they are carrying to any live player on their team.</li>
<li>That player may not intentionally touch any other balls.</li>
<li>That player may not intentionally obstruct any live players or ball retrievers.</li>
<li>That player may not intentionally obstruct any live balls thrown by the opposing team.</li>
</ol>
</li>
<li>Dead players must line up in the marked player return area in the order that they have been rendered out.</li>
</ol>
关键点:
-
start="34"控制外层起始编号,内部ol不影响该值 - 子
ol的type="a"仅作用于该层级,不污染外层 - 所有文本内容必须包裹在
li内,不能裸写在ol中
多级嵌套时编号格式不继承,需显式声明
HTML 原生 ol 不支持 1.1、1.1.1 这类复合编号;type 和 start 都是单层控制。若需此类效果,必须用 CSS 计数器手动实现。
例如要让第二级显示为 1.1、1.2,第三级为 1.1.1:
- 不能依赖
ol type="1"自动叠加,它只会输出 1、2、3 - 需用
counter-reset+counter-increment+::before重写编号逻辑 - 此时
value属性可配合计数器做初始化,但不可单独使用
原生嵌套只解决“结构分层”,不解决“编号样式融合”——这是最容易被忽略的分水岭。



















