萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網頁製作 >> HTML 腳本教學

CSS Counter 實現多級級聯編號:counter-reset/increment/content 完整指南

核心屬性三件套

屬性作用典型值
counter-reset創建/重置計數器section 0, item -1
counter-increment遞增計數器section 1, item (默認+1)
content偽元素顯示計數器值counter(section) "." counter(item)

基礎:單級有序列表

  1. Item A
  2. Item B
  3. Item C
.numbered { list-style: none; counter-reset: item; } .numbered li::before { counter-increment: item; content: counter(item) ". "; font-weight: bold; color: #666; }

進階:多級嵌套編號 (1.1, 1.2, 2.1...)

  1. Chapter 1
    1. Section 1.1
    2. Section 1.2
  2. Chapter 2
    1. Section 2.1
.nested { list-style: none; counter-reset: level1; } .nested > li { counter-reset: level2; } .nested > li::before { counter-increment: level1; content: counter(level1) " "; } .nested ol > li::before { counter-increment: level2; content: counter(level1) "." counter(level2) " "; }

counters() 函數:一鍵生成所有層級編號

.nested li::before {
  content: counters(item, ".") " ";  /* 自動串聯所有祖先層級 */
}
.nested {
  counter-reset: item -1;  /* 從 0 開始,抵消根 li 的 +1 */
}

counters(name, separator, style) 會遍歷當前元素所有祖先的同名計數器,用分隔符連接。

完整可運行示例



Getting Started

Installation

Configuration

Advanced Usage

Custom Themes

Plugins

瀏覽器支持

  • IE8+、所有現代瀏覽器完整支持
  • 無需 JavaScript,純 CSS 實現,打印樣式同步生效
copyright © 萬盛學電腦網 all rights reserved