谢谢你的帮助。它工作得更好,但现在 h3 计数器在新的 h2 出现时永远不会重置。这是我的完整 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>
和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;
}
}