【发布时间】:2015-11-20 15:31:02
【问题描述】:
我有这样一段代码,其中的 HTML 是由 hieroglyph,CSS 由 我:
<html>
<head>
<style>
body {
counter-reset: chapter 3 section 0;
}
h2 {
counter-reset: slide 0;
counter-increment: section;
}
h3 {
counter-increment: slide;
}
h1:before {
content: counter(chapter) ". ";
}
h2:before {
content: counter(chapter) "." counter(section) " ";
}
h3:before {
content: counter(chapter) "." counter(section) "." counter(slide) " ";
}
</style>
</head>
<body>
<article> <h1>chapter</h1> </article>
<article> <h2>section A</h2> </article>
<article> <h3> slide a</h3> </article>
<article> <h3> slide b</h3> </article>
<article> <h2>section B</h2> </article>
<article> <h3> slide a</h3> </article>
<article> <h3> slide b</h3> </article>
</body>
</html>
我想给 h1/h2/h3 编号(我将其命名为章节、部分、幻灯片),但是
article 标签让它变得困难。
如何修复 CSS 规则,以便查看:
3. chapter
3.1 section A
3.1.1 side a
3.1.2 side b
3.2 section B
3.2.1 side a
3.2.2 side b
而不是(h3 编号错误):
3. chapter
3.1 section A
3.1.1 side a
3.1.1 side b
3.2 section B
3.2.1 side a
3.2.1 side b
【问题讨论】:
-
stackoverflow.com/questions/31658111/… 的可能副本(或相关)。编号的原因是因为
h3不在同一个父级中。您需要重新查看文档结构,因为使用当前方式,即使您重置body的所有计数器(如该答案中所述),它也无法正常工作(待续......) -
即使你在
body处执行counter-reset: slide 0会发生什么,所有h3都会得到连续编号,因为将其重置回0 的h2在不同的父级之下。 -
感谢 cmets,我会尝试用 JavaScript 而不是 CSS 来实现它。
-
不,伙计。看来您要么必须更改结构(或)将类添加到
article标记,如 in this demo(或)使用脚本。 -
谢谢哈利!我想我可以将您的解决方案与课程一起使用。这是我的scripting solution
标签: css css-counter