≪ Today I learned.
RSS購読
    公開日
    タグ
    CSS
    著者
    ダーシノ

    CSS @counter-styleを使ったリストマークのカスタマイズ

    @counter-styleを使うことで、カウンターによってリストマークをカスタマイズすることができる。

    同じようにリストマークを変更するlist-styleプロパティもあるのだが、こちらはすべてのリストマークが変更されてしまう。(※nth-childなどを使えば1つずつ変更できないこともない)

    <ol class="hands">
      <li>One</li>
      <li>Two</li>
      <li>Three</li>
      <li>Four</li>
      <li>Five</li>
      <li>Six</li>
      <li>Seven</li>
      <li>Eight</li>
      <li>Nine</li>
      <li>Ten</li>
    </ol>
    /* list-styleを使う場合 */
    .hands {
      &:nth-child(1) {
        list-style: url(...);
      }
      &:nth-child(2) {
        list-style: url(...);
      }
      ...
    }
    
    /* @counter-styleを使う場合 */
    @counter-style hands {
      system: additive;
      additive-symbols: 5 "✋", 4 "☝️🤟", 3 "🤟", 2 "✌️", 1 "☝️";
      suffix: " ";
    }
    
    .hands {
      list-style: hands;
    }

    @counter-styleの仕組み

    @counter-styleには以下のようなプロパティがある。

    デモ

    1. One
    2. Two
    3. Three
    4. Four
    5. Five
    6. Six
    7. Seven
    8. Eight
    9. Nine
    10. Ten