LoginSignup
1
0

More than 1 year has passed since last update.

CSSカウンターを使ってみる

Last updated at Posted at 2022-12-09

ひとりCSS Advent Calendar 2022 10日目です。

CSSカウンターを使ってみます。

CSSカウンターとは

  • 見出し番号を自動で振れる
  • ol の番号を変えられる

使い方

  • counter-reset プロパティでカウンターの値を初期化する
    • 初期化する以外にデフォルト値を設定できる
  • counter-increment でカウンターの値を増加できる
  • counter() または counters()content プロパティで使用すると表示できる

使ってみる

<section>
  <h2>Headline</h2>
  <p>Pragraqh</p>
</section>

<section>
  <h2>Headline</h2>
  <p>Pragraqh</p>
</section>

<section>
  <h2>Headline</h2>
  <p>Pragraqh</p>
</section>

<section>
  <h2>Headline</h2>
  <ol>
    <li>List item</li>
    <li>List item</li>
    <li>List item</li>
  </ol>
</section>
:root {
  counter-reset: section;
}

h2::before {
  counter-increment: section;
  content: counter(section) ": ";
}

ol {
  counter-reset: listConter;
}

li {
  counter-increment: listConter;
}

li::marker {
  content: counter(section) "." counter(listConter) " ";
}

感想

  • 便利

参考

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0