在web设计中,应用一种东倒西歪的办法来展现数据是非常重要的,这样用户就能够很清晰的了解网站所展现的数据结构和内容,应用有序列表就是实现数据有组织的展现的一种简略办法。
如果你须要更加深刻地管制有序列表数字的款式,你可能会感觉必须通过减少更多的 html DOM
构造或者通过 JavaScript
能力做到。侥幸的是,应用 CSS计数器
能够更加容易的解决这个问题。在本文中,次要介绍什么是 CSS计数器
和一些应用案例。
有序列表的问题
当你写了一个如下的有序列表,浏览器会主动在列表项后面加上数字
<ol> <li>My First Item</li> <li>My Second Item</li> <li>My Third Item</li></ol>
这看起来很好,然而它不容许你对数字进行款式调整。如果,你须要把列表前的数字放进一个圆圈里来润饰列表,你该怎么做呢?
一种办法是齐全删除列表,并本人手动增加数字。
<div> <span>1</span> My First Item</div><div> <span>2</span> My Second Item</div><div> <span>3</span> My Third Item</div>
div { margin-bottom:10px;}div span { display:inline-flex; align-items:center; justify-content:center; width:25px; height:25px; border-radius:50%; background-color:#000; color:#fff;}
这的确是咱们想要做的成果,然而也有一些毛病。首先,手动增加数字是很麻烦的。如果你须要更改一个编号,你必须一个接一个地扭转它们。面对这种状况,你能够应用 JavaScript
动静增加 <span>
标签来解决这些问题,但这会为 DOM
增加更多的节点,从而导致大量内存占用。
在大多数状况下,最好应用CSS计数器。让咱们来看看起因。
CSS计数器简介
CSS计数器
是网页范畴变量,其值能够应用 CSS
规定更改。
首先,应用 counter-reset
属性设置计数器。list-number
是此处应用的变量名。
div.list { counter-reset: list-number;}
接着,应用 counter-increment
属性来减少计数器的值。
div.list div { counter-increment: list-number;}
当初,每次呈现 div.listdiv
元素时,list-number
变量都会减少一。
最初,应用含有设置 content
属性和 counter()
函数的 :before
伪元素来展现数字。
div.list div:before { content: counter(list-number);}
这里是残缺代码:
<div class="list"> <div>My first item</div> <div>My second item</div> <div>My third item</div></div>
div.list { counter-reset: list-number;}/** 能够在:before 为元素中应用 counter-increment **/div.list div:before { counter-increment: list-number; content: counter(list-number);}
当初咱们还没有齐全达到目标。让咱们对 :before
伪元素进行款式设计,使其看起来更好。
div.list div:before { counter-increment: list-number; content: counter(list-number); margin-right: 10px; margin-bottom:10px; width:35px; height:35px; display:inline-flex; align-items:center; justify-content: center; font-size:16px; background-color:#d7385e; border-radius:50%; color:#fff;}
<iframe allowfullscreen="true" allowpaymentrequest="true" allowtransparency="true" class="cp_embed_iframe " frameborder="0" height="265" width="100%" name="cp_embed_1" scrolling="no" src="https://codepen.io/SupunKavinda/embed/OJybvoq?height=265&theme-id=dark&default-tab=css%2Cresult&user=SupunKavinda&slug-hash=OJybvoq&pen-title=OJybvoq&name=cp_embed_1" style="width: 100%; overflow:hidden; display:block;" title="OJybvoq" loading="lazy" id="cp_embed_OJybvoq"></iframe>
批改起始数字
默认状况下,counter-reset
会将计数器设置为 0。当第一个 counter-increment
被调用后它的起始变为1 能够通过将一个整数作为 counter-reset
函数的第二个参数来设置初始值。
div.list { counter-reset: list-number 1;}
如果你想从 0
开始,能够将初始值设置为 -1
。
div.list { counter-reset: list-number -1;}
更改增量值
默认状况下,counter-increment
会使计数器的值减少一。就像 counter-reset
一样,你能够定义 counter-increment
属性的偏移值。
在此示例中,counter-reset
将 list-number
设置为 0
。每次调用 counter-increment
时,list-number
数值都会减少 2
,因而,你将会看到列表序为 2
、4
和 6
。
div.list { counter-reset: list-number;}div.list div:before { counter-increment: list-number 2; // other styles}
计数器格局
counter()
函数能够有两个参数:counter-name
和 counter-format
。对于第二个参数,你能够应用任何无效的列表类型值,包含:
decimal
(e.g., 1, 2, 3…)lower-latin
(e.g., a, b, c…)lower-roman
(e.g., i, ii, iii…)
默认值为数字。
例如,如果你像我一样迷信,你能够应用 lower-greek
小写希腊字母作为编号的值。
div.list div:before { counter-increment: list-number; content: counter(list-number, lower-greek); // ... other styles}
计数器嵌套
应用嵌套订单列表时,始终以这种格局显示编号:
如果您须要子列表我的项目的数字编号(例如,1.1),您能够应用具备 counters()
性能的 CSS计数器
。
<ol> <li> My First Item <ol> <li>My Nested First Item</li> <li>My Nested Second Item</li> </ol> </li> <li>My Second Item</li></ol>
ol { list-style-type:none; counter-reset:list;}ol li:before { counter-increment:list; content: counters(list, ".") ". ";}
留神,咱们应用的是 counters()
函数,而不是 counter()
函数。
counters()
函数的第二个参数是连贯字符串。它还能够有第三个参数来设置格局(例如,希腊数字或罗马数字)。
带题目的嵌套计数器
元素,如 <h1>
,<h2>
不嵌套在文档中。它们以不同的元素呈现,但仍代表一种层次结构。上面介绍如何将嵌套数字设置到题目中:
body { counter-reset:h1;}h1 { counter-reset:h2;}h1:before { counter-increment: h1; content: counter(h1) ". ";}h2:before { counter-increment:h2; content: counter(h1) "." counter(h2) ". ";}
每次找到<h1>
时,<h2>
计数器都会重置。<h2>
在文档中取得的编号和 <h1>
相干。
Browser support
值得庆幸的是,CSS
计数器自与 CSS2
一起推出以来,失去了浏览器的广泛支持。尽管在内容以外的属性中应用 counter()
函数依然是实验性的,但你能够毫不犹豫地执行本教程中涵盖的所有例子。
一个简略挑战
您筹备好迎接波及CSS计数器的简略挑战了吗?
应用 CSS计数器
在 10
行代码中显示 1
到 1000
及其罗马字符。
如果你被难倒了,上面是你如何做到这一点:
要创立 1000
个 div
元素,能够应用以下内容。
for (var i = 0; i < 1000; i++) { document.body.appendChild( document.createElement("div") ); }
CSS计数器:
body { counter-reset:number;}div:before { counter-increment:number; content: counter(number) " => " counter(number, lower-roman);}
论断
CSS 计数器在 CSS 中是一个鲜为人知的性能,但您会诧异于它们派上用场的频率。在此教程中,咱们探讨了如何以及何时应用 CSS 计数器,并展现了一些示例。
以下是咱们应用的属性列表。
属性 | 用法 |
---|---|
counter-reset | 重置(或创立)给定值计数器(默认0) |
counter-increment | 通过给定偏移减少给定计数器(默认值 1) |
counter(counter-name, counter-format) | 从给定格局获取计数器的价值 |
counters(counter-name, counter-string, counter-format) | 从给定格局获取嵌套计数器的价值 |
CSS计数器
尽管很酷。但有一件事须要明确的是,所有计数器都是全局性的。如果你在一个有很多 CSS
文件的大型项目中应用,你可能无奈找到它们的创立、重置和增量地位。不要适度应用它们,肯定要应用描述性名称的计数器,以防止抵触。
一些实战例子
<style> html { box-sizing: border-box; font-size: 62.5%; } *, *::before, *:after { box-sizing: inherit; } body { font-family: Rambla, sans-serif; font-size: 2rem; line-height: 1.5; color: #03c03c; } h1 { text-align: center; } .wrapper { margin: 0 auto; width: 85%; display: -webkit-box; display: -webkit-flex; display: -ms-flexbox; display: flex; -webkit-justify-content: space-around; -ms-flex-pack: distribute; justify-content: space-around; } @media (max-width: 1100px) { .wrapper { -webkit-box-orient: vertical; -webkit-box-direction: normal; -webkit-flex-direction: column; -ms-flex-direction: column; flex-direction: column; -webkit-box-align: center; -webkit-align-items: center; -ms-flex-align: center; align-items: center; } } ol { counter-reset: li; margin: 20px 0; padding-left: 0; } ol>li { position: relative; margin: 0 0 25px 2em; padding: 4px 8px 4px 20px; list-style: none; } ol>li::before { content: counter(li); counter-increment: li; position: absolute; top: -2px; left: -2em; width: 2em; margin-right: 8px; padding: 4px; font-weight: bold; text-align: center; } li ol, li ul { margin-top: 6px; } ol ol li:last-child { margin-bottom: 0; } .disc>li::before { color: white; background-color: #03c03c; border-radius: 50%; } .circle>li::before { color: #03c03c; border: solid 2px #03c03c; border-radius: 50%; } .angle>li::before { color: #03c03c; border-right: solid 3px #03c03c; border-bottom: solid 3px #03c03c; } .shadow>li::before { color: white; background: #03c03c; box-shadow: 5px 5px 0 0 greenyellow; } .rombo>li { margin-bottom: 25px; } .rombo>li::before { color: white; z-index: 2; } .rombo>li::after { position: absolute; top: -2px; left: -2em; width: 2em; margin-right: 8px; padding: 4px; background-color: #03c03c; height: 2em; -webkit-transform: rotate(45deg); -ms-transform: rotate(45deg); transform: rotate(45deg); content: ''; z-index: 1; } .underline>li::before { border-bottom: solid 3px #03c03c; } </style><body> <h1>Styling Ordered List Numbers</h1> <div class="wrapper"> <ol class="disc"> <li>Tomato</li> <li>Cucumber</li> <li>Onion</li> <li>Pepper</li> </ol> <ol class="circle"> <li>Tomato</li> <li>Cucumber</li> <li>Onion</li> <li>Pepper</li> </ol> <ol class="angle"> <li>Tomato</li> <li>Cucumber</li> <li>Onion</li> <li>Pepper</li> </ol> <ol class="shadow"> <li>Tomato</li> <li>Cucumber</li> <li>Onion</li> <li>Pepper</li> </ol> <ol class="rombo"> <li>Tomato</li> <li>Cucumber</li> <li>Onion</li> <li>Pepper</li> </ol> <ol class="underline"> <li>Tomato</li> <li>Cucumber</li> <li>Onion</li> <li>Pepper</li> </ol> </div></body>
更多优良案例
https://css-tricks.com/custom...
文章地址:https://www.cnblogs.com/drago... 作者:dragonir