counter-reset: 名稱 —— 在容器建立/重置計數器。counter-increment: 名稱 —— 在目標元素遞增。content: counter(名稱) —— 在偽元素顯示。body { counter-reset: h2-counter; }
h2::before {
counter-increment: h2-counter;
content: "第 " counter(h2-counter) " 章 ";
margin-right: 0.5em;
color: #0066cc;
}
article { counter-reset: section; }
h2 { counter-reset: subsection; } /* 子計數器隨父重置 */
h2::before {
counter-increment: section;
content: counter(section) " ";
}
h3::before {
counter-increment: subsection;
content: counter(section) "." counter(subsection) " ";
}
counter(name, upper-roman) /* I, II, III */
counter(name, lower-alpha) /* a, b, c */
counter(name, cjk-ideographic) /* 一, 二, 三 */
counter(name, "★ ") /* 自定義字串(CSS3) */
<ol> 結構)。figure::before { content: "圖 " counter(fig) ":" }IE8+、所有現代瀏覽器完整支援。無需前綴。