Here's a scenario I find myself in now and then: I want an ordered list, and I want it to be pretty.
Because semantic HTML is important, I reach for the trusty <ol>
tag:
<ol>
<li>Stop</li>
<li>Drop</li>
<li>Roll</li>
</ol>
- Stop
- Drop
- Roll
The problem is that the "bullets" (the prefix numbers) are in a CSS-selector dead zone. There is no way to style them independently!
I am not one to accept lackadaisical bullets. So I did some digging, and found an awesome solution 🎉.
Link to this headingCounters to the rescue
CSS has a pretty nifty trick up its sleeve to deal with this situation. It has a built-in counter mechanism.
Here's how that looks:
ol li {
counter-increment: muffins;
}
ol li:before {
content: counter(muffins) ". ";
}
ol {
list-style: none;
counter-reset: muffins;
}
Let's go through this step by step:
Link to this headingStyling
With this CSS, we've effectively recreated a default <ol>
. The difference is that we now have a CSS selector, ol li:before
, we can use to apply custom styles.
Here's how ordered lists look on this blog, using this trick.
How to bake a cake:
- I think you do something with dry ingredients
- The mixers play a role here
- Combine everything while crossing fingers
- Transfer to oven
- Poke with toothpick for fun, and then serve
It's not night-and-day, but I'm very pleased with the overall effect!
Link to this headingBrowser support
CSS counters feel like a next-gen feature, but actually they've been around forever. They're supported in Internet Explorer 8!!
Use this property without guilt ✨
Also: there's one more trick CSS counters have up their sleeves…
Link to this headingNested lists
Here's the really cool thing: counter
has a cousin, counters
, and it works for nested lists.
Notice how the numbering stacks recursively, in this awesome-sounding curriculum:
- Welcome to web school!
- HTML
- What are tags anyway?
- The truth about DOM
- CSS
- Wallpaper of the Web
- Box Model Mayhem
- Padding
- Border
- Margin
- Specificity as a service
Here's the CSS necessary for this:
ol {
counter-reset: cupcake;
padding-left: 32px;
}
ol li {
counter-increment: cupcake;
}
ol li:before {
content: counters(cupcake, '.') ' ';
/* Whatever custom styles you want here */
color: hotpink;
font-weight: bold;
font-family: cursive;
}
It's super similar, except you use counters
instead of counter
, and you add an "intermediary" spacer (in this case, a period).
Link to this headingBringing Order Back
The ol
doesn't get a lot of love compared to the ul
. And yet, people love counting things! Something doesn't add up.
Maybe with this neat trick, we'll finally see ol
get the attention it deserves.
Last updated on
April 7th, 2020