html – 如何在我的css代码中增加计数器?

前端之家收集整理的这篇文章主要介绍了html – 如何在我的css代码中增加计数器?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
参见英文答案 > html – CSS Counter Increment does not work3个
> Create line numbers on pre with CSS only3个
在我的HTML代码中,我定义了以下列表:
  1. <p class="list">first.</p>
  2. <p class="list">second.</p>

现在,我在css中:

  1. .list {
  2. display: table;
  3. }
  4. .list:before {
  5. display: table-cell;
  6. counter-increment: counter;
  7. content: counter(counter) '.';
  8. padding-right: 5px;
  9. }

并且页面上的结果是:

  1. 1. first
  2. 1. second

如何将第二个数字增加到2的值?

解决方法

您应该在列表的包装上使用counter-reset属性 – 请参阅下面的演示:
  1. body {
  2. counter-reset:counter;
  3. }
  4. .list {
  5. display: table;
  6. }
  7. .list:before {
  8. display: table-cell;
  9. counter-increment: counter;
  10. content: counter(counter) '.';
  11. padding-right: 5px;
  12. }
  1. <p class="list">first.</p>
  2. <p class="list">second.</p>

猜你在找的HTML相关文章