How can I reset a CSS-counter to the start-attribute of the given list
You may just use the attribute start as a filter :
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">
:
A jQuery solution can be easily set up : DEMO
$( "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
Just providing a streamlined version of GCyrillus JS solution
$('ol[start]').each(function() {
var val = parseFloat($(this).attr("start")) - 1;
$(this).css('counter-increment','lis '+ val);
});
I wish CSS could read and use numeric values from HTML attributes :(
Back on an old question i have forgotten about.
Nowdays there is the CSS custom properties that could be used , even then , it requires to add a style
attribute aside your start
attribute
Custom properties (sometimes referred to as CSS variables or cascading variables) are entities defined by CSS authors that contain specific values to be reused throughout a document. They are set using custom property notation (e.g., --main-color: black;) and are accessed using the var() function (e.g., color: var(--main-color);).
example (if the code is generated it seems easier to set both value the same for start=x
and var(--s:x)
to avoid mistake):
ol {
list-style-type: none;
/* this does not work like I expected
counter-reset: lis attr(start, number, 0); */
/* update using the css varaiable from html */
counter-reset: lis calc(var(--s) - 1) ;
/* calc() is used to keep html attributes values coherent */
}
li {
counter-increment: lis
}
li:before {
content: counter(lis)". ";
color: red;
}
<ol>
<li>Number One</li>
<li>Number Two</li>
<li>Number Three</li>
</ol>
<ol start="10" style="--s:10"><!-- or set it right away to nine to get rid of calc() in the css rule-->
<li>Number Ten</li>
<li>Number Eleven</li>
<li>Number Twelve</li>
</ol>
<ol start="30" style="--s:30"><!-- or set it right away to twenty nine to get rid of calc() in the css rule -->
<li>Number Thirty</li>
<li>Number Thirty one</li>
<li>Number Thirty two</li>
</ol>
That's far to late to be an answer but could be useful to anyone else from now.
ol {
list-style-type: none;
counter-reset: lis var(--start-value, 0);
}
li {
counter-increment: lis;
}
li:before {
content: counter(lis)". ";
color: red;
}
<ol>
<li>Number One</li>
<li>Number Two</li>
<li>Number Three</li>
</ol>
<ol style="--start-value: 1;">
<li>Number Two</li>
<li>Number Three</li>
<li>Number Four</li>
</ol>
You can use CSS custom properties (variables), see.
For example, use inline styles for list and add custom property --start-value: 1;
.
In CSS you can use it like var(--start-value, 0);
with fallback value (0
). If you skip this custom property, list will start by default.
HTML
<ol style="--start-value: 1;">
<li>Number Two</li>
<li>Number Three</li>
<li>Number Four</li>
</ol>
CSS
ol {
list-style-type: none;
counter-reset: lis var(--start-value, 0);
}
li {
counter-increment: lis;
}
li:before {
content: counter(lis)". ";
color: red;
}