Can you count a particular class with CSS?

后端 未结 7 1079
刺人心
刺人心 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;
    }
    • one
    • two
    • three
    • four
    • blabla
    • five
    • six
    • blabla
    • seven

提交回复
热议问题