63
<ol>
   <li>test</li>
   <li>test</li>
</ol>

will show as:

  1. test
  2. test

I want to have numbers coloured and text black!

I can edit the css, but I do not have access to the HTML.

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254

13 Answers13

123

The CSS spec gives an example of doing just this. Unfortunately, while it works on Firefox 3, it doesn't appear to work on IE7:

<html>
<head>
    <style>
        ol { counter-reset: item; }
        ol li { display: block; }
        ol li:before {
            content: counter(item) ". ";
            counter-increment: item;
            color: red;
        }
    </style>
</head>
<body>
    <ol>
        <li>item</li>
        <li>item</li>
        <li>item</li>
    </ol>
</body>
</html>
feelinferrety
  • 197
  • 1
  • 14
kdgregory
  • 38,754
  • 10
  • 77
  • 102
  • 1
    @MartynChamberlin in a real-world implementation you would use selectors to specify where this styling should be applied. – kdgregory Aug 15 '15 at 11:22
  • @kdgregory Well, @feelinferrety updated his answer yesterday. It used to be that adding selectors to his solution broke it for `ol`, but I see that he fixed that problem. Rock on... – Martyn Chamberlin Aug 16 '15 at 13:38
  • 2
    @MartynChamberlin @kdgregory another approach would be to use the child combinator, so `ol > li` and `ol > li::before` -- that way you only target this specific level of `li` elements. – Erwin Wessels Oct 24 '17 at 06:22
  • Really Awesome .. Works Perfect ... Thanks – Jawad Abdani Jan 17 '18 at 12:42