最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How can I style a css order list to display 1-2, 3-4, 5-6, 7-8 etc - Stack Overflow

programmeradmin1浏览0评论

I want to output like so:

1-2 number one
3-4 number two
5-6 number three

This is my HTML-CSS code which I have tried

ol {
  list-style    : none;   /* Remove default numbering */
  counter-reset : item 0; /* Start counter at 0 */
  }
li {
  display           : block;
  counter-increment : item 2; /* Increment by 2 for each item */
}
li:before {
  /* Display as '1-2', '3-4', etc. */
  content           : counter(item) "-" counter(item, decimal) " "; 
  font-weight       : bold;
  counter-increment : item; /* Increment again to get the second number */
}
<ol class="custom-list">
  <li>number one</li>
  <li>number two</li>
  <li>number three</li>
</ol>

I want to output like so:

1-2 number one
3-4 number two
5-6 number three

This is my HTML-CSS code which I have tried

ol {
  list-style    : none;   /* Remove default numbering */
  counter-reset : item 0; /* Start counter at 0 */
  }
li {
  display           : block;
  counter-increment : item 2; /* Increment by 2 for each item */
}
li:before {
  /* Display as '1-2', '3-4', etc. */
  content           : counter(item) "-" counter(item, decimal) " "; 
  font-weight       : bold;
  counter-increment : item; /* Increment again to get the second number */
}
<ol class="custom-list">
  <li>number one</li>
  <li>number two</li>
  <li>number three</li>
</ol>

Which isn't producing the desired results... :/

Share Improve this question edited Feb 7 at 18:31 Mister Jojo 22.3k6 gold badges25 silver badges42 bronze badges asked Feb 7 at 18:18 hobbsiehobbsie 438 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

Simply use 2 counters:

ol {
  list-style    : none;
  counter-reset : c_1 -1 c_2 0; 
  }
li:before {
  font-weight       : bold;
  counter-increment : c_1 2 c_2 2; 
  content           : counter(c_1) '-' counter(c_2) ' '; 
  }
<ol class="custom-list">
  <li>number one</li>
  <li>number two</li>
  <li>number three</li>
</ol>

发布评论

评论列表(0)

  1. 暂无评论