简体   繁体   English

CSS反增量和反重置

[英]CSS Counter-increment and counter-reset

Please can someone explain to me why I am getting '1' even though reset is not applied to the subsection counter.I mean should I not get 1.1, 1.2 ,1.3 instead of 1.1, 1.1,1.1 for all h2 tag under the same h1 tag?I am a beginner in learning CSS and it would help me a lot if any one could explain.Thanks in advance. 请问有人可以向我解释为什么即使没有将复位应用于小节计数器,我也得到'1'的意思。我的意思是我在同一个h1下不应该为所有h2标签获得1.1、1.2、1.3而不是1.1、1.1、1.1我是学习CSS的初学者,如果有人可以解释的话将对我有很大帮助。 The code is: 代码是:

<!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>

Output: 输出: 输出

Please try this: 请尝试以下方法:

Instead of 代替

h1 {
    counter-reset: section;
}

It will be 这将是

h1 {
    counter-reset: subsection;
}

See this very good article on counter-reset property. 看到这篇关于counter-reset属性的很好的文章。

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

As it said, h1 element is used to reset h2 counter-increment when a new h1 occurs in the code. 就像说的那样,当代码中出现新的h1时, h1元素用于重置h2 counter-increment

Example : 范例:

<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

So you have to use this code to make the reset works properly on h2:before counter : 因此,您必须使用此代码来使重置在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