Can you count a particular class with CSS?

后端 未结 7 902
刺人心
刺人心 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:33

    You can use HTML to set the value of a list item specifically:

    <ul>
        <li class="count">one</li>
        <li class="count">two</li>
        <li class="count">three</li>
        <li class="count">four</li>
        <li>blabla</li>
        <li value="5" class="count">five</li>
        <li class="count">six</li>
        <li>blabla</li>
        <li value="7" class="count">seven</li>
    </ul>
    

    http://jsfiddle.net/03bu5sct/1/

    You may also want to look at CSS3 counters if you want a pure CSS solution.

    0 讨论(0)
  • 2021-02-02 08:35

    Giving display: block to the li elements without .count class is also working.

    ul {
        list-style-type:decimal;
        padding-left: 30px;
    }
    li:not(.count) {
        display: block;
    }
    

    Working Fiddle

    For older browsers:

    ul {
        list-style-type:decimal;
        padding-left: 30px;
    }
    li {
        display: block;
    }
    li.count {
        display: list-item;
    }
    

    Working Fiddle

    Another way, if you are planning to change the display state of all li elements, use :after/:before pseudo classes with display as list-item.

    Working Fiddle

    0 讨论(0)
  • 2021-02-02 08:40
    li {
        list-style-type: decimal;
    }
    
    li:not(.count) {
          -webkit-appearance: textfield;
          -moz-appearance: textfield;
          appearance: textfield;
    }
    

    this is a simple hack http://jsfiddle.net/9w9vkcf3/1/

    The appearance property is used to display an element using a platform-native styling based on the users' operating system's theme. source

    0 讨论(0)
  • 2021-02-02 08:40

    One solution could be to use CSS counters and apply it using pseudo-element :before taking advance of content property with counter,

    Counters may be specified with two different functions: 'counter()' or 'counters()'. The former has two forms: 'counter(name)' or 'counter(name, style)'. The generated text is the value of the innermost counter of the given name in scope at this pseudo-element; it is formatted in the indicated style ('decimal' by default). The latter function also has two forms: 'counters(name, string)' or 'counters(name, string, style)'. The generated text is the value of all counters with the given name in scope at this pseudo-element, from outermost to innermost separated by the specified string. The counters are rendered in the indicated style ('decimal' by default). See the section on automatic counters and numbering for more information. The name must not be 'none', 'inherit' or 'initial'. Such a name causes the declaration to be ignored.

    only in li elements with class count:

    body {
      counter-reset: section;/* Set the section counter to 0 */
    }
    ol {
      list-style-type: none;
    }
    li.count::before {
      counter-increment: section;/* Increment the section counter*/
      content: counter(section)". ";/* Display the counter */
    }
    <ol>
      <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>
    </ol>

    References

    counter-increment

    counter-reset

    0 讨论(0)
  • 2021-02-02 08:41

    Here you go:

    https://jsfiddle.net/Cheese1991/qnmhvvxj/

    HTML:

    <ul>
    <li>One</li>
    <li>Two</li>
    <li class="skip">Skip</li>
    <li>Three</li>
    <li>Four</li>
    </ul>

    CSS:

    ul {
        counter-reset:yourCounter;
        list-style:none;
    }
    ul li:not(.skip) {
        counter-increment:yourCounter;
        list-style:none;
    }
    ul li:not(.skip):before {
        content:counter(yourCounter) ". ";
    }
    ul li.skip:before {
        content:"\a0\a0\a0";
    }

    I hope this will help you

    0 讨论(0)
  • 2021-02-02 08:53

    This can be done with CSS-counters

    If I set display:none on the generated content with the counter, the counter skips over it, and continues to the next item!

    FIDDLE

    (Edit: Or, alternatively - as others have pointed out - we could increment the counter only on the elements with the particular class - like so)

    ol {
      counter-reset: count;
      list-style-type: none;
      padding-left: 30px;
    }
    li:before {
      content: counter(count)".";
      counter-increment: count;
    }
    li:not(.count) {
      padding-left: 13px;
    }
    li:not(.count):before {
      display: none;
    }
    <ol>
      <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>
    </ol>

    So actually, with counters, not only can we count classes, we can also count any selector combination.

    Here's an example for proof of concept:

    html {
      counter-reset: divs;
    }
    body {
      counter-reset: paragraphs;
      position: relative;
    }
    div {
      counter-increment: divs;
    }
    .wpr {
      counter-reset: count-me;
      position: relative;
    }
    .container {
      position: relative;
      border-bottom: 1px solid green;
    }
    .container .count-me {
      counter-increment: count-me;
    }
    .container p {
      counter-increment: paragraphs;
    }
    .wpr:after {
      content: "Number of count-me classes in container:" counter(count-me);
      position: absolute;
      bottom: -20px;
    }
    .container:after {
      content: "Number of paragraphs:" counter(paragraphs);
      position: absolute;
      bottom: -40px;
    }
    body:after {
      content: "Number of divs:" counter(divs);
      position: absolute;
      bottom: -60px;
    }
    <div class="wpr">div1
      <div class="container">div2
        <div>div3
          <span class="count-me">count-me</span>
        </div>
        <div>div4
          <span class="count-me">count-me</span>
          <p>I"m a paragragh</p>
        </div>
        <div>div5
          <p>I"m a paragragh</p>
        </div>
        <div>div6
          <span class="count-me">count-me</span>
        </div>
        <div>div7
          <span class="count-me">count-me</span>
          <p>I"m a paragragh</p>
        </div>
        <div>div8</div>
      </div>
    </div>

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