9
I’m making a numbered list of regressive counters using the counter-reset
. Currently I put the number of items in the list manually:
.faq { counter-reset:my-counter 2; }
.faq dt { counter-increment: my-counter -1; }
.faq dt:before { content: counter(my-counter, decimal-leading-zero) "."; }
With HTML:
<dl class="faq">
<dt>Item 1</dt>
<dt>Item 2</dt>
</dl>
Prints:
01. Item 1
00. Item 2
But if you add one <dt>
and not adjust the counter-reset
, will print:
01. Item 1
00. Item 2
-01. Item 3
Li Can CSS Detect the number of Children an element has? and tried to counter-reset:my-counter ~ dt
, but I didn’t really understand the use of ~
.
I know what I can do with jQuery
var reset = 'my-counter '+ $('.faq dt').length;
$('.faq').css( 'counter-reset', reset );
It is possible with pure CSS?
Reference: Numbering In Style | CSS-Tricks
That Soen’s answer works by seeing if the umpteenth element
li
exists and applying the rule both to him and to siblings his. Unfortunately I think there is no way to get a number just using css, you will have to use Magic number even (maybe using pre-processors of css role, but then I can’t say)– William Barbosa
Checking the documentation on MDN I noticed that CSS Counter has three properties, these being: -counter - counter-increment - counter-reset in this case what can be done is to display using CSS the list in reverse order.
– ooredroxoo
Correcting, resetting to the desired integer. In this case using CSS you can display the list in the reverse order to which it is in the DOM, thus achieving the desired effect.
– ooredroxoo
Wow, ooredpurple, if you can confirm your theory and publish an answer, fantastic.
– brasofilo
I did some digging and I don’t think I can. I suspect that CSS has no way of knowing this, it increments the counter when it finds each element, so it only knows the total after going through all.
– bfavaretto
Worse than in a
<ol>
would be extremely simple (except for IE maybe): http://www.impressivewebs.com/reverse-ordered-lists-html5/– bfavaretto
I think what the purple ooredo meant was something similar to this here: Use css to invert the list in the race
– William Barbosa