Lets say I have a simple list like so:
<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>
Now I only want to number list items with the class 'count'
So If I add the CSS:
li { list-style-type: decimal; }
and then remove the list-style-type for list items without the class:
li:not(.count) { list-style-type: none; }
I get this:
FIDDLE
li { list-style-type: decimal; } li:not(.count) { list-style-type: 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>
The obvious problem here is that the list items without the class are also 'counted' here, just not shown.
So in the above example, the list should be numbered to 7 - with the numbering skipping the list items without the class. Can this be done with CSS?
In CSS, identifiers (including element names, classes, and IDs in selectors) can contain only the characters [a-zA-Z0-9] and ISO 10646 characters U+00A0 and higher, plus the hyphen (-) and the underscore (_); they cannot start with a digit, two hyphens, or a hyphen followed by a digit.
To specify multiple classes, separate the class names with a space, e.g. <span class="left important">. This allows you to combine several CSS classes for one HTML element.
To count the number of elements with a specific class: Use the querySelectorAll() method to get a collection of the matching elements. Access the length property on the collection. The length property will return the number of matching elements.
College students can retake a class a maximum of 3 times, most of the time. If they want to retake it a fourth time, they have to write a special letter to the school. It might not be worth it to redo a class so many times, as you would graduate late.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With