css - CSS计数器不递增
问题描述
我的示例中的 CSS 计数器不会增加,尽管我已指定它这样做。我在这里阅读了一些关于可能导致这种情况的元素的嵌套级别的问题,但在我的情况下,所有元素都处于同一级别。
MWE
<!DOCTYPE html>
<head>
<style>
body {
counter-reset: firstCounter;
counter-reset: secondCounter;
}
h2.heading::before {
counter-increment: firstCounter;
content: counter(firstCounter)"\00a0";
}
h3.issue::before {
counter-increment: secondCounter;
content: counter(secondCounter)"\00a0";
}
</style>
</head>
<body>
<h1>The Book</h1>
<h2 class="heading">First Heading</h2>
<h3 class="issue">The Issue of Drinking</h3>
<h3 class="issue">The Issue of Drinking Too Much</h3>
<h2 class="heading">Second Heading</h2>
<h3 class="issue">The Issue of Driving</h3>
<h3 class="issue">The Issue of Drunk Driving</h3>
</body>
</html>
结果
标题“第二标题”的计数器需要为“2”。在 CSS 中交换标签counter-reset
下的顺序body
会导致相同的问题,但与受影响的标题相反。
解决方案
问题是counter-increment
属性的重复定义:
body {
counter-reset: firstCounter;
counter-reset: secondCounter;
}
第二个定义覆盖第一个;要增加多个计数器,您只需使用这些计数器的空格分隔列表:
body {
counter-reset: firstCounter secondCounter;
}
名称:反增量
价值: [ ?]+ | 没有任何
<custom-ident> <integer>?
元素改变其上一个或多个计数器的值。如果元素上当前没有给定名称的计数器,则该元素创建一个具有给定名称的新计数器,其起始值为 0(尽管它可能会立即将该值设置或增加为不同的值)。
body {
counter-reset: firstCounter secondCounter;
}
h2.heading::before {
counter-increment: firstCounter;
content: counter(firstCounter)"\00a0";
}
h3.issue::before {
counter-increment: secondCounter;
content: counter(secondCounter)"\00a0";
}
/* irrelevant, just for the sake of easier visuals in the answer: */
h2 {
border-top: 2px solid #aaa;
}
h3 {
margin-left: 2em;
}
<h1>The Book</h1>
<h2 class="heading">First Heading</h2>
<h3 class="issue">The Issue of Drinking</h3>
<h3 class="issue">The Issue of Drinking Too Much</h3>
<h2 class="heading">Second Heading</h2>
<h3 class="issue">The Issue of Driving</h3>
<h3 class="issue">The Issue of Drunk Driving</h3>
参考:
推荐阅读
- python - 为什么准确率低于 0.01,但预测非常好(99.99%)
- angular - 绑定到模型后,角度 ng-multiselect-dropdown 布局更改
- r - 在闪亮的数据表excel输出中删除标题上方的“标题”行?
- python-3.x - 如何使用 python boto3 在 aws S3 中创建文件
- pandas - 将 pandas 组中的值拆分为新列
- python - 单元测试时未找到 Django Reverse
- java - Java 1.4 和 Maven 2.1.0 的 Maven 构建问题
- android - flutter build apk --debug 在没有发布密钥库的情况下无法构建调试
- sql - SQL 过滤器以查找具有不同状态的重复产品名称
- python - python中for循环的问题只得到最后一项