Can you style ordered list numbers?
Asked Answered
S

5

128

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.png')};
ol li:nth-child(2) {list-style-image:url('2.png');} 
etc...

Is there a simpler solution?

Sollie answered 12/5, 2014 at 13:17 Comment(3)
You can research for a solution from my demo here 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.Ouachita
some links for the info 1. codeitdown.com/ordered-list-css-styles 2. css-tricks.com/numbering-in-style Its a good qtn, but little bit of searching might have got you the answerAffricative
@KingKing - I'd suggest marking this as a duplicate then...Dextrality
F
254

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>
Fay answered 12/5, 2014 at 13:20 Comment(7)
The counter-reset: item; should go in the ol block, otherwise the counter will not be reset in nested <ol>.Sickener
a nice solution, but how is the rendering when the body of the li element is more than one line?Lemuelah
I think that, as in #13355078, you can use for example ` margin-left: -2.0em; width: -2.0em;`Lemuelah
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%," October 30, 2010.)Dree
@Lemuelah - 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.Inaudible
How do you stop this applying to nested <ol><li> that have a,b,c etcAchieve
the "start" attribute for ol does not work with this solution.Wrecker
T
27

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>
Trilateration answered 28/5, 2016 at 4:35 Comment(0)
G
19

2021 update, you can use the ::marker pseudo element selector in recent browsers: https://developer.mozilla.org/en-US/docs/Web/CSS/::marker

 li::marker {
   color: red;
 }
 li {
   color: blue;
 }
<ol>
  <li>item</li>
  <li>item</li>
  <li>item</li>
  <li>item</li>
</ol>
Gleiwitz answered 29/6, 2021 at 21:55 Comment(3)
cool! does this work on Chrome and Safari?Sollie
@Sollie yes but with some restrictions on what can and can't be styled. For simple stuff like color it works nicely. caniuse.com/css-marker-pseudoGleiwitz
Judging by CanIUse, as of 2022 there don't seem to be any more restrictions on usage and all modern browsers seem to support it well.Countryandwestern
C
2

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>

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>
Cusec answered 25/9, 2023 at 18:24 Comment(0)
S
1

The simple solution is:

    ol li::marker {
        font-weight: bold;
    }

Works in Chrome.

Saree answered 14/7, 2023 at 20:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.