【CSS】counter-incrementを使って擬似要素をリスト化する

デザオ
番号をふりたいんですけどー
デザコト
あー、擬似要素を使ってのリスト化ですねー

サンプル一覧

1段目/1からはじまる
2段目/0からはじまる
3段目/01からはじまる
4段目/001からはじまる

「1」からはじまる

コードを見る(HTML・CSS)

HTML

<div class="count_1">
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
</div>

CSS

/*=======
1 2 3 4 5 ...
=======*/
.count_1 section {
  counter-increment: section;
}
.count_1 section:before {
  content: counter(section);
}

「0」からはじまる

コードを見る(HTML・CSS)

HTML

<div class="count_0">
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
</div>

CSS

/*=======
0 1 2 3 4 ...
=======*/
.count_0 section:nth-of-type(1) {
  counter-reset: section;
}
.count_0 section:not(:nth-of-type(1)) {
  counter-increment: section;
}
.count_0 section:before {
  content: counter(section);
}

「01」からはじまる

コードを見る(HTML・CSS)

HTML

<div class="count_01">
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
</div>

CSS

/*=======
01 02 03 04 05 ...
=======*/
.count_01 section {
  counter-increment: section;
}
.count_01 section:before {
  content: counter(section, decimal-leading-zero);
}

「001」からはじまる

コードを見る(HTML・CSS)

HTML

<div class="count_001">
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
</div>

CSS

/*=======
001 002 003 004 005 ...
=======*/
.count_001 section {
  counter-increment: section;
}
.count_001 section:before {
  content: '0' counter(section, decimal-leading-zero);
}
.count_001 section:nth-of-type(99) ~ section:before {
  content: counter(section, decimal-leading-zero);
}

いまの書き方

この記事を書いたあと、counter まわりの書き方が少しかんたんになりました。「0」からはじめるときは、親に『counter-reset: section -1;』と書くだけで、1 つ目を分けて書かなくて済みます。「001」のように 0 でうめるときは、『@counter-style』の『pad』で桁数を決められます。100 番目からも、そのまま「100」になります。

「0」からはじまる(counter-reset: section -1)

コードを見る(HTML・CSS)

HTML

<div class="count_0_now">
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
</div>

CSS

.count_0_now {
  counter-reset: section -1;
}
.count_0_now section {
  counter-increment: section;
}
.count_0_now section:before {
  content: counter(section);
}

「001」からはじまる(@counter-style の pad)

コードを見る(HTML・CSS)

HTML

<div class="count_001_now">
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
</div>

CSS

@counter-style pad3 {
  system: extends decimal;
  pad: 3 "0";
}
.count_001_now section {
  counter-increment: section;
}
.count_001_now section:before {
  content: counter(section, pad3);
}

どれを使う?

  • 1・01 からはじめる → 上の書き方のまま(decimal-leading-zero で 01)
  • 0 からはじめる → 親に counter-reset: section -1;
  • 001・0001 のように桁をそろえる → @counter-style の pad

@counter-style は、いまのブラウザ(Chrome・Edge・Safari・Firefox)で使えます。

さいごに

デザオ
そうそう、これこれー
デザコト
このサイトの人気ランキングで使ってますよー

ふたりの小言

ほかの見本は、みほんちょうに。

デザコトauthor

あ、いいな、と思うWebデザインを紹介しています。デザインの参考に。やさしいデザインが多いです。Webデザインギャラリー『デザインのこと - Web design gallery』を運営しています。