css實現標題自動編號

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

—— CSS list-style-type 屬性ci

具體的使用

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

相關文章
相關標籤/搜索