I am using a self-styled, numbered list. How can I read the start-attribute and add it to the counter with CSS?
HTML
<ol> <li>Number One</li> <li>Number Two</li> <li>Number Three</li> </ol> <ol start="10"> <li>Number Ten</li> <li>Number Eleven</li> <li>Number Twelve</li> </ol>
CSS
ol { list-style-type: none; /* this does not work like I expected */ counter-reset: lis attr(start, number, 0); } li { counter-increment: lis } li:before { content: counter(lis)". "; color: red; }
FIDDLE
The counter-reset CSS property resets a CSS counter to a given value. This property will create a new counter or reversed counter with the given name on the specified element. Normal counters have a default initial value of 0.
Steps to create HTML counter Step 1: Create the simple structure using HTML <div> tags. Step 2: Add CSS to make the counter more attractive. Step 3: To add the small icons (like suitcase, coffee-cup, Smylie, user icon, book and more) with the counter, use the bootstrap cdn link and code.
The counter-increment property increases or decreases the value of one or more CSS counters. The counter-increment property is usually used together with the counter-reset property and the content property. Default value: none.
ol[start="10"] { counter-reset: lis 9; }
Demo , but this will only apply for this ol
attribute. You would need some javaScript in order to retrieve attribute value to apply, generate the correct counter-reset
.
<ins data-extra="Use of Scss">
see this : DEMO to generate 100 rules from these lines :
@for $i from 1 through 100 { .ol[start="#{$i}"] { counter-reset: lis $i ; } }
Then just copy paste the rules generated if Scss is not avalaible on your hosting .
</in>
<ins data-extra="jQueryFix">
:
$( "ol" ).each(function() { var val=1; if ( $(this).attr("start")){ val = $(this).attr("start"); } val=val-1; val= 'lis '+ val; $(this ).css('counter-increment',val ); });
Notice that : $(this ).css('counter-reset',val );
works too :)
.</ins>
I see that this is an old question, but I'm putting this here because it may come to help someone yet.
You cannot read an attribute in css counter properties. Instead, you could use inline css with counter-reset
to define the starting number for a particular list.
(Yes, I know it is not a best practice to use inline css, but it can and should be used for edge cases like this one)
The first item increments the reset value by 1, so besides providing the counter name, you will need to subtract the number you want the list to start at by 1:
HTML
<ol> <li>Number One</li> <li>Number Two</li> <li>Number Three</li> </ol> <!-- NOTE: List numbering starts at counter-reset + 1 --> <ol style="counter-reset: lis 9;" start="10"> <li>Number Ten</li> <li>Number Eleven</li> <li>Number Twelve</li> </ol>
CSS
ol { list-style-type: none; counter-reset: lis; /* Resets counter to zero unless overridden */ } li { counter-increment: lis } li:before { content: counter(lis)". "; color: red; }
FIDDLE (http://jsfiddle.net/hcWpp/308/)
[EDIT]: kept start attribute as suggested to address accessibility and progressive enhancement
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