4

如果每个元素都嵌套在单独的元素中,我如何计算一个元素?

    .variant--name::before {
      counter-increment: section;
      content: "Abschnitt " counter(section) ": ";
    }
<div class="variant--group">
<h3 class="variant--name">variant</h3>
</div>

<div class="variant--group">
<h3 class="variant--name">variant</h3>
</div>

<div class="variant--group">
<h3 class="variant--name">variant</h3>
</div>

4

1 回答 1

5

你还需要counter-reset

body {
  counter-reset: section;
}
.variant--name::before {
  counter-increment: section;
  content: "Abschnitt " counter(section)": ";
}
<div class="variant--group">
  <h3 class="variant--name">variant</h3>
</div>
<div class="variant--group">
  <h3 class="variant--name">variant</h3>
</div>
<div class="variant--group">
  <h3 class="variant--name">variant</h3>
</div>

编辑:您可以创建元素并counter-reset在其上使用,因此现在为每个parent元素创建 counter Fiddle的新实例,而不是在 body Fiddle上重置计数器

于 2016-04-12T19:05:51.837 回答