CSS Counters其实就是一计数器,早期在CSS中计数器仅存在于
uloldivlist-style-imagebackgroud-image:after:before::after::beforelist-style-type
 这种技术令人有点困惑,因为他看起来不同于其他CSS属性,需要同时使用多个CSS属性。接下来与大家一起探讨如何使用这几个属性。
CSS Counters用到的属性
前面也提到过,使用CSS Counters给元素创建自动递增计算器不仅仅是依赖于某一个CSS属性来完成,他需要几个属性一起使用才会有效果。使用的到属性包括:
-  
 :此值是必需的。必须用于选择器,主要用来标识该作用域,其值可以自定义。**counter-reset**
-  
 :用来标识计数器与实际相关联的范围。**counter-increment**
-  
 :用来生成内容,其为**content**
 、:before
 或:after
 、::before
 的一个属性。在生成计数器内容,主要配合::after
 一起使用。counter()
-  
 :该函数用来设置插入计数器的值。**counter()**
-  
 :配合**:before**、**:after**或**::before**、**::after**
 用来生成计数器内容。content
要想完全了解或者熟练使用好CSS Counters来创建计数器,就需要对用到的每个属性的使用规则有一定的了解。接下来就先简单的了解这几个属性。
counter-reset
语法规则
counter-reset:[ <identifier> <integer>? ]  | none | inherit 取值说明
counter-resetnone-  
 :用来定义计数器的名称,这个名称可以自定义,如:**identifier**
 。而且后面可以紧跟一个整数值,中间用空格分隔开来,如item
 ;headings 0
-  
 :此值用来设置调用计算数器时起始值,默认值为**integer**
 。0
功能描述
counter-reset001-5-4counter-resetnoneinheritcounter-resetsection 0 heading 0counter-increment
语法规则
counter-increment:[ <identifier> <integer>? ]  | none | inherit 取值说明
默认值为
none-  
 :计数器名称,就调用**identifier**
 声明的计数器的标识符。counter-reset
-  
 :一个整数值,指定计数起始值。其值允许是0或者负整数值,如果未指定任何值,则该值为1(前提是**integer**
 未显式设置计数的起始值)。其值递增是按倍数值递增,如果设置了值为2,后面元素递增值为4、6、8,依此类推。counter-reset
功能描述
counter-incrementcounter-resetcounter-ncrement1content
content:before:after::before::aftercontent在本文中所涉及到的内容,
contentcounter([])
h2{
counter-increment: section;
&:before{
content:"Chapter" counter(Chapter) "." counter(section);
}
}
counter()
counter()contentcounter()counter(),counter-incrementlist-style-typeupper-romanupper-alphacounter()list-style-type- disc
- circle
- square
- decimal
- decimal-leading-zero
- lower-roman
- upper-roman
- lower-greek
- lower-latin
- upper-latin
- armenian
- georgian
- lower-alpha
- upper-alpha
- none
- inherit
同样的,可以使用多个
counter()content: counter(Chapter,upper-roman) "-" counter(section,upper-roman);注:定义reset要在元素之前定义,部分安桌机设置默认值无效!!!
在线案例1
在线案例2
转载请注明:前端录»CSS的计数器:counter-increment与counter-reset
<script src="http://www.wozhuye.com/index.php?m=digg&c=index&a=init&id=3-297-2"></script>