css的counter属性能够实现元素的计数,咱们能够考虑利用counter来实现标题的自动编号。css
counter相关的属性
counter-reset属性
用来设置某个计数器的值。ide
使用示例wordpress
counter-reset: cnt_h2; /* 设置cnt_h2,默认从0开始 */ counter-reset: cnt_h3 1; /* 设置cnt_h3,从1开始 */ counter-reset: cnt_h4 1 cnt_h5 2; /* 设置cnt_h4从1开始,cnt_h5从2开始 */
counter-increment属性
给某计数器的值加1。例如:counter-increment: cnt_h3
(默认增长1)或counter-increment: cnt_h4 2
(指定增长2)。性能
counter()方法
counter()
获取计数器的值,默认的编号类型是数字。固然,除数字外,也可使用字母、罗马数字等,只需在 counter 中指定编号种类便可,编号种类能够是 list-style-type 属性能支持的任何值。如:content: counter(counter_h1, lower-roman);
spa
list-style-type支持的值
CSS2 的值:ssr
值 描述 none 无标记。 disc 默认。标记是实心圆。 circle 标记是空心圆。 square 标记是实心方块。 decimal 标记是数字。 decimal-leading-zero 0开头的数字标记。(01, 02, 03, 等。) lower-roman 小写罗马数字(i, ii, iii, iv, v, 等。) upper-roman 大写罗马数字(I, II, III, IV, V, 等。) lower-alpha 小写英文字母The marker is lower-alpha (a, b, c, d, e, 等。) upper-alpha 大写英文字母The marker is upper-alpha (A, B, C, D, E, 等。) lower-greek 小写希腊字母(alpha, beta, gamma, 等。) lower-latin 小写拉丁字母(a, b, c, d, e, 等。) upper-latin 大写拉丁字母(A, B, C, D, E, 等。) hebrew 传统的希伯来编号方式 armenian 传统的亚美尼亚编号方式 georgian 传统的乔治亚编号方式(an, ban, gan, 等。) cjk-ideographic 简单的表意数字 hiragana 标记是:a, i, u, e, o, ka, ki, 等。(日文片假名) katakana 标记是:A, I, U, E, O, KA, KI, 等。(日文片假名) hiragana-iroha 标记是:i, ro, ha, ni, ho, he, to, 等。(日文片假名) katakana-iroha 标记是:I, RO, HA, NI, HO, HE, TO, 等。(日文片假名) CSS2.1 的值:code
disc | circle | square | decimal | decimal-leading-zero | lower-roman | upper-roman | lower-greek | lower-latin | upper-latin | armenian | georgian | none | inherit
具体的使用
h1 { counter-reset: cnt_h2 0 cnt_h3 0 cnt_h4 0 cnt_h5 0; } h2 { counter-reset: cnt_h3 0 cnt_h4 0 cnt_h5 0; counter-increment: cnt_h2; } h2:before { content: counter(cnt_h2)" "; color: #f66; font-family: monospace; font-style: oblique; } h3 { counter-reset: cnt_h4 0 cnt_h5 0; counter-increment: cnt_h3; } h3::before { content: counter(cnt_h2)"-"counter(cnt_h3)" "; color: #f66; font-family: monospace; font-style: oblique; } h4 { counter-reset: cnt_h5; counter-increment: cnt_h4; } h4::before { content: counter(cnt_h2)"-"counter(cnt_h3)"-"counter(cnt_h4)" "; color: #f66; font-family: monospace; font-style: oblique; } h5 { counter-increment: cnt_h5; } h5::before { content: counter(cnt_h2)"-"counter(cnt_h3)"-"counter(cnt_h4)"-"counter(cnt_h5)" "; color: #f66; font-family: monospace; font-style: oblique; }
参考资料
[1] CSS counter计数器(content目录序号自动递增)详解rem
[2] CSS list-style-type 属性get