97

I'm trying to style the numbers in a ordered list, I'd like to add background-color, border-radius and color so they match the design I'm working from:

enter image description here

I guess it's not possible and I'll have to use different images for each number i.e.

ol li:first-child {list-style-image:url('1.gif')};
ol li:nth-child(2) {list-style-image:url('2.gif');} 
etc...

Is there a simpler solution?

Pixelomo
  • 5,847
  • 3
  • 42
  • 56
  • 3
    You can research for a solution from my demo here http://jsfiddle.net/viphalongpro/Hj8Nn/ BTW, I don't think this is **impossible** to search, searching first may give you **a lot of results**, right in SO, this kind of question has been asked many times. – King King May 12 '14 at 13:20
  • 1
    some links for the info 1. http://codeitdown.com/ordered-list-css-styles/ 2. http://css-tricks.com/numbering-in-style/ Its a good qtn, but little bit of searching might have got you the answer – crafter May 12 '14 at 13:22
  • 1
    @KingKing - I'd suggest marking this as a duplicate then... – Lee Taylor May 12 '14 at 13:29

2 Answers2

199

You can do this using CSS counters, in conjunction with the :before pseudo element:

 ol {
   list-style: none;
   counter-reset: item;
 }
 li {
   counter-increment: item;
   margin-bottom: 5px;
 }
 li:before {
   margin-right: 10px;
   content: counter(item);
   background: lightblue;
   border-radius: 100%;
   color: white;
   width: 1.2em;
   text-align: center;
   display: inline-block;
 }
<ol>
  <li>item</li>
  <li>item</li>
  <li>item</li>
  <li>item</li>
</ol>
SW4
  • 65,094
  • 17
  • 122
  • 131
  • 13
    The `counter-reset: item;` should go in the ol block, otherwise the counter will not be reset in nested
      .
    – Michael Klöpzig Sep 06 '16 at 11:36
  • 2
    a nice solution, but how is the rendering when the body of the `li` element is more than one line? – cmhughes Apr 15 '18 at 11:52
  • I think that, as in https://stackoverflow.com/questions/13354578/custom-li-list-style-with-font-awesome-icon, you can use for example ` margin-left: -2.0em; width: -2.0em;` – cmhughes Apr 15 '18 at 12:01
  • A value of `50%` for `border-radius` (rather than `100%`) is sufficient to get a circle. (See, e.g., Lea Verou, "[The curious case of border-radius:50%](http://lea.verou.me/2010/10/the-curious-case-of-border-radius50/)," October 30, 2010.) – Jim Ratliff Jul 13 '19 at 05:09
  • @cmhughes - if you wanted to do that, you'd give the `li` `position: relative;`, and then for `:before` you'd give that `position: absolute;` and then use `top` and `left` to position it exactly as you like. – mike May 12 '20 at 19:28
25

I was looking for something different, and found this example at CodePen;

try this: http://codepen.io/sawmac/pen/txBhK

body {
  font-size: 1.2em;
  font-family: "Helvetica Neue", Helvetica, sans-serif;
  margin: 50px;
}
.custom-counter {
  margin: 0;
  padding: 0;
  list-style-type: none;
}
.custom-counter li {
  counter-increment: step-counter;
  margin-bottom: 5px;
}
.custom-counter li::before {
  content: counter(step-counter);
  margin-right: 20px;
  font-size: 80%;
  background-color: rgb(180, 180, 180);
  color: white;
  font-weight: bold;
  padding: 3px 8px;
  border-radius: 11px;
}
<ol class="custom-counter">
  <li>This is the first item</li>
  <li>This is the second item</li>
  <li>This is the third item</li>
  <li>This is the fourth item</li>
  <li>This is the fifth item</li>
  <li>This is the sixth item</li>
</ol>
jacroe
  • 104
  • 9
Russ
  • 251
  • 3
  • 2