You can use CSS counters to achieve this. Note that I apply: before the pseudo-element to whether that displays the score.
Please note that the reset counter is set to the ol element - this means that the count will be repeated with each ol instance, so you do not need to keep track of the number of lists you have, or the number of litas the list has. You can use it to count nested lists in a sheet, and also display them as 1.1, 1.2, etc.
You can set the counter to start again on each ol .... and you can have different counts and displays for different lists. That way you can definitely structure them the way you want .... https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Lists_and_Counters/Using_CSS_counters
UPDATE - based on a good answer from @Bhuwan Bhatt - I added only the javascript version for setting on the counter via querySelectorAll - used twice - first to get all ols in the document, and then in each ol to get the li number of this ol.
Then just setting the counter to lengthen li (+1 due to zero indexing li)
var lists = document.querySelectorAll('ol'); var listsLength = lists.length; for(i=0;i<listsLength;i++) { var lis= lists[i].querySelectorAll('li'); var lisLength = lis.length +1; lists[i].style.counterReset= 'section '+ lisLength; }
ol { list-style:none; padding-left:0; counter-reset: section ; } li::before { counter-increment: section -1; content: counter(section); margin-right:15px; }
<ol> <li>test 1</li> <li>test 2</li> <li>test 3</li> </ol> <ol> <li>test 1</li> <li>test 2</li> <li>test 3</li> <li>test 3</li> </ol> <ol> <li>test 1</li> <li>test 2</li> </ol>
source share