CSS leading zeros for decimals not working with large numbers
P粉018548441
P粉018548441 2023-11-07 10:35:53
0
1
742

I recently encountered a problem where I created an ordered list containing over a hundred list items. I set list-style:decimal-leading-zero;

ol{
  list-style: decimal-leading-zero;
}
<ol>
<li>Some item</li>
<li>Some item</li>
<li>Some item</li>
<li>Some item</li>
<li>Some item</li>
<li>Some item</li>
<li>Some item</li>
<li>Some item</li>
<li>Some item</li>
<li>Some item</li>
<li>Some item</li>
<li>Some item</li>
</ol>
<p>...</p>
<ol start="96">
<li>Some item</li>
<li>Some item</li>
<li>Some item</li>
<li>Some item</li>
<li>Some item</li>
<li>Some item</li>
<li>Some item</li>
<li>Some item</li>
<li>Some item</li>
<li>Some item</li>
<li>Some item</li>
<li>Some item</li>
</ol>

Problem: Only the first nine list items have leading zeros.

Expected: Two leading zeros in the first nine list items, and one leading zero in the 10th to 99th list items.

P粉018548441
P粉018548441

reply all(1)
P粉576184933

There is no list-style for three decimal places.

However, you can use pseudo-selectors and counters a> to achieve what you want here.

Reference Code:

ol {
  counter-reset: items;
}

li {
  display: block;
  counter-increment: items;
}

li:before {
  content: "00" counter(items)". ";
}

li:nth-child(n+10):before {
  content: "0" counter(items)". ";
}

li:nth-child(n+100):before {
  content: counter(items)". ";
}
<ol>
  <li>Item 1</li>
  <li>Item 2</li>
  <li>Item 3</li>
  <li>Item 4</li>
  <li>Item 5</li>
  <li>Item 6</li>
  <li>Item 7</li>
  <li>Item 8</li>
  <li>Item 9</li>
  <li>Item 10</li>
  <li>Item 11</li>
  <li>Item 12</li>
  <li>Item 13</li>
  <li>Item 14</li>
  <li>Item 15</li>
  <li>Item 16</li>
  <li>Item 17</li>
  <li>Item 18</li>
  <li>Item 19</li>
  <li>Item 20</li>
  <li>Item 21</li>
  <li>Item 22</li>
  <li>Item 23</li>
  <li>Item 24</li>
  <li>Item 25</li>
  <li>Item 26</li>
  <li>Item 27</li>
  <li>Item 28</li>
  <li>Item 29</li>
  <li>Item 30</li>
  <li>Item 31</li>
  <li>Item 32</li>
  <li>Item 33</li>
  <li>Item 34</li>
  <li>Item 35</li>
  <li>Item 36</li>
  <li>Item 37</li>
  <li>Item 38</li>
  <li>Item 39</li>
  <li>Item 40</li>
  <li>Item 41</li>
  <li>Item 42</li>
  <li>Item 43</li>
  <li>Item 44</li>
  <li>Item 45</li>
  <li>Item 46</li>
  <li>Item 47</li>
  <li>Item 48</li>
  <li>Item 49</li>
  <li>Item 50</li>
  <li>Item 51</li>
  <li>Item 52</li>
  <li>Item 53</li>
  <li>Item 54</li>
  <li>Item 55</li>
  <li>Item 56</li>
  <li>Item 57</li>
  <li>Item 58</li>
  <li>Item 59</li>
  <li>Item 60</li>
  <li>Item 61</li>
  <li>Item 62</li>
  <li>Item 63</li>
  <li>Item 64</li>
  <li>Item 65</li>
  <li>Item 66</li>
  <li>Item 67</li>
  <li>Item 68</li>
  <li>Item 69</li>
  <li>Item 70</li>
  <li>Item 71</li>
  <li>Item 72</li>
  <li>Item 73</li>
  <li>Item 74</li>
  <li>Item 75</li>
  <li>Item 76</li>
  <li>Item 77</li>
  <li>Item 78</li>
  <li>Item 79</li>
  <li>Item 80</li>
  <li>Item 81</li>
  <li>Item 82</li>
  <li>Item 83</li>
  <li>Item 84</li>
  <li>Item 85</li>
  <li>Item 86</li>
  <li>Item 87</li>
  <li>Item 88</li>
  <li>Item 89</li>
  <li>Item 90</li>
  <li>Item 91</li>
  <li>Item 92</li>
  <li>Item 93</li>
  <li>Item 94</li>
  <li>Item 95</li>
  <li>Item 96</li>
  <li>Item 97</li>
  <li>Item 98</li>
  <li>Item 99</li>
  <li>Item 100</li>
  <li>Item 101</li>
  <li>Item 102</li>
</ol>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template