繁体   English   中英

CSS反增量和反重置

[英]CSS Counter-increment and counter-reset

请问有人可以向我解释为什么即使没有将复位应用于小节计数器,我也得到'1'的意思。我的意思是我在同一个h1下不应该为所有h2标签获得1.1、1.2、1.3而不是1.1、1.1、1.1我是学习CSS的初学者,如果有人可以解释的话将对我有很大帮助。 代码是:

<!DOCTYPE html>
<html>
<head>
<style>
body {
    counter-reset: section;
}

h1 {
    counter-reset: section;
}

h1:before {
    counter-increment: section;
    content: "Section " counter(section) ". ";
}

h2:before {
    counter-increment: subsection;
    content: counter(section) "." counter(subsection) " ";
}
</style>
</head>

<body>

<p><b>Note:</b> IE8 supports these properties only if a !DOCTYPE is specified.</p>

<h1>HTML tutorials</h1>
<h2>HTML Tutorial</h2>
<h2>XHTML Tutorial</h2>
<h2>CSS Tutorial</h2>

<h1>Scripting tutorials</h1>
<h2>JavaScript</h2>
<h2>VBScript</h2>

<h1>XML tutorials</h1>
<h2>XML</h2>
<h2>XSL</h2>

</body>
</html>

输出: 输出

请尝试以下方法:

代替

h1 {
    counter-reset: section;
}

这将是

h1 {
    counter-reset: subsection;
}

看到这篇关于counter-reset属性的很好的文章。

https://css-tricks.com/almanac/properties/c/counter-reset/

就像说的那样,当代码中出现新的h1时, h1元素用于重置h2 counter-increment

范例:

<h1>HTML tutorials</h1> // count 1 
<h2>HTML Tutorial</h2> // count 1.1 because you have one h1 above
<h2>XHTML Tutorial</h2> // count 1.2
<h2>CSS Tutorial</h2> // count 1.3

<h1>Scripting tutorials</h1> // count 2 because of the new h1 tag
<h2>JavaScript</h2> // count 2.1 because of the reset after the new h1 appears
<h2>VBScript</h2> // count 2.2

因此,您必须使用此代码来使重置在h2:before正常工作:

h1 {
    counter-reset: subsection // which is the name of h2 counter-increment;
}

暂无
暂无

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

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