counter-reset::用于创建或重置计数器。
counter-increment:用于增加计数器值。
content::用于插入生成的内容。
counter()或counters()函数:用于将计数器的值添加到元素。
<!DOCTYPE html>
<html>
<head>
<style>
body {
counter-reset: section;
}
h2::before {
counter-increment: section;
content: "Section " counter(section) ": ";
}
</style>
</head>
<body>
<h1>CSS Counter示例:</h1>
<h2>Java教程</h2>
<h2>HTML教程</h2>
<h2>CSS教程</h2>
<h2>Oracle教程</h2>
<p><strong>注意:</strong> IE8支持只有指定了!DOCTYPE时,这些属性才有效。</p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
body {
counter-reset: section;
}
h1 {
counter-reset: subsection;
}
h1::before {
counter-increment: section;
content: "Section " counter(section) ". ";
}
h2::before {
counter-increment: subsection;
content: counter(section) "." counter(subsection) " ";
}
</style>
</head>
<body>
<h1>Java教程:</h1>
<h2>Java核心教程</h2>
<h2>Servlet教程</h2>
<h2>JSP教程</h2>
<h2>Spring教程</h2>
<h2>Hibernate教程</h2>
<h1>Web技术教程:</h1>
<h2>HTML教程</h2>
<h2>CSS教程</h2>
<h2>jQuery教程</h2>
<h2>Bootstrap教程</h2>
<h1>Database教程:</h1>
<h2>SQL教程</h2>
<h2>MySQL教程</h2>
<h2>PL/SQL教程</h2>
<h2>Oracle教程</h2>
<p><strong>注意:</strong> IE8支持只有指定了!DOCTYPE时,这些属性才有效。</p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
ol {
counter-reset: section;
list-style-type: none;
}
li::before {
counter-increment: section;
content: counters(section,".") " ";
}
</style>
</head>
<body>
<h2>不同级别的嵌套counter</h2>
<ol>
<li>item</li>
<li>item
<ol>
<li>item</li>
<li>item</li>
<li>item
<ol>
<li>item</li>
<li>item</li>
<li>item</li>
</ol>
</li>
<li>item</li>
</ol>
</li>
<li>item</li>
<li>item</li>
</ol>
<p><b>Note:</b> IE8支持只有指定了!DOCTYPE时,这些属性才有效。</p>
</body>
</html>