dir*_*irk 69 html css html-lists
我使用嵌套计数器和范围来创建有序列表:
ol {
counter-reset: item;
padding-left: 10px;
}
li {
display: block
}
li:before {
content: counters(item, ".") " ";
counter-increment: item
}
Run Code Online (Sandbox Code Playgroud)
<ol>
<li>one</li>
<li>two</li>
<ol>
<li>two.one</li>
<li>two.two</li>
<li>two.three</li>
</ol>
<li>three</li>
<ol>
<li>three.one</li>
<li>three.two</li>
<ol>
<li>three.two.one</li>
<li>three.two.two</li>
</ol>
</ol>
<li>four</li>
</ol>
Run Code Online (Sandbox Code Playgroud)
我期待以下结果:
1. one
2. two
2.1. two.one
2.2. two.two
2.3. two.three
3. three
3.1 three.one
3.2 three.two
3.2.1 three.two.one
3.2.2 three.two.two
4. four
Run Code Online (Sandbox Code Playgroud)
相反,这就是我所看到的(错误的编号):
1. one
2. two
2.1. two.one
2.2. two.two
2.3. two.three
2.4 three <!-- this is where it goes wrong, when going back to the parent -->
2.1 three.one
2.2 three.two
2.2.1 three.two.one
2.2.2 three.two.two
2.3 four
Run Code Online (Sandbox Code Playgroud)
我不知道,有谁看到它出错了?
这是一个JSFiddle:http://jsfiddle.net/qGCUk/2/
Zol*_*oth 79
取消选中"规范化CSS" - http://jsfiddle.net/qGCUk/3/ 使用的CSS重置默认所有列表边距和填充为0
更新 http://jsfiddle.net/qGCUk/4/ - 你必须在你的主要包括你的子列表<li>
ol {
counter-reset: item
}
li {
display: block
}
li:before {
content: counters(item, ".") " ";
counter-increment: item
}
Run Code Online (Sandbox Code Playgroud)
<ol>
<li>one</li>
<li>two
<ol>
<li>two.one</li>
<li>two.two</li>
<li>two.three</li>
</ol>
</li>
<li>three
<ol>
<li>three.one</li>
<li>three.two
<ol>
<li>three.two.one</li>
<li>three.two.two</li>
</ol>
</li>
</ol>
</li>
<li>four</li>
</ol>
Run Code Online (Sandbox Code Playgroud)
Mos*_*tov 50
使用此样式仅更改嵌套列表:
ol {
counter-reset: item;
}
ol > li {
counter-increment: item;
}
ol ol > li {
display: block;
}
ol ol > li:before {
content: counters(item, ".") ". ";
margin-left: -20px;
}
Run Code Online (Sandbox Code Playgroud)
Dr.*_*eon 14
看一下这个 :
你的问题似乎已得到解决.
什么显示给我(在Chrome和Mac OS X下)
1. one
2. two
2.1. two.one
2.2. two.two
2.3. two.three
3. three
3.1 three.one
3.2 three.two
3.2.1 three.two.one
3.2.2 three.two.two
4. four
Run Code Online (Sandbox Code Playgroud)
代替 :
<li>Item 1</li>
<li>Item 2</li>
<ol>
<li>Subitem 1</li>
<li>Subitem 2</li>
</ol>
Run Code Online (Sandbox Code Playgroud)
做:
<li>Item 1</li>
<li>Item 2
<ol>
<li>Subitem 1</li>
<li>Subitem 2</li>
</ol>
</li>
Run Code Online (Sandbox Code Playgroud)
我认为这些答案使问题过于复杂化。如果您不需要支持 Internet Explorer,那么解决方案很简单:
ol > li::marker { content: counters(list-item, '.') '. '; }
Run Code Online (Sandbox Code Playgroud)
<ol>
<li>one</li>
<li>two</li>
<ol>
<li>two.one</li>
<li>two.two</li>
<li>two.three</li>
</ol>
<li>three</li>
<ol>
<li>three.one</li>
<li>three.two</li>
<ol>
<li>three.two.one</li>
<li>three.two.two</li>
</ol>
</ol>
<li>four</li>
</ol>
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请参阅MDN CSS 参考网站上的::marker
CSS 伪元素页面和使用 CSS 计数器页面。
小智 7
这是一个很好的解决方案!使用一些额外的CSS规则,您可以将其格式化,就像MS Word大纲列表中挂起的第一行缩进:
OL {
counter-reset: item;
}
LI {
display: block;
}
LI:before {
content: counters(item, ".") ".";
counter-increment: item;
padding-right:10px;
margin-left:-20px;
}
Run Code Online (Sandbox Code Playgroud)
这是一个更简单和标准的解决方案,用于增加数字并保留末尾的点。
\n即使你的 CSS 正确,如果你的 HTML 不正确,它也不会工作。见下文。
ol {\n counter-reset: item;\n}\nol li {\n display: block;\n}\nol li:before {\n content: counters(item, ". ") ". ";\n counter-increment: item;\n}\n
Run Code Online (Sandbox Code Playgroud)\nol {\n counter-reset: item;\n li {\n display: block;\n &:before {\n content: counters(item, ". ") ". ";\n counter-increment: item\n }\n }\n}\n
Run Code Online (Sandbox Code Playgroud)\n如果添加子项,请确保它位于父项下li
。
注意家长li
和孩子ol li
是单独的,这是行不通的。
ol {\n counter-reset: item;\n li {\n display: block;\n &:before {\n content: counters(item, ". ") ". ";\n counter-increment: item\n }\n }\n}\n
Run Code Online (Sandbox Code Playgroud)\n您需要将ol li
子元素放置在parent 中li
。通知家长li
正在拥抱孩子。
<ol>\n <li>Parent 1</li> <!-- Parent has open and close li tags -->\n <ol> \n <li>Child</li>\n </ol>\n <li>Parent 2</li>\n</ol>\n
Run Code Online (Sandbox Code Playgroud)\n