7

What I have:

An ordered list with a custom counter:

ol {
  /*list-style-type:decimal-leading-zero;*/
  list-style: none;
  padding-left: 0px;
  counter-reset: item;
}

ol>li {
  display: table;
  counter-increment: item;
}

ol>li:before {
  display: table-cell;
  padding: 0 0.5em 0 0;
  content: counter(item) ".";
  font-weight: bold;
}
<ol>
  <li>List item one.</li>
  <li>List item two.</li>
  <li>List item three.</li>
  <li>List item four.</li>
  <li>List item five.</li>
  <li>List item six.</li>
  <li>List item seven.</li>
  <li>List item eight.</li>
  <li>List item nine.</li>
  <li>List item ten.</li>
</ol>

What I need:

A leading zero before any list item numbered less than 10.

I.e. 01, 02, 03, 04, 05, 06, 07, 08, 09, 10.

My question:

How can I achieve the required leading zero via CSS?

Clarus Dignus
  • 3,847
  • 3
  • 31
  • 57
  • 1
    https://codepen.io/maddesigns/pen/JGQejg – Doug Apr 19 '18 at 00:06
  • @Doug Thanks. This works. With respects to my code, I simply had to add `, decimal-leading-zero` immediately after `item` for `ol>li:before`. Please post as an answer to allow me to accept your solution. – Clarus Dignus Apr 19 '18 at 00:28

1 Answers1

17

From: Sven Wolfermann via CodePen

You can add a leading zero by including decimal-leading-zero to your li:before{ content: counter(...); }

li:before {
  counter-increment: li;
  content: counter(item, decimal-leading-zero);
}
Doug
  • 1,435
  • 1
  • 12
  • 26
  • Thanks. The example you linked to has enabled me to understand how to incorporate leading zeroes by editing the content/counter values. – Clarus Dignus Apr 19 '18 at 01:10