I'm Dudley Storey, the author of Pro CSS3 Animation. This is my blog, where I talk about web design and development with HTML, CSS and SVG. To receive more information, including news, updates, and tips, you should follow me on Twitter or add me on Google+.

my books

Pro CSS3 Animation book coverPro CSS3 Animation, Apress, 2013

my other blogs

Massive Head CanonMassive Head Canon: Intelligent discussion of movies, books, games, and technology.

my projects

The New DefaultsThe New Defaults — A Sass color keyword system for designers.

CSSslidyCSSslidy — an auto-generated #RWD image slider. 3.8K of JS, no JQuery.

Creating Decorated Ordered Lists With CSS

HTML ordered lists can be incremented in many different languages, but customizing the appearance of the leading numbers in each list item is rather...

HTML ordered lists can be incremented in many different languages, but customizing the appearance of the leading numbers in each list item is rather difficult. Formatting the list in an OpenType font and using font-feature-settings to produce a special numbers option is one possibility, but there is an easier method. By using the under-appreciated CSS counters property – usually associated with generating point-numbered lists – we can customize the leading numbers with abandon.

Given a standard HTML ordered list:

<h1>Clarke’s Three Laws</h1>
<ol id="clarke-laws">
<li>When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
<li>The only way of discovering the limits of the possible is to venture a little way past them into the impossible.
<li>Any sufficiently advanced technology is indistinguishable from magic.
</ol>

We do something rather counter-intuitive, turning off the default appearance of the numbered list and setting up a counter-reset property with a variable (point) while pushing it in from the left:

ol#clarke-laws {
line-height: 1.6;
list-style-type: none;
counter-reset: section;
margin-left: 3rem;
}

Every new list item increments point by 1:

ol#clarke-laws li {
counter-increment: point;
margin-top: 2rem;
}

point is then added back in front of each list item with a pseudo-selector, and customized:

ol#clarke-laws li:before {
content: counters(point,"");
border: 2px solid #000;
border-radius: 50%;
display: inline-block; float: left;
width: 3.5rem; height: 3rem;
text-align: center;
padding-top: .25rem; font-weight: 700;
margin-left: -5rem; margin-right: 1rem;
background: rgba(0,0,0,0.025);
}

Adding Responsive Breakpoints

One of the advantages of this approach is that the point number can be relocated at smaller screen sizes, placing them above each list item:

@media screen and (max-width: 30em) {
ol#clarke-laws{ margin-left: 0; }
ol#clarke-laws li:before { display: block; float: none; margin: 1rem auto; }
}

You can see this behaviour when you narrow the browser window, or look at the page on a mobile device. Inspect the code for decorated ordered lists on CodePen

This site helps millions of visitors while remaining ad-free. For less than the price of a cup of coffee, you can help pay for bandwidth and server costs while encouraging further articles.