CSS counter() 関数

CSS 函数CSS 関数

例

ページ内にカウンタを作成し(body セレクタ内)、各 <h3> 要素のカウンタ値を増やし、各 <h3> 要素の前にテキスト「Section カウンタ値」を追加します:

body { counter-reset: section; /*カウンタを0にリセット*/ } h3:before { counter-increment: section; /*カウンタ値を増やす*/ content: "Section " counter(section) ": "; /*カウンタを表示*/ }

試してみる »

定義と使い方

counter() 関数は、現在のカウンタの値を文字列として返します。

カウンタの値は、counter-resetとcounter-increment操作を使用して、content 上で counter() 関数を適用してページに表示します。

対応バージョン:CSS3


ブラウザサポート

関数
counter() サポート サポート サポート サポート サポート

CSS 構文

counter(countername, counterstyle)
值 説明
countername 必須。カウンタの名前(counter-reset および counter-increment プロパティで使用される名前と同じ)。
counterstyle オプション。カウンタのスタイル(CSS のlist-style-typeプロパティ値)。

その他の例

例

カウンタのスタイルを設定:

body { counter-reset: section; } h2::before { counter-increment: section; content: "Section " counter(section, upper-roman) ": "; }

試してみる »

例

ネストされたカウンタを設定:

ol { counter-reset: section; /*各 ol 要素に新しいカウンタインスタンスを作成します*/ list-style-type: none; } li:before { counter-increment: section; /*カウンタの現在のインスタンスのみを増加させます。*/ content: counters(section, ".") " "; /*すべてのカウンタインスタンスに、"." で区切られた値を追加します*/ }

試してみる »

CSS 函数CSS 関数

その他の拡張