前言
在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,后面的详细优化就可以慢慢来了。
女人固然是脆弱的,
母亲却是坚强的。
——维克多·雨果
评论
It is the best time to make some plans for the longer term and it is time to be happy. I’ve read this publish and if I could I want to suggest you some attention-grabbing things or advice. Maybe you could write subsequent articles regarding this article. I want to learn more issues approximately it!
https://youtu.be/NFKYOfmt1Fw
Loving the info on this website , you have done outstanding job on the content.
https://youtu.be/R9ArZ-LGyeI
601251 616773my grandmother is always into herbal stuffs and she always say that ayurvedic medicines are the best stuff 828819
Everything is very open and very clear explanation of issues. was truly information. Your website is very useful. Thanks for sharing.
https://youtu.be/yW59aXmGVfE
990795 637436Heya just wanted to give you a brief heads up and let you know some of the pictures arent loading properly. Im not positive why but I think its a linking concern. Ive tried it in two different internet browsers and both show exactly the same outcomes. 435882
I discovered your weblog website on google and check a few of your early posts. Proceed to maintain up the very good operate. I simply additional up your RSS feed to my MSN News Reader. Searching for ahead to reading more from you in a while!…
https://youtu.be/-qqTpCK9DbI
53194 726845I conceive this internet internet site holds some real superb information for everybody : D. 3887
625442 808143its excellent as your other articles : D, regards for posting . 880003
Really enjoyed this blog post, can you make it so I get an alert email whenever you write a fresh article?
https://youtu.be/5-HarAQi8mY
I have recently started a blog, the information you provide on this site has helped me greatly. Thank you for all of your time & work. “A physicist is an atom’s way of knowing about atoms.” by George Wald.
https://youtu.be/WyFYL7cvPd0
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 suggestions for your blog you might be interested in hearing. Either way, great blog and I look forward to seeing it improve over time.
https://youtu.be/lFeouRgDjfk
762782 688792I discovered your weblog on yahoo and can bookmark it currently. carry on the nice function. 67202
823086 7105Wow Da weiss man, wo es hingehen muss Viele Grsse Mirta 664561
Attractive component to content. I just stumbled upon your weblog and in accession capital to claim that I acquire actually enjoyed account your blog posts. Anyway I will be subscribing for your feeds and even I fulfillment you access persistently rapidly.
https://youtu.be/GgmHS5mFzdk
397445 905041An intriguing discussion will probably be worth comment. I think which you basically write considerably a lot more about this subject, it may well become a taboo topic but usually consumers are inadequate to communicate in on such topics. To an additional. Cheers 552359
I beloved as much as you’ll receive performed right here. The caricature is attractive, your authored subject matter stylish. nonetheless, you command get got an impatience over that you want be turning in the following. in poor health unquestionably come more until now once more as precisely the same nearly very ceaselessly inside case you protect this increase.
https://youtu.be/Lo8WPaSu8u0
163801 569205Basically wanna remark that you have a quite nice web internet site , I enjoy the layout it in fact stands out. 998086
140059 834929I dugg some of you post as I thought they were handy extremely helpful 853349
With everything that appears to be building within this particular subject material, many of your points of view tend to be quite radical. Having said that, I appologize, but I do not give credence to your whole plan, all be it refreshing none the less. It seems to everyone that your opinions are not entirely validated and in simple fact you are your self not entirely certain of the point. In any case I did enjoy reading it.
https://youtu.be/zKl96YeFZYw
260991 544088superb post. Neer knew this, appreciate it for letting me know. 231855
I was more than happy to seek out this net-site.I wished to thanks on your time for this excellent read!! I positively having fun with every little bit of it and I’ve you bookmarked to take a look at new stuff you blog post.
https://youtu.be/1VwJ4jgGbHM
Thanks a lot for sharing this with all of us you really know what you are talking about! Bookmarked. Kindly also visit my web site =). We could have a link exchange agreement between us!
https://youtu.be/9qV8SnK5StI
Hey there! I’ve been reading your site for a while now and finally got the courage to go ahead and give you a shout out from New Caney Tx! Just wanted to mention keep up the great work!
https://youtu.be/LOW-VIcxNrM