Learned and implemented a CSS property I had never heard of before: counter-increment

Micah Iverson Author

So this css code, navigates through the dom to "count" how many players there are. In this case our Accordion Section Header has a class of "accordion", so it looks for that class and when it finds it, it adds some ::before content with the current item index value. Then it goes to the next one and does the same. So now my friend can add/delete/reorder players without manually updating the number every time.

0 Likes
Micah Iverson Author

Yeah it's pretty slick and seems to be supported in most browsers.

0 Likes
Micah Iverson Author

This is all the code it took as well…

    body{
        counter-reset: player; 
    }

    .accordion::before{
        counter-increment: player;
        content: "#" counter(player) " ";
    }
0 Likes

what's it do?

0 Likes
Micah Iverson Author

In my case, my friend has an accordion on his page, with like 100 sections, but he wants the header of each section to show "#1 Player Name", next one would be "#2 Player Name", etc. It's just a static Shopify site so it's all hard-coded. The problem is the order of players changes somewhat often, so if he moved player 3 down to player 50, he would have to manually update all the numbers.

0 Likes
Trisha Lim

oooh didn't know about this!

0 Likes

Please sign in to leave a comment.