Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS to add leading zero to an ordered list custom counter

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?

like image 575
Clarus Dignus Avatar asked Apr 19 '18 00:04

Clarus Dignus


People also ask

How do I target a OL Li in CSS?

A space between selectors will get all matching elements below it, no matter how far down in the tree, so your ol li selector will select all li elements, at all three levels. Use the > selector to get direct children. You also need to anchor them to something at the top level, say a div .

How do you style a list of numbers in CSS?

If you want to number items in order, you can use the <ol> (ordered list) tag. But it is kind of hard to style those list numbers in CSS. There is an easier way to create a number styled list of item using the :before pseudo element along with counter properties.


1 Answers

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);
}
like image 82
Doug Avatar answered Nov 03 '22 00:11

Doug