定义
counter-reset 创建或重置一个或多个计数器。
counter-increment 计数器增长的步长。
用法:
counter-reset <counter-name> number
- 可设置多个计数器
- <counter-name> 是计数器名字
- number是该计数器初始值,忽略时默认为0
/* 将自定义的计数器section 设置为0 */
counter-reset section
/* 将自定义的计数器section 设置为20 */
counter-reset section 20
/* 将自定义的计数器section-1 设置为2 section-2 设置为20*/
counter-reset section-1 2 section-2 20
例子
注意事项
counter-increment通常都配合伪类使用,此时注意设置content样式的属性,至少也要有个"",否则:before会不起作用的。
demo
code
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>css counter</title>
<style>
body {
counter-reset: firstName;
}
h2{
list-style: none;
counter-reset: twoName;
}
h2:before {
counter-increment: firstName;
// content:"";
content: "Section " counter(firstName) ". ";
}
li:before {
counter-increment: twoName;
content: counter(firstName) "." counter(twoName) " ";
}
li{
list-style: none;
}
</style>
</head>
<body>
<h2>圣诞节系列</h2>
<ul>
<li>麋鹿</li>
<li>平安夜</li>
<li>圣诞老人</li>
</ul>
<h2>元旦系列</h2>
<ul>
<li>跨年</li>
<li>放假咯</li>
<li>吃火锅</li>
</ul>
</body>
</html>