CSS Ordered List 1, 1.1...1.10 Formatting and Alignment

What?
A quick article to remind me on how create an multi-level ordered list that indents and aligns correctly.

Why?
I'm finding that I need to do this quite often for some clients who want to include their terms and conditions in quote/invoice templates and want the HTML to be indented neatly.

Other examples out there will work, but I found that once the list count increased the number of digits (eg. 1.10) the text would be more indented (relative) to 1.1. I need all list elements to be all perfectly aligned.
copyraw
// What I Have

1.   Item 1
     1.1   Item 1a
     1.2   Item 1b
     ... 
     1.9   Item 1c
     1.10   Item 1d
2.   Item 2
  1.  // What I Have 
  2.   
  3.  1.   Item 1 
  4.       1.1   Item 1a 
  5.       1.2   Item 1b 
  6.       ... 
  7.       1.9   Item 1c 
  8.       1.10   Item 1d 
  9.  2.   Item 2 
copyraw
// What I Want

1.   Item 1
     1.1   Item 1a
     1.2   Item 1b
     ... 
     1.9   Item 1c
     1.10  Item 1d
2.   Item 2
  1.  // What I Want 
  2.   
  3.  1.   Item 1 
  4.       1.1   Item 1a 
  5.       1.2   Item 1b 
  6.       ... 
  7.       1.9   Item 1c 
  8.       1.10  Item 1d 
  9.  2.   Item 2 
copyraw
// What I DON'T Want  (happens if you use list-style-position: outside)

1.   Item 1
      1.1  Item 1a
      1.2  Item 1b
      ... 
      1.9  Item 1c
     1.10  Item 1d
2.   Item 2
  1.  // What I DON'T Want  (happens if you use list-style-position: outside) 
  2.   
  3.  1.   Item 1 
  4.        1.1  Item 1a 
  5.        1.2  Item 1b 
  6.        ... 
  7.        1.9  Item 1c 
  8.       1.10  Item 1d 
  9.  2.   Item 2 

How?
I've been refining this based on several examples and the following solution seems to be the most stable:
copyraw
ol {
    list-style-type: none;
    counter-reset: myCounter;
    display: table;
    margin: 0;
    padding: 5px 0 5px 15px;
}

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

ol > li:before {
    content: counters(myCounter, ".") ". ";
    display: table-cell;
    position: relative;
    left:-12px;
}

li ol > li:before {
    content: counters(myCounter, ".") " ";
    position: relative;
    left:-15px;
}
  1.  ol { 
  2.      list-style-type: none; 
  3.      counter-reset: myCounter; 
  4.      display: table; 
  5.      margin: 0
  6.      padding: 5px 0 5px 15px
  7.  } 
  8.   
  9.  ol > li { 
  10.      counter-increment: myCounter; 
  11.      display: table-row; 
  12.  } 
  13.   
  14.  ol > li:before { 
  15.      content: counters(myCounter, ".") ". "
  16.      display: table-cell; 
  17.      position: relative; 
  18.      left:-12px
  19.  } 
  20.   
  21.  li ol > li:before { 
  22.      content: counters(myCounter, ".") " "
  23.      position: relative; 
  24.      left:-15px
  25.  } 

DEMO: See my JsFiddle at: jsfiddle.net/Jlipman/6vkw7tc3/4
Category: Cascading Stylesheets :: Article: 686

Credit where Credit is Due:


Feel free to copy, redistribute and share this information. All that we ask is that you attribute credit and possibly even a link back to this website as it really helps in our search engine rankings.

Disclaimer: Please note that the information provided on this website is intended for informational purposes only and does not represent a warranty. The opinions expressed are those of the author only. We recommend testing any solutions in a development environment before implementing them in production. The articles are based on our good faith efforts and were current at the time of writing, reflecting our practical experience in a commercial setting.

Thank you for visiting and, as always, we hope this website was of some use to you!

Kind Regards,

Joel Lipman
www.joellipman.com

Related Articles

Joes Revolver Map

Accreditation

Badge - Certified Zoho Creator Associate
Badge - Certified Zoho Creator Associate

Donate & Support

If you like my content, and would like to support this sharing site, feel free to donate using a method below:

Paypal:
Donate to Joel Lipman via PayPal

Bitcoin:
Donate to Joel Lipman with Bitcoin bc1qf6elrdxc968h0k673l2djc9wrpazhqtxw8qqp4

Ethereum:
Donate to Joel Lipman with Ethereum 0xb038962F3809b425D661EF5D22294Cf45E02FebF
© 2024 Joel Lipman .com. All Rights Reserved.