前言
在CSS3之前,只有ul和ol等列表元素中存在计数器,其他元素很难通过CSS实现计数功能。而CSS3之后,通过新增的counter属性,可以实现任意元素的计数。
使用
以本博客的代码行计数功能为例,代码标签构成方式如下:
页面展示效果如下:
可以看出,<pre>
标签嵌套<span>
标签,<pre>
标签为整段代码,<span>
标签为单行代码,因此计数代码行数也就是对<span>
标签的计数。
在后台CSS中增加以下CSS代码:
pre { ... position: relative; counter-reset: step 0; } pre span:before { position: absolute; content: counter(step); counter-increment: step 1; color: black; border-right: solid 2px #eb7350; }
其中counter
、counter-reset
、counter-increment
是最主要的属性,counter
用于设置计数器的名称,counter-reset
写在父元素中用于限定计数范围并将计数器重置为初始值,counter-increment
则写到子元素中,每次出现子元素,对计数器进行累加。
注意,通过将display属性设置为none或者hidden而隐藏的子元素不参与计数,而通过visibility:hidden等方式隐藏的子元素仍然参与计数。
得到以下效果:
像代码中一样,counter经常和伪元素搭配使用,避免污染被计数的元素,例如复制代码时不会复制到行数。
后话
为了兼容这个代码行数计算功能,我把之前发的上百篇博客挨个整理了一下,提前近一个月完成了早期博客的基本重构(尤其是被垃圾评论们骚扰最多的命令行工具nircmd,当时都写的什么玩意儿,直接重新写了),也算是拔了一月份立下的一个flag,后面的详细优化就可以慢慢来了。
女人固然是脆弱的,
母亲却是坚强的。
——维克多·雨果
评论
196860 668650hi this post support me full . .in case you want watches men pay a visit to my web sites is quite assist you for males watches. .thank man good job. 737459
Does your blog have a contact page? I’m having trouble locating it but, I’d like to send you an email. I’ve got some creative ideas for your blog you might be interested in hearing. Either way, great site and I look forward to seeing it grow over time.
https://www.circle13.com/hire-a-hacker-to-catch-a-cheating-spouse/
632125 361045have to do 1st? Most entrepreneurs are so overwhelmed with their online business plans that 156610
Thanks for this post, I am a big fan of this web site would like to continue updated.
https://youtu.be/I2GEhzob7p4
I really appreciate your work, Great post.
https://youtu.be/JVKOorD1_io
I have been surfing on-line more than 3 hours today, yet I by no means discovered any attention-grabbing article like yours. It is lovely value enough for me. Personally, if all web owners and bloggers made excellent content material as you did, the internet will probably be much more helpful than ever before. “Dignity is not negotiable. Dignity is the honor of the family.” by Vartan Gregorian.
https://youtu.be/gKr3OKOswAs
What Is Puravive? Before we delve into the various facets of the supplement, let’s start with the most important
https://youtu.be/q-Ty4LIXU7Q
Lottery Defeater Software? Lottery Defeater is a software application created to help people win lotteries
https://youtu.be/E4TAts1cZx4
very nice submit, i actually love this web site, keep on it
https://youtu.be/7BpUb06bzB4
I just could not depart your site prior to suggesting that I actually enjoyed the standard info a person provide for your visitors? Is going to be back often in order to check up on new posts
https://youtu.be/MF9LESRCb8Q
Hi my friend! I wish to say that this article is awesome, nice written and include approximately all important infos. I’d like to see more posts like this.
https://youtu.be/Rw9j0nwPfuM
You could definitely see your expertise within the work you write. The arena hopes for more passionate writers such as you who are not afraid to mention how they believe. Always go after your heart.
https://youtu.be/CbhDD-WcNx8