5

This is a list I am working with:

* {
  margin: 0;
  padding: 0;
  font-family: Arial;
}

ol {
  counter-reset: item;
}

li {
  display: block;
}

li:before {
  content: counters(item, ".") " ";
  counter-increment: item;
}

ol li ol li {
  margin-left: 80px; /* The left margin should have exactly the width of one number and a   */
}
<ol>
  <li>Coffee
    <ol>
      <li>One</li>
      <li>Two</li>
      <li>Three</li>
    </ol>
  </li>
  <li>Tea
    <ol>
      <li>One</li>
      <li>Two</li>
      <li>Three</li>
    </ol>
  </li>
  <li>Milk</li>
  <li>Cool</li>
</ol>

The ol li ol li currently has margin-left: 80px;. Now it would be possible to manually adjust the px value in a way that for example 1.1 is perfectly left aligned with Coffee. But to make sure it will be always perfectly aligned, also with other fonts, I would like to insert an invisible space of one number and a &emsp;. With that, it should be perfectly aligned on one line.

Here is an image to show how it should look like: enter image description here

How can this be done? I would be very thankful for help.

Anna_B
  • 820
  • 1
  • 4
  • 23

1 Answers1

2

Float can do it. You make the first number float with a tiny bottom margin and it will push everything to the right by its width.

* {
  margin: 0;
  padding: 0;
  font-family: Arial;
}

ol {
  counter-reset: item;
}

li {
  display: block;
}

li:before {
  content: counters(item, ".") " ";
  counter-increment: item;
}

/* select li that has "ol" */
li:has(ol):before {
  float: left;
  margin-bottom: 5px; /* small value */
}

ol ol {
  overflow: hidden; /* create a formatting context for the float */
}
<ol>
  <li>Coffee
    <ol>
      <li>One</li>
      <li>Two</li>
      <li>Three</li>
    </ol>
  </li>
  <li>Tea
    <ol>
      <li>One</li>
      <li>Two</li>
      <li>Three</li>
    </ol>
  </li>
  <li>Milk</li>
  <li>Cool</li>
</ol>
Temani Afif
  • 245,468
  • 26
  • 309
  • 415
  • 2
    Thank you! Looks good. But what happened with the last item? "4 Cool" should be aligned same like "3 Milk". – Anna_B Mar 09 '23 at 12:00
  • li:not(ol ol li) is you make from this style a class (.list:not(ol ol li)) you can put it on only the items with an ol beneath it – Victor Pieper Mar 09 '23 at 12:08
  • @Anna_B I wil fix it, one moment – Temani Afif Mar 09 '23 at 12:13
  • @TemaniAfif Looks great, thank you! Is this "margin-bottom: 5px;" completely invisible? I don't really understand the sense of it. – Anna_B Mar 09 '23 at 12:18
  • @TemaniAfif I just wanted to add a jQuery function to show and hide items → https://jsfiddle.net/4789ozkx/  – Unfortunately, it works only without this "margin-bottom: 5px;". Do you know how to fix that? – Anna_B Mar 09 '23 at 12:25
  • @Anna_B well, that's a complete new code if you will consider jQuery. You need to also toggle the margin when you show the "ol" – Temani Afif Mar 09 '23 at 12:37
  • Oh okay. I thought it would be enough to change some CSS, and then just add the jQuery function. But in this case, I need to connect the CSS with jQuery? – Anna_B Mar 09 '23 at 12:46
  • _"You make the first number float"_ - that'll only work as long as the number of items stays in the single digits though. – CBroe Mar 09 '23 at 12:48
  • @CBroe I don't get your point – Temani Afif Mar 09 '23 at 13:04
  • @Anna_B yes, I made a solution based on your question. Adding jQuery will change the logic and you need something else but you can easily toggle the margin like you do with the visibility – Temani Afif Mar 09 '23 at 13:05
  • @TemaniAfif if you add a couple of list items on the top level, so that their number goes > 9, the result will look like this: https://i.stack.imgur.com/JCJ0J.png – CBroe Mar 09 '23 at 13:50
  • @CBroe Oh, true. If this case occurs, can a 0 be automatically added in front of the numbers with only one digit? – Anna_B Mar 09 '23 at 14:22
  • @Anna_B better edit your question with all your requirements to get accurate answers. You keep adding more in the comments and this won't be useful to anyone. – Temani Afif Mar 09 '23 at 14:29
  • @Anna_B https://stackoverflow.com/a/59369344/1427878 - in this case here, with `counters`, it needs to go in the third place, so `content: counters(item, ".", decimal-leading-zero) " ";` – CBroe Mar 09 '23 at 14:29
  • @TemaniAfif Since it's basically a new question, I accepted your answer and created a new topic: https://stackoverflow.com/q/75688292/12501851 – I would be very happy if you still could help there :) – Anna_B Mar 09 '23 at 17:50
  • @CBroe That's very interesting, thank you. I think it also must be connected to jQuery to only activate it if that's needed. – Anna_B Mar 09 '23 at 17:51