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:
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?
You can use HTML to set the value of a list item specifically:
http://jsfiddle.net/03bu5sct/1/
You may also want to look at CSS3 counters if you want a pure CSS solution.
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)
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:
Here you go:
https://jsfiddle.net/Cheese1991/qnmhvvxj/
HTML:
CSS:
I hope this will help you
this is a simple hack http://jsfiddle.net/9w9vkcf3/1/
Giving
display: block
to the li elements without.count
class is also working.Working Fiddle
For older browsers:
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
One solution could be to use CSS counters and apply it using pseudo-element :before taking advance of content property with counter,
only in
li
elements with classcount
:References
counter-increment
counter-reset