counter-increment parent,但内容 child

counter-increment on parent, but content on child

我在使用 css 'counter-increment' 和 'counter-reset' 属性时遇到了一些问题。我已经让它与列表完美地工作了,但是当涉及到 header 标签时,我就不太清楚了。

设想以下布局:

<div id="root">
    <div class="section">
        <h1 class="header">Header</h1>
        
        <div class="section">
            <h2 class="header">Header</h2>
            <!-- can be nested multiple times -->
        </div>
    </div>
    <div class="section">
        <h1 class="header">Header</h1>
        <!-- content -->
    </div>
</div>

我希望对 header 标签进行编号(包括嵌套计数!)但是由于 header 标签本身没有嵌入,但它们的 parents 我不能让它正常工作。所以这就是我到目前为止得到的(基于逻辑,但不是最接近我想要实现的解决方案!我尝试了我能想到的所有可能的组合)

#root
{
    /* root should reset counter */
    counter-reset: section_header;
}

#root .section
{
    /* since every section has a header tag, we should count sections-divs, as they wrap children as well */
    counter-increment: section_header;
}

.section .header::before
{
    /* This would take care of the display, but I suspect it doesn't obtain the correct value since the scope of the counter is not available? */
    content: counters(section_header, ".") " ";
}

如果有任何解决方案(如果存在),我将不胜感激。

ps。我应该提到我主要在 Chrome 上测试这个,但我也在其他浏览器上发现了这个。

编辑

我正在寻找的格式是“1.1”、“1.2”、“1.2.1”等。每个嵌套部分都应该附加另一个计数器层,尽管我可以在没有分层的情况下使用它(单深度) ,动态 multi-layering 显然很难实现。

更新:我看到了 MDN 示例,您在创建它时可能会从中获得灵感。将其转换为您的标记的令人困惑的部分是它们使用包含子元素的 <ol><li> 标签。这在 header <h_> 元素的上下文中没有多大意义,但似乎您必须将子元素保留在其中才能完成同样的事情。我添加了 after 元素和一些光 css 来显示每个元素结束的位置。希望这有帮助。

.section {
  counter-reset: section;                /* Creates a new instance of the
                                            section counter with each ol
                                            element */
  list-style-type: none;
}

.header::before {
  counter-increment: section;            /* Increments only this instance
                                            of the section counter */
  content: counters(section, ".") " ";   /* Combines the values of all instances
                                            of the section counter, separated
                                            by a period */
}

.section{
  font-size: 16px;
  margin: 10px 0;
}
h1::after {
  content: ' (end h1)';
}

h2 {
  margin-left: 10px;
}
h2::after {
  content: ' (end h2)';
}

h3 {
  margin-left: 20px;
}
h3::after {
  content: ' (end h3)';
}
<div id="root">
  <div class="section">
    <h1 class="header">item</h1>          <!-- 1     -->
    <h1 class="header">item               <!-- 2     -->
      <div class="section">
        <h2 class="header">item</h2>      <!-- 2.1   -->
        <h2 class="header">item</h2>      <!-- 2.2   -->
        <h2 class="header">item           <!-- 2.3   -->
          <div class="section">
            <h3 class="header">item</h3>  <!-- 2.3.1 -->
            <h3 class="header">item</h3>  <!-- 2.3.2 -->
          </div>
         </h2>
         <h2 class="header">item 
          <div class="section">
            <h3 class="header">item</h3>  <!-- 2.4.1 -->
            <h3 class="header">item</h3>  <!-- 2.4.2 -->
            <h3 class="header">item</h3>  <!-- 2.4.3 -->
          </div>
        </h2>
        <h2 class="header">item</h2>      <!-- 2.5   -->
      </div>
    </h1>
    <h1 class="header">item</h1>          <!-- 3     -->
    <h1 class="header">item</h1>          <!-- 4     -->
  </div>
</div>