Can you count a particular class with CSS?

后端 未结 7 904
刺人心
刺人心 2021-02-02 08:08

Lets say I have a simple list like so:

  1. one
  2. two
  3. &l
相关标签:
7条回答
  • 2021-02-02 08:56

    The counters section in CSS 2.1 specifications contains various examples of how to implement your custom counter. Here is a very simple example where you:

    1. Define a counter variable
    2. Increment it for specific elements (in your case it would be .count elements)
    3. Display it inside pseudo elements

    .custom-counter {
      /* define a counter variable */
      counter-reset: clumsycount 0;
      /* style */
      list-style-type: none;
    }
    .custom-counter .count {
      /* increment the counter variable */
      counter-increment: clumsycount 1;
      /* style */
      position: relative;
      background-color: #EEE;
    }
    .custom-counter .count:before {
      /* display the counter variable */
      content: counter(clumsycount) ".";
      /* style */
      position: absolute;
      top: 0;
      right: 100%;
      padding-right: .25em;
      background-color: #CCC;
    }
    <ul class="custom-counter">
      <li class="count">one</li>
      <li class="count">two</li>
      <li class="count">three</li>
      <li class="count">four</li>
      <li>blabla</li>
      <li class="count">five</li>
      <li class="count">six</li>
      <li>blabla</li>
      <li class="count">seven</li>
    </ul>

    0 讨论(0)
提交回复
热议问题