以下 CSS 计数器示例未按预期工作。副标题的计数器应在每个主标题后重置:
body {
font: smaller sans-serif;
counter-reset: h1 h2;
}
h1:before {
counter-reset: h2;
content: counter(h1) ". ";
counter-increment: h1;
}
h2:before {
content: counter(h1) "." counter(h2) ". ";
counter-increment: h2;
}
<h1>Heading</h1>
<h2>Sub-heading</h2>
<h2>Sub-heading</h2>
<h1>Heading</h1>
<h2>Sub-heading</h2>
<h2>Sub-heading</h2>
但是,以下工作按预期工作:
body {
font: smaller sans-serif;
counter-reset: h1 h2;
}
h1:before {
content: counter(h1) ". ";
counter-increment: h1;
}
h1 {
counter-reset: h2;
}
h2:before {
content: counter(h1) "." counter(h2) ". ";
counter-increment: h2;
}
<h1>Heading</h1>
<h2>Sub-heading</h2>
<h2>Sub-heading</h2>
<h1>Heading</h1>
<h2>Sub-heading</h2>
<h2>Sub-heading</h2>
我的问题是,为什么counter-reset
在伪元素中不起作用?