【CSS 19】counters 计数器 自动编号 创建计数器属性 递增计数器属性 插入内容属性 嵌套计数器 counters()函数 创建概述列表

counters 计数器

CSS 计数器是由 CSS 保持的“变量”,其值可以通过 CSS 规则递增(以跟踪其使用次数)

计数器可以根据内容在文档中的位置来调整外观

带有计数器的自动编号
CSS 计数器就像“变量”
变量值可以通过 CSS 规则递增(跟踪使用次数)

如需使用 CSS 计数器,我们将使用以下属性:

  1. counter-reset - 创建或重置计数器
  2. counter-increment - 递增计数器值
  3. content - 插入生成内容
  4. counter() 或 counters()函数 - 将计数器的值添加到元素

如需使用 CSS 计数器,必须首先使用 counter-reset 创建它

<!--
下面的例子为页面(在 body 选择器中)创建一个计数器,然后为每个 <h2> 元素增加计数器值,并在每个 <h2> 元素的开头添加 "Section <value of the counter>:"
-->
<!DOCTYPE html>
<html>
<head>
<style>
body {
	counter-reset: section;
}

h2::before {
	counter-increment: section;
	content: "Section " counter(section) ": ";
</style>
</head>
<body>

<h1>CSS计数器</h1>
<h2>HTML教程</h2>
<h2>CSS教程</h2>
<h2>JavaScript教程</h2>

</body>
</html>

嵌套计数器

<!--
下面的例子为页面(section)创建一个计数器,为每个 <h1> 元素(subsection)创建一个计数器
"section" 计数器为每个 <h1> 元素计数,同时写入 "Section" 以及 section 计数器的值,"subsection" 计数器为每个 <h2> 元素计数,同时写入 section 计数器的值以及 subsection 计数器的值
-->
<!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>HTML教程:</h1>
<h2>HTML教程</h2>
<h2>CSS教程</h2>

<h1>Scripting教程</h1>
<h2>Javascript</h2>
<h2>VBScript</h2>

<h1>XML教程</h1>
<h2>XML</h2>
<h2>XSL</h2>

</body>
</html>

计数器对于创建概述列表也很有用,因为在子元素中会自动创建一个计数器的新实例
在这里,我们使用 counters() 函数在不同级别的嵌套计数器之间插入一个字符串

<!DOCTYPE html>
<html>
<head>
<style>
ol {
	counter-reset: section;
	list-style-type: none;
}

li::before {
	counter-increment: section;
	content: counters(section,".") " ";
}
</style>
</head>
<body>

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

<ol>
	<li>item</li>
	<li>item</li>
</ol>	

</body>
</html>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Zanebla

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值