简体   繁体   English

带嵌套div的SCSS计数器

[英]SCSS counter with nested divs

I have a problem with CSS and counter. 我的CSS和计数器有问题。 The second h3 should have the number "1.2" but in my case it has always the number "1.1". 第二个h3应该具有数字“ 1.2”,但在我的情况下,它始终具有数字“ 1.1”。

Is this a problem with the divs around the h3 or did I make a mistake with the counter-reset? 这是h3周围的div的问题,还是我在计数器重置中弄错了? Thanks for your help! 谢谢你的帮助! :) :)

I have the following HTML: 我有以下HTML:

 body { counter-reset: counterh2; } h2 { counter-reset: counterh3; } h3 { counter-reset: counterh4; } h4 { counter-reset: counterh5; } .container { >h2::before, >.div>h2::before { content: counter(counterh2, decimal) ". "; counter-increment: counterh2; } >h3::before, >.div>h3::before { content: counter(counterh2, decimal) "." counter(counterh3, decimal) ". "; counter-increment: counterh3; } >h4::before, >.div>h4::before { content: counter(counterh2, decimal) "." counter(counterh3, decimal) "." counter(counterh4, decimal) ". "; counter-increment: counterh4; } >h5::before, >.div>h5::before { content: counter(counterh2, decimal) "." counter(counterh3, decimal) "." counter(counterh4, decimal) "." counter(counterh5, decimal) ". "; counter-increment: counterh5; } } 
 <div class="container"> <div class="div"> <h2><span>Should be 1</span></h2> </div> <div class="div"> <h3><span>Should be 1.1</span></h3> </div> <div class="div"> <h3><span>Should be 1.2</span></h3> </div> </div> 

The h2 that resets the h3counter is in a different container than the h3's. 重置h3counter的h2与h3位于不同的容器中。 That means the h3counter will be in a different context than the h3's. 这意味着h3counter将与h3处于不同的上下文中。

Solution: don't reset the counter in the h2, but in its parent .div, which is in the same container as the divs containing the h3's. 解决方案:不要在h2中重置计数器,而是在其父.div中重置该计数器,该父对象与包含h3的div位于同一容器中。

.container .div:first-child {
  counter-reset: counterh3;
}

 body { counter-reset: counterh2; } .container .div:first-child { counter-reset: counterh3; } h3 { counter-reset: counterh4; } h4 { counter-reset: counterh5; } .container>h2::before, .container>.div>h2::before { content: counter(counterh2, decimal) ". "; counter-increment: counterh2; } .container>h3::before, .container>.div>h3::before { content: counter(counterh2, decimal) "." counter(counterh3, decimal) ". "; counter-increment: counterh3; } .container>h4::before, .container>.div>h4::before { content: counter(counterh2, decimal) "." counter(counterh3, decimal) "." counter(counterh4, decimal) ". "; counter-increment: counterh4; } .container>h5::before, .container>.div>h5::before { content: counter(counterh2, decimal) "." counter(counterh3, decimal) "." counter(counterh4, decimal) "." counter(counterh5, decimal) ". "; counter-increment: counterh5; } 
 <div class="container"> <div class="div"> <h2><span>Should be 1</span></h2> </div> <div class="div"> <h3><span>Should be 1.1</span></h3> </div> <div class="div"> <h3><span>Should be 1.2</span></h3> </div> </div> 

(Note: I had to translate the SCSS for this snippet, because snippet doesn't do SCSS, but you get the idea. All you need to replace is the first line of CSS I gave above.) (注意:我必须为此代码段翻译SCSS,因为该代码段不执行SCSS,但是您明白了。您需要替换的是上面给出的CSS的第一行。)

Thanks for your help. 谢谢你的帮助。 It works better, but now the h3 counter never resets when a new h2 appears. 它的效果更好,但是现在h3计数器永远不会在出现新的h2时重置。 This is my full HTML and CSS: 这是我完整的HTML和CSS:

<div class="container">
    <div class="article-text-unit">
        <h2><span>Hauptkapitel h2</span></h2>
        <div>
            <p>Sed sit amet elit scelerisque, tempor dui non, pharetra augue. Mauris fringilla sem sed magna molestie dignissim.</p>
        </div>
    </div>
    <div class="article-text-unit">
        <h3><span>Unterkapitel h3</span></h3>
        <div>
            <p>Sed sit amet elit scelerisque, tempor dui non, pharetra augue. Mauris fringilla sem sed magna molestie dignissim.</p>
        </div>
    </div>
    <div class="article-text-unit">
        <h3><span>Unterkapitel h3</span></h3>
        <div>
            <p>Sed sit amet elit scelerisque, tempor dui non, pharetra augue. Mauris fringilla sem sed magna molestie dignissim.</p>
        </div>
    </div>
    <div class="article-text-unit">
        <h2><span>Hauptkapitel h2</span></h2>
        <div>
            <p>Sed sit amet elit scelerisque, tempor dui non, pharetra augue. Mauris fringilla sem sed magna molestie dignissim.</p>
        </div>
    </div>
    <div class="article-text-unit">
        <h3><span>Unterkapitel h3</span></h3>
        <div>
            <p>Sed sit amet elit scelerisque, tempor dui non, pharetra augue. Mauris fringilla sem sed magna molestie dignissim.</p>
        </div>
    </div>
    <div class="article-text-unit">
        <h3><span>Unterkapitel h3</span></h3>
        <div>
            <p>Sed sit amet elit scelerisque, tempor dui non, pharetra augue. Mauris fringilla sem sed magna molestie dignissim.</p>
        </div>
    </div>
    <div class="article-text-unit">
        <h4><span>Unterkapitel h4</span></h4>
        <div>
            <p>Sed sit amet elit scelerisque, tempor dui non, pharetra augue. Mauris fringilla sem sed magna molestie dignissim.</p>
        </div>
    </div>
    <div class="article-text-unit">
        <h4><span>Unterkapitel h4</span></h4>
        <div>
            <p>Sed sit amet elit scelerisque, tempor dui non, pharetra augue. Mauris fringilla sem sed magna molestie dignissim.</p>
        </div>
    </div>
    <div class="article-text-unit">
        <h5><span>Unterkapitel h5</span></h5>
        <div>
            <p>Sed sit amet elit scelerisque, tempor dui non, pharetra augue. Mauris fringilla sem sed magna molestie dignissim.</p>
        </div>
    </div>
    <div class="article-text-unit">
        <h5><span>Unterkapitel h5</span></h5>
        <div>
            <p>Sed sit amet elit scelerisque, tempor dui non, pharetra augue. Mauris fringilla sem sed magna molestie dignissim.</p>
        </div>
    </div>
    <div class="article-text-unit">
        <h3><span>Unterkapitel h3</span></h3>
        <div>
            <p>Sed sit amet elit scelerisque, tempor dui non, pharetra augue. Mauris fringilla sem sed magna molestie dignissim.</p>
        </div>
    </div>
    <div class="article-text-unit">
        <h2><span>Hauptkapitel h2</span></h2>
        <div>
            <p>Sed sit amet elit scelerisque, tempor dui non, pharetra augue. Mauris fringilla sem sed magna molestie dignissim.</p>
        </div>
    </div>
</div>

And SASS: 和SASS:

body {
  counter-reset: counterh2;
}

.container .article-text-unit:first-child {
  counter-reset: counterh3;
}

h3 {
  counter-reset: counterh4;
}

h4 {
  counter-reset: counterh5;
}

.container {
  > h2::before,
  > .article-text-unit > h2::before {
    content: counter(counterh2, decimal) ". ";
    counter-increment: counterh2;
  }
  > h3::before,
  > .article-text-unit > h3::before {
    content: counter(counterh2, decimal) "." counter(counterh3, decimal) ". ";
    counter-increment: counterh3;
  }
  > h4::before,
  > .article-text-unit > h4::before {
    content: counter(counterh2, decimal) "." counter(counterh3, decimal) "." counter(counterh4, decimal) ". ";
    counter-increment: counterh4;
  }
  > h5::before,
  > .article-text-unit > h5::before {
    content: counter(counterh2, decimal) "." counter(counterh3, decimal) "." counter(counterh4, decimal) "." counter(counterh5, decimal) ". ";
    counter-increment: counterh5;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM