我正在用css的“counter increment”和“counter reset”属性进行一点串接。我已经能很好地处理列表了,但是当涉及到标题标签时,我就不那么清楚了。
想象一下下面的布局:
<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>
我希望标题标记被编号(包括嵌套计数!)但是由于头标签本身没有嵌入,但是他们的父母我不能让它正常工作。这就是我到目前为止得到的(基于逻辑,但不是我想要实现的最接近的解决方案!我尝试了所有我能想到的组合)
#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, ".") " ";
}
我会感激任何解决方案的方向(如果存在的话)。
我应该说我主要在Chrome上测试这个,但是我在其他浏览器上也发现了这个。
编辑
我要找的格式是“1.1”、“1.2”、“1.2.1”等。。每个嵌套的部分都应该附加另一个计数器层,尽管我可以在不分层(单深度)的情况下使用它,但是动态的多层是很难实现的。