javascript - CSS nested ordered list numbered with alphanumerical characters - Stack Overflow

I am trying to make an ordered list where numbering style is alphanumerical characters in nested lists

I am trying to make an ordered list where numbering style is alphanumerical characters in nested lists too.

Example:

a. one
b. two
   b.a. three
   b.b. four
   b.c. five
c. six
   c.a. seven
      c.a.a. eight
   c.b. nine
      c.b.a. ten
d. eleven

I have tried setting list-style-type to lower-alpha but it doesn't number nested lists correctly. I have achived what I want with numbers like this:

 ol {
    counter-reset: section;  
    list-style-type: none;
 }
 li {
    counter-increment: section;
 }
 li::before {  
    content: counters(section,".") ". ";
}

Now I want the same thing but with alphanumerical characters as numbering style. The solution can be done in HTML, CSS or JS.

I am trying to make an ordered list where numbering style is alphanumerical characters in nested lists too.

Example:

a. one
b. two
   b.a. three
   b.b. four
   b.c. five
c. six
   c.a. seven
      c.a.a. eight
   c.b. nine
      c.b.a. ten
d. eleven

I have tried setting list-style-type to lower-alpha but it doesn't number nested lists correctly. I have achived what I want with numbers like this:

 ol {
    counter-reset: section;  
    list-style-type: none;
 }
 li {
    counter-increment: section;
 }
 li::before {  
    content: counters(section,".") ". ";
}

Now I want the same thing but with alphanumerical characters as numbering style. The solution can be done in HTML, CSS or JS.

Share Improve this question edited Jun 3, 2019 at 16:04 Freddy19 asked Jun 3, 2019 at 16:00 Freddy19Freddy19 1875 silver badges12 bronze badges 2
  • stackoverflow./questions/31859932/… – Paulie_D Commented Jun 3, 2019 at 16:06
  • 1 Possible duplicate of A styled ordered list whose nested list should have numbers with letters using CSS counter property. Here's a demo based on that answer. – showdev Commented Jun 3, 2019 at 16:36
Add a ment  | 

1 Answer 1

Reset to default 9

To achieve expected result, use type -'a' for orderlist

<ol type="a">
  <li>one</li>
  <li>two
  <ol type="a">
  <li>three</li>
  <li>four</li>
  <li>five</li>
</ol></li>
</ol>

Refer this link for different ordered types - https://developer.mozilla/en-US/docs/Web/HTML/Element/ol

type
Indicates the numbering type:
'a' indicates lowercase letters,
'A' indicates uppercase letters,
'i' indicates lowercase Roman numerals,
'I' indicates uppercase Roman numerals,
and '1' indicates numbers (default).

Code sample to achieve expected format

ol{
  list-style: none;
  counter-reset: item;
}

ol > li:before{
    counter-increment: item;
    content: counter(item, lower-alpha)".";
}

ol li ol{
    counter-reset: letter;
}
ol ol li:before{
    counter-increment: letter;
    content: counter(item, lower-alpha)"."counter(letter, lower-alpha) " ";
}
<ol type="a"> 
  <li>one</li>
  <li>two
  <ol>
  <li>three</li>
  <li>four</li>
  <li>five</li>
</ol></li>
</ol>

codepen - https://codepen.io/nagasai/pen/rgPgBO

Explanation:

  1. Reset list style for all ordered list and reset counter to initial default
  2. Add content - counter(item, lower-alpha)"." with list type lower-alpha for list and sublist
  3. Reset counter for sublist to start from initial default value (in this case - 'a')
  4. Use content: counter(item, lower-alpha)"."counter(letter, lower-alpha) " " to use existing order value from list followed by sublist counter

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744195836a4562653.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信