使用常规方法使用CSS计数器将标题数添加到标题时,使用header
标记时遇到问题。
它似乎在某种程度上干扰了counter
(s)
body {
counter-reset: h1
}
h1 {
counter-reset: h2
}
h2 {
counter-reset: h3
}
h1:before {
counter-increment: h1;
content: counter(h1)". "
}
h2:before {
counter-increment: h2;
content: counter(h1)"." counter(h2)". "
}
h3:before {
counter-increment: h3;
content: counter(h1)"."counter(h2)"." counter(h3)". "
}

<!DOCTYPE HTML>
<html>
<head>
<title>Headings counting</title>
</head>
<body>
<header>
<h1>First chapter</h1>
</header>
<h2>Sub Chapter</h2>
<h2>Sub Chapter</h2>
<h2>Sub Chapter</h2>
<h3>Sub sub section</h3>
</body>
</html>
&#13;
输出为:
header
代码output is as expected
后:
在H1周围使用header
标签是否会导致这种情况发生?它在Firefox&amp; Edge,不在Chrome&amp;戏
答案 0 :(得分:2)
只需将header
添加/添加到counter-reset
声明规则中的h1
,具体取决于您是否允许h1
使用header
标记。
body {
counter-reset: h1
}
header { /* add h1 if you going to use h1 without header tag */
counter-reset: h2
}
h2 {
counter-reset: h3
}
h1:before {
counter-increment: h1;
content: counter(h1)". "
}
h2:before {
counter-increment: h2;
content: counter(h1)"." counter(h2)". "
}
h3:before {
counter-increment: h3;
content: counter(h1)"."counter(h2)"." counter(h3)". "
}
<header>
<h1>First chapter</h1>
</header>
<h2>Sub Chapter</h2>
<h2>Sub Chapter</h2>
<h2>Sub Chapter</h2>
<h3>Sub sub section</h3>