首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >父母反增,子内容反增

父母反增,子内容反增
EN

Stack Overflow用户
提问于 2018-08-08 21:24:09
回答 1查看 1.1K关注 0票数 0

我有点怀疑的css‘反增量’和‘反重置’属性。我已经很好地处理了列表,但是当涉及到标题标记时,事情就变得不那么清晰了。

想象一下以下布局:

代码语言:javascript
复制
<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>

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

代码语言:javascript
复制
#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‘等等。每个嵌套的部分应该附加另一个计数器层,虽然我可以让它不需要分层(单深度),但是动态的多层是很难实现的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-08 22:01:16

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

代码语言:javascript
复制
.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)';
}
代码语言:javascript
复制
<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>

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51755849

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档