Sequential Lists Without jQuery: CSS Counters and sibling-index()
You no longer need jQuery to number a list. A sequential list is now three lines of CSS: a counter, an increment and a ::before that prints the value. For staggered animation, the new sibling-index() function gives every item its position, so you can delay each one without writing a single class or a loop.
The original version of this tutorial looped over li elements with jQuery and added item1, item2, item3 classes, each with its own sliced background image. That approach broke whenever the list changed and cost a library download. Below are the modern replacements, each tested in a real browser, followed by one complete file you can paste and run.
Number a list with CSS counters
CSS counters are variables the browser maintains while it renders. You create one with counter-reset, bump it with counter-increment, and display it with counter() inside content. They are long-established and work in every current browser.
<ol class="steps">
<li><div><strong>Install the tools</strong>Node, a browser and an editor.</div></li>
<li><div><strong>Write the markup</strong>A plain ordered list.</div></li>
<li><div><strong>Add the counter</strong>Three declarations, no script.</div></li>
</ol>
.steps {
list-style: none;
margin: 0;
padding: 0;
counter-reset: step;
}
.steps li {
counter-increment: step;
display: grid;
grid-template-columns: 2.5rem 1fr;
gap: 12px;
align-items: start;
padding: 14px 0;
}
.steps li::before {
content: counter(step);
display: grid;
place-items: center;
width: 2.5rem;
aspect-ratio: 1;
border-radius: 50%;
background: oklch(0.55 0.2 265);
color: #fff;
font-weight: 700;
}
Why this beats the old technique: the number is generated from the document, so adding, removing or reordering items renumbers everything instantly. There are no step1.png images, no per-item classes, and the badge scales with the text. Counters also skip elements with display: none, so a filtered list stays correctly numbered.
Keep the semantics
Use a real <ol> for steps. Screen readers announce list position for ordered lists, and that works best when the markup is honest. One caveat: Safari with VoiceOver stops announcing a list as a list when list-style: none is set (Chrome and Firefox keep the semantics). If that matters for your audience, keep the default list-style and style the marker with li::marker instead, or add role="list" to the element.
Add a comment counter
The second original example prepended “#1, #2, #3” to a comment list. Same idea, but the number goes in ::after and is positioned in the corner. Use logical properties so it also works right-to-left.
.comments {
list-style: none;
margin: 0;
padding: 0;
counter-reset: comment;
}
.comments li {
counter-increment: comment;
position: relative;
padding: 14px 56px 14px 16px;
border: 1px solid #e3e6ee;
border-radius: 10px;
margin-bottom: 10px;
}
.comments li::after {
content: "#" counter(comment);
position: absolute;
inset-block-start: 12px;
inset-inline-end: 14px;
font-size: 14px;
font-variant-numeric: tabular-nums;
}
Generated content is not reliably exposed to every assistive technology. If the number carries real meaning, such as a comment someone will refer to by number, also render it in the HTML (a link with the comment ID works well) rather than relying on the counter alone.
Counting backwards and nesting
Use counters(name, ".") for outline numbering like 1.1, 1.2, 2.1: each nested list creates its own counter instance and the function joins them. For descending numbers, an <ol reversed> counts down on its own. CSS counters have an equivalent in the reversed() function of counter-reset, documented on MDN. MDN marks counter-reset as Baseline Widely available overall but notes that parts of it vary, and reversed() is one of the newer parts, so check the compatibility table before relying on it. MDN’s own example guards it with @supports not (counter-reset: reversed(c)); do the same and provide a fallback such as the native reversed attribute.
Stagger items with sibling-index()
“Sequential” often means animation as well: items fade in one after another. Before, that meant a class or inline --i variable per item, usually written by a script. sibling-index() returns an element’s 1-based position among its siblings, and sibling-count() returns the total. MDN lists it as Baseline Newly available, so it works in the latest versions of the major browsers but not necessarily in older ones. The screenshot below freezes the demo about 330 ms in: the first item is nearly done, the second is part-way and the third has only just started.

.reveal li {
animation: fade 0.6s both;
animation-delay: calc(sibling-index() * 90ms);
}
@keyframes fade {
from {
opacity: 0;
translate: 0 12px;
}
}
@media (prefers-reduced-motion: reduce) {
.reveal li {
animation: none;
}
}
The both fill mode keeps each item hidden during its delay and visible after. The reduced-motion query removes the movement for people who ask their system for it. Items that never animate are always fully visible, so the fallback is safe.
A fallback for older browsers
Where sibling-index() is unsupported, the declaration is dropped and every item animates at once. That is a graceful failure, but if you want the stagger everywhere, set a custom property per item and swap in the function when available:
.reveal li:nth-child(1) { --i: 1; }
.reveal li:nth-child(2) { --i: 2; }
.reveal li:nth-child(3) { --i: 3; }
.reveal li {
animation-delay: calc(var(--i, 0) * 90ms);
}
@supports (animation-delay: calc(sibling-index() * 1ms)) {
.reveal li {
animation-delay: calc(sibling-index() * 90ms);
}
}
Trigger the animation on scroll
Animations that run at page load are wasted on lists below the fold. Start them when the list scrolls into view with an IntersectionObserver. Pause the animation by default and run it when a class is added:
.reveal.is-waiting li {
animation-play-state: paused;
}
const lists = document.querySelectorAll('.reveal');
if ('IntersectionObserver' in window) {
const observer = new IntersectionObserver((entries) => {
for (const entry of entries) {
if (!entry.isIntersecting) continue;
entry.target.classList.remove('is-waiting');
observer.unobserve(entry.target);
}
}, { threshold: 0.2 });
lists.forEach((list) => {
list.classList.add('is-waiting');
observer.observe(list);
});
}
Adding is-waiting from JavaScript, not in the HTML, means that if the script fails the list simply animates normally instead of staying paused and invisible. Unobserving after the first trigger stops the list from replaying every time it is scrolled past.
A connector line and 01, 02 numbering
The old tutorial used sliced badge images joined by a drawn line. Both parts are now generated. Pass a counter style as the second argument, counter(step, decimal-leading-zero), to print 01, 02, 03, and draw the connector with a second pseudo-element on every item except the last. decimal-leading-zero is one of the standard list styles; for fully custom markers, @counter-style lets you define your own system, and you can use the result in both counter() and list-style.

The connector is positioned from the badge centre with logical properties, so it flips correctly in right-to-left layouts:
.steps li {
position: relative;
padding-block-end: 28px;
}
.steps li::before {
content: counter(step, decimal-leading-zero);
}
.steps li:not(:last-child)::after {
content: "";
position: absolute;
inset-inline-start: calc(1.25rem - 1px);
inset-block: 2.75rem 0.25rem;
width: 2px;
background: #d5d9e4;
}
The complete demo in one place
The snippets above are easier to follow separately, but this is the whole thing: markup, the numbered badges with connector, the staggered reveal and the scroll trigger. Save the three parts into one HTML file (the CSS in a <style> element, the script at the end of the body) and open it. It was tested in current Chromium; the cap of eight on sibling-index() keeps long lists from waiting seconds for the last row.
<ol class="steps reveal">
<li><div><strong>Install the tools</strong>Node, a browser and an editor.</div></li>
<li><div><strong>Write the markup</strong>A plain ordered list.</div></li>
<li><div><strong>Add the counter</strong>Three declarations, no script.</div></li>
<li><div><strong>Ship it</strong>Reorder items; the numbers follow.</div></li>
</ol>
:root { --accent: oklch(0.55 0.2 265); --line: #d5d9e4; }
body { font: 16px/1.5 system-ui, sans-serif; margin: 0; padding: 32px; background: #f6f7fb; color: #1d2330; }
.steps { list-style: none; margin: 0; padding: 0; counter-reset: step; max-width: 32rem; }
.steps li {
counter-increment: step;
position: relative;
display: grid;
grid-template-columns: 2.5rem 1fr;
gap: 16px;
padding-block-end: 28px;
}
.steps li::before {
content: counter(step, decimal-leading-zero);
display: grid;
place-items: center;
width: 2.5rem;
aspect-ratio: 1;
border-radius: 50%;
background: var(--accent);
color: #fff;
font-weight: 700;
font-variant-numeric: tabular-nums;
}
.steps li:not(:last-child)::after {
content: "";
position: absolute;
inset-inline-start: calc(1.25rem - 1px);
inset-block: 2.75rem 0.25rem;
width: 2px;
background: var(--line);
}
.steps strong { display: block; }
.reveal li {
animation: fade 0.6s both;
animation-delay: calc(min(sibling-index(), 8) * 90ms);
}
.reveal.is-waiting li { animation-play-state: paused; }
@keyframes fade { from { opacity: 0; translate: 0 12px; } }
@media (prefers-reduced-motion: reduce) { .reveal li { animation: none; } }
const lists = document.querySelectorAll('.reveal');
if ('IntersectionObserver' in window) {
const observer = new IntersectionObserver((entries) => {
for (const entry of entries) {
if (!entry.isIntersecting) continue;
entry.target.classList.remove('is-waiting');
observer.unobserve(entry.target);
}
}, { threshold: 0.2 });
lists.forEach((list) => {
list.classList.add('is-waiting');
observer.observe(list);
});
}
Design tips for numbered lists
A few details separate a polished sequence from a default one. Use font-variant-numeric: tabular-nums so digits keep equal width and badges do not jiggle between “1” and “11”. Size the badge in rem or em so it follows the user’s text size, and give it enough contrast: white on the oklch(0.55 0.2 265) blue used here measures about 5.1:1, above the 4.5:1 WCAG AA minimum for normal text. Keep the stagger short. Around 60 to 100 milliseconds per item feels deliberate, while long lists with a delay per item can leave the last row waiting for seconds, so cap the delay with min() if lists can grow, for example calc(min(sibling-index(), 8) * 90ms).
What about jQuery?
jQuery solved real cross-browser problems in 2009, but selecting elements, looping and adding classes are now native one-liners, and numbering itself no longer needs a loop at all.
You only need script when the number must exist in the DOM, for example to copy it, sort by it or send it to a server. The vanilla version of the old loop is short:
document.querySelectorAll('#commentlist li').forEach((li, index) => {
li.style.setProperty('--i', index + 1);
li.dataset.number = index + 1;
});
For anything visual, prefer the CSS counter. It is declarative, needs no JavaScript, and cannot get out of sync with the markup.
Where to go next
Staggered lists pair well with other CSS motion. Start with Easy CSS Animation Using @keyframes and transitions and transforms for the basics, and read The Power of CSS Selectors if :nth-child() still feels unfamiliar. For ready-made multi-step interfaces, see the vertical form with steps and the animated progress wizard. For scroll-driven motion, see How to Zoom Hero Images on Scroll.
Such a simple solution – Thanks!
Just one thing in your JS code:
“i = i+1;”
Change it to “i++” to make it shorter :) .
Nice, but this really shouldn’t be done using JavaScript.
As ~James noticed – depending design on JS IMHO is not good idea. Why don’t you use CSS3 selectors (nth-child)?
Of course, older browsers won’t understand it, but logic are still semantic. I think, the best thing nowadays is generating adequate classes on the server-side. Alternatively, you may use IE’s expression and CSS3 selectors. ;)
The simple ‘ul’ and ‘li’ codes can make a lot of arts.
If you use wordpress or other CMS’s you can use those, so why using JS on a site? If it’s turned off, you’ll end up with nothing?
I prefer using JS for motion and such… This way the site still remains functional, is JS is turned off.
It’s a nice simple solution. Personally, I don’t see that there’s a problem using javascript if the end result is an enhancement to the user interface (progressive enhancement). If someone has javascript turned off the page will still be usable (although it won’t be as pretty).
Thanks for sharing.
There are much better uses for JQuery then just accomodating laziness. Though surely the effort to do this if you aren’t already using JQuery on the site is about the same as adding in those classes.
I think the javascript code would be better off at the end of the document as well.
Cool tutorial, but as what it seems a lot of other people are saying: why use js? Regardless, it’s still a great tutorial!
– Greg
Thanks for the article. Jquery is truly wonderful!
how well does this scale? i am guessing, not well.
Nice, efficient little enhancement! Building on what @Marco said, you could even trim it down to one line if you wanted:
$(“#step li”).each(function (i) {
$(this).addClass(“item” + (++i));
});
a lot of people seem to being saying this is a way to be lazy. to me this seems like the best way to make user generated content (or cms content) to look as part of a well thought out uniformed designed site, when so often it does not. thanks for sharing Nick.
Muito bom cara, gostei.
Porém uso o PHP para isso, para mim é mais facil, mais a dica acima é valida
:D
I wonder what the advantage is of this way of working
i can see only one…
if you send 10 comments over from the server to the user, you have
10 times <span class=”commentnumber”> #1</span>
witch if you have large amounts of comments is faster to generate the number on the userside.
but i was wondering is there any other advantage to this usage?
because if it were only 10 comments (like on lots of blogs)
or paged comments (10 per page)
then this does not give that mutch of a speed differance right?
so if anyone can shed some light here i’d appriciate it
Sander
I see, jQuery is useful :)
and here is a nice example for refreshing old html codes….
I like the concept – just not sure if it serves a purpose which there is already a simple solution for?
Isn’t this what ordered lists are for? For example 1 if you defined the ordered list with a class of ‘steps’ you could access it directly and put one background image in place instead of bandwidth consuming individual ones? Atleast, that is the case if as in your example the only thing that is changing is the text that could be identically styled in html or via sifr.
Just a caution with this approach, (over)doing this will make it difficult for other designers/developers working on the same design as they need to be looking at javascript files for css hooks which will not be immediately apparent…
Hey Nick, how did you do the comment guidelines above the comment box? Something like that on one of my sites would be really useful. Can you email me if you have an extra moment?
Regarding comment #34:
You mean like this?
.commentbox {
border-top-width: medium;
border-top-style: solid;
border-top-color: #666666;
}
Nevermind, I see what you’re talking about…. I misunderstood. You mean like this:
Oh boy do I like this… i am new jquery but I like what i see.
I have been playing with jQuery this week a lot, and I see what it is doing. It’s just taking me extra time to get it. But it is such a useful tool in generating CSS after the DOM has loaded!
This is another reason why i need to take the time and learn javascript/jquery – so I can come up with great idea’s like this.
I’d have done this with php rather than javascript.
Great practical use of jQuery’s prepend() function. jQuery almost makes it too easy.
I use this method on my Indonesia Blog ;)
What version of jQuery did you use? In local, width WordPress 2.7 jQuery version the sequential list doesn’t work.
Thanks very nice post….
I am just starting to use jQuery….
Instead of writing,
$(“#step li”).each(function (i) {
i = i+1;
$(this).addClass(“item” i);
});
couldn’t we just change the background directly and not give it the class like,
$(“#step li”).each(function (i) {
i++;
$(this).css(“background” , “url(step” + i + “.png) no-repeat;”);
});
This will remove the need for us to specify classes like .item1, item2 …
Is it possible to use this code to build a jquery photo gallery? Or is that a completely different deal?
very nice tutorial. is it possible that we could make the background create through a php snippet?
Trey Cruz, is it a trick question? or was it only me found it a self-answerable question?
Awesome solution. Although, what if Javascript is turned off?
It looks pretty clean. I didn’t know about this feature. Thanks for this post! Keep it up!
@Adhip: it’s best to leave the styling in the CSS. this allows you to change your style/design where you normally would, rather than hunting down your styling in javascript code. but yes, you are technically correct.
Thanks and great post!
Just a little typo on the content: “.. find it annonying?”.
Keep it up!
Not sure if it’s been mentioned but Expression Engine has a built-in counter (and perhaps other CMS, that’s only my fav). So adding a sequential number to each item is easy. The gist of it would be
li class=”item{count}”
which would output
li class=”item1″
li class=”item2″
And if you don’t need all the extra markup on ever single li, you can use a conditional to target just one
if count == 5
li class=”item{count}”
I’m building my company site and I’m getting a lot of great ideas from you here. These are RSS feeds worth keeping! Great application of the jQuery UI. I recently started using jQuery – had been using only Mootools before, but now I’m seeing the benefits and weaknesses of both engines.
Trey: Building a gallery out of jQuery is a totally different, and a deeper depth of coding. You’d have to incorporate a way to feed your images whether it be XML or just by plain HTML code, and then turn it into a Javascript masterpiece. Easiest thing to do – look up “Photo Gallery jQuery” on Google and you’ll see a LOT of different galleries already programmed for you!
This is a great article. Website owners often forget that it’s indeed about exposure to the content and not the website. A very usefull list with ‘old’ and new ways to promote your content and even drive sales.
Thanks Thanks What’s the problem here? Google could bury the meager profit number from even the biggest media conglomerates.
Hi,
It’s not really about this trick but i am trying to add a onClick event to a link with a sequential variable.
The code I use is
$(document).ready(function(){
$(‘.imgvign a’).each(function (i) {
i = i+1;
$(‘.imgvign a’).bind(‘click’, function(){return viewer.show()});
});
});
This way it works. My links got the onClick event but if i want {return viewer.show(i)}, it doesn’t work and just link to the last. I have 14 elements and i got each the same 14 number.
I tried
$(document).ready(function(){
$(‘.imgvign a’).each(function (i) {
i = i+1;
});
$(‘.imgvign a’).bind(‘click’, function(){return viewer.show(i)});
});
but it doesn’t work.
So How can i have an increment of all the event like
return viewer.show(0), return viewer.show(1), return viewer.show(2)…
And by the way, how can i start the increment to sort 0 and after 1, 2, 3…
I hope someone could help me with that.
Thanks for answer and for your very useful blog.
thanks so much for this sweet piece of code i’ve been searching for so long!
Very neat and clean trick. The idea is simply awesome, but that title should be CSS Sequential List, as it can be done even without jQuery.
@Smashing Themes: I’m pretty sure that the point here is to NOT have to hard-code the individual class names into the HTML, so the jQuery is completely necessary to accomplish this. Check out what the first paragraph of the article says for confirmation. Remember, you need to actually read the article to get the context, not just look at the examples.
Hey,
Just wondering how to do this with two ol’s with the same class. I’d like it to be for example
#1 – something
#2 – something
#3 – something
then start again from the beginning at the next ol
#1 – something
#2 – something
#3 – something
Any ideas? At the moment, the numbers just keep going – 1,2,3,4,5,6 into the next ol rather than starting again
Hello, i tried out the 1a code snippet as above and it didn’t work. I asked a colleague to help me and he pointed out a syntax error, your example above has a missing + as follows: $(this).addClass(“item”+i);
@Charles Boyung
I read the article first, then I posted the comments :), I meant applying classes on element in loop can be done with native Javascript, you don’t actually have to include a JS library to do so.
Lovely! Thanks for this jQuery boost, with comment #88 from Chris, it was the elegant solution I was looking for. Tks!
@palance
Replying #87, I have this problem too. I believe you have to modify the code a bit. In this tutorial it only loops through the <li> element. That’s why the number doesn’t reset at another <ol> element. I guess……just add another loop for <ol>, like…”for each” <ol>, we loop the <li>.
I’m a newbie of jQuery, but seems like the below code had solved the problem,
$('ol#step').each(function () {
$(this).find('li').each(function (i) {
i = i+1;
$(this).addClass('item' + i);
});
});
Hope this helps.
I’m looking forward to a cleaner solution too~~
I would be a bit careful with this one, make sure that the sequential programming generated by JQuery only is for presentation, and not content. :-)
Thanks for this great tutorial!
One thing though; remember to use the “string operator” when combining literals and variables:
var variable = "text" + otherVariable + "more text";Never mind… to me it is useless and makes no sense. You are using 3 different images for 1, 2, 3. Why not a single image for background and put the numbers in text?
Ya I know the font you are using for the “Step 1”, “Step 2”, “Step 3” may not be in the system and it has a little glow effect. For that I’ll use “Cufon” technology.
Also using css you can apply “Step 1” instead of “1” to OL (ordered list) or any elements
For an example:
body {counter-reset:step;
}
li:before
{
counter-increment:step;
content:"Step " counter(step);
}
Excellent use of jQuery! I’m using this for all my ordered lists with sequential images. One thing to note for all you developers out there: There is a typo in figure 1a (above).
This line – $(this).addClass(“item” i); is missing the plus sign (+) between “item” and i. To get the above code working replace
$(this).addClass(“item” i); with
$(this).addClass(“item”+i);
Still a great tutorial! Very clear and informative.
Cheers,
Kathryn
there are a error in the first example
the line:
$(this).addClass("item" i);it generates a error, and perhaps don’t work
the correct is:
$(this).addClass("item"+i);thanks a lot!
i need a source to learn about jqurey from A_Z
does this work in ie 6 ? my application has to use something similar. I was wondering whether there is a way around for this to show in ie 6.
Great work indeed.
Thanks.
I have recently started using the blogengine.net and I having some problems here? in your blog you stated that we need to enable write permissions on the App_Data folder…unfortunately I don’t understand how to enable it.
If you depend on classes “item1” upto “item4”, you might as well use :nth-child(1) upto :nth-child(4), which will do exactly the same thing without any javascript. Won’t work in IE6 though (but who cares).
There is missing a “+” in your javascript in this line:
$(this).addClass(“item” i);
Elsewise it’s great! :)
Apple’s launch of the iPhone 4 white has seen the greatest excitement for a new phone ever, with HD video recording, a super high-res screen and ridiculously slim dimensions, it’s not hard to see why its so popular in the world.
As iphone 4 white 3GS has something of non-update to the iPhone range, but there are finally decent alternatives in the smartphone market, with the HTC Desire and Samsung Galaxy S leading the Android fight right to Apple’s door.
i need to know the complete list of all possible jquery operations.
IE, the only browser that wastes designers time.
I hope microsoft changes the way it behaves now in the near future.If not no one would use it.
ağlattın bir günde güldür beni sennnn
This information is really good and I will say will always be helpful if we try it risk free. So if you can back it up. That will really help us all. And this might bring some good repute to you.
i need to know the complete list of all possible jquery operations.
i need to know the complete list of all possible jquery operations.
Yep, definitely is annonying to manually code something that is sequential. You really are a big help. Thanks.
I really enjoy this theme youve got going on on your internet site. What is the name of the theme by the way? I was thinking of using this style for the web site I am going to construct for my class project.
This tutorial will show you how to use jQuery to add a sequent of CSS classes to create a graphical list.
You are using 3 different images for 1, 2, 3. Why not a single image for background and put the numbers in text?
Ya I know the font you are using for the “Step 1″, “Step 2″, “Step 3″ may not be in the system and it has a little glow effect. For that I’ll use “Cufon” technology.
you forgot a “+” there.
$(document).ready(function(){
$(“#step li”).each(function (i) {
i = i+1;
$(this).addClass(“item”+i);
});
});
Thank you
Exactly what I was looking for. Already looked up the jQuery online help.
Hi Jathin,
thanks for the hint regarding the “+” … now it works!