【问题标题】:Counter does not increment in CSS计数器在 CSS 中不会增加
【发布时间】:2015-11-28 14:27:16
【问题描述】:

在这个例子中:

h3 {
  font-size: .9em;
  counter-reset: section;
}
h4 {
  font-size: .7em;
  counter-reset: subsection;
}
h3:before {
  counter-increment: section;
  content: counter(section)" ";
}
h4:before {
  counter-increment: subsection 2;
  content: counter(section)"." counter(subsection)" ";
}
<h3>Favorite Movies</h3>
<h4>District 9</h4>
<h4>The Matrix</h4>
<h4>The Cabin in the Woods</h4>

<h3>Others</h3>
<h4>Minority Report</h4>
<h4>Lord of the Rings</h4>
<h4>Fargo</h4>

我看到sectionsubsection 没有增加。

这段代码有什么问题?

【问题讨论】:

    标签: css css-counter


    【解决方案1】:

    计数器没有正确递增,因为您在它们自己的父级中重置它们。

    例如,每次浏览器遇到h3标签时,计数器section都会被重置(即值设置为0),因此当counter-incrementh3:before内被调用时( h3 的子代),它只是每次从 0 递增到 1。

    您应该在祖父母级别重置计数器(如果没有祖父母,则在 body)。

    body {
      counter-reset: section;
    }
    h3 {
      font-size: .9em;
      counter-reset: subsection;
    }
    h4 {
      font-size: .7em;
    }
    h3:before {
      counter-increment: section;
      content: counter(section)" ";
    }
    h4:before {
      counter-increment: subsection 2;
      content: counter(section)"." counter(subsection)" ";
    }
    <!-- at body reset section to 0 -->
    
    <h3>
      <!-- the h3 will inherit counter section from body (its parent) and increment to 1 in :before -->
      <!-- every time a h3 is encountered, the subsection is reset to 0 -->
      Favorite Movies
    </h3>
    <h4>
      <!-- this inherits subsection counter from previous sibling and increments value to 2 in :before -->
      District 9
    </h4>
    <h4>
      <!-- this inherits subsection counter and its value from previous sibling and increments value to 4 in :before -->
      The Matrix
    </h4>
    <h4>
      <!-- this inherits subsection counter and its value from previous sibling and increments value to 6 in :before -->
      The Cabin in the Woods
    </h4>
    
    <h3>
      <!-- the h3 will inherit counter section from body (its parent), its value from the previous sibling and increment to 2 in :before -->
      <!-- here the subsection counter is again reset to 0 because the subsection numbering has to restart -->
      Others
    </h3>
    <h4>
      <!-- this inherits subsection counter from previous sibling and increments value to 2 in :before -->
      Minority Report
    </h4>
    <h4>
      <!-- this inherits subsection counter and its value from previous sibling and increments value to 4 in :before -->  
      Lord of the Rings
    </h4>
    <h4>
      <!-- this inherits subsection counter and its value from previous sibling and increments value to 6 in :before -->  
      Fargo
    </h4>

    【讨论】:

    • 来自上一个兄弟姐妹? h3h4 的兄弟姐妹吗?
    • @overexchange:每个元素都将继承先前兄弟元素的计数器和值,因此节计数器的值也将传递给所有 h4 元素。为了简洁起见,我把它留了下来。您可以在我的回答here 中找到关于计数器继承的非常详细的解释。是的,根据所讨论的结构,第二个h3h4 的兄弟姐妹。只要元素共享同一个父元素,它们就是兄弟元素。这里h3h4 的父级是body
    • 据我所知,与其他 CSS 属性相比,计数器受到的关注相对较少。这是由于最有可能相对较少的使用。但是@Harry 的答案非常有用和有帮助。好问题,好答案。谢谢。
    • @Michael_B 你如何给列表项编号?
    • @overexchange,我使用默认编号或start 属性。我从未在生产中使用过计数器,也不是专家。因此,我很高兴阅读这些问题和答案,它们是有用的课程。