nth-of-type() 详解
·
nth-of-type()
详解
ele:nth-of-type(n)
表示选择父元素
下的第n
个ele
元素,其中n
可以是正整数、公式或者关键字。同时,ele
一般是标签选择器。
<style>
* {
margin: 0;
padding: 0;
}
.box {
width: 400px;
height: 200px;
margin: 100px auto;
border: 1px solid #ccc;
}
li {
padding-left: 16px;
list-style: none;
}
</style>
<div class="box">
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
</div>
1. n
作为正整数、
当 n
作为一个具体的数时,从 1
开始生效。
<style>
/* .... */
li:nth-of-type(1), li:nth-of-type(4) {
background-color: #eee;
color: #1890ff;
}
</style>
2. n
作为关键字
even | odd,选择偶数还是奇数。
<style>
/* .... */
li:nth-of-type(even) {
background-color: #eee;
color: #1890ff;
}
</style>
3. n
作为公式
作为公式时, n
是从 0
开始的。
<style>
/* .... */
li:nth-of-type(n+2):nth-of-type(-n+4) {
background-color: #eee;
color: #1890ff;
}
</style>
上面组合使用的,表示从第二行到第四行,闭区间。
也可以将其拆开使用, li:nth-of-type(n+2)
表示从第二行到最末尾,li:nth-of-type(-n+4)
表示从开始到第四行。
4. 作用范围:
ele:nth-of-type()
作用的是父元素下第几个 ele
元素,如果中间有其他元素,则略过。
<style>
* {
margin: 0;
padding: 0;
}
.box {
width: 400px;
height: 200px;
margin: 100px auto;
border: 1px solid #ccc;
}
p:nth-of-type(3) {
background-color: #eee;
color: #1890ff;
}
</style>
<div class="box">
<p>1</p>
<p>2</p>
<div>3-div</div>
<p>4</p>
<p>5</p>
</div>
阅读全文
AI总结
更多推荐
相关推荐
查看更多
鸿蒙开发工具大赶集

本仓将收集和展示鸿蒙开发工具,欢迎大家踊跃投稿。通过pr附上您的工具介绍和使用指南,并加上工具对应的链接,通过的工具将会成功上架到我们社区。
OpenManus

No fortress, purely open ground. OpenManus is Coming.
G-Star公益行

G-Star 公益行 是 GitCode G-Star 计划旗下专为公益机构打造的技术赋能计划,依托 GitCode 开源平台、生态流量、云计算与 AI 支持,旨在连接开源技术与公益组织,通过技术赋能帮助公益组织实现数字化转型,以提升运营效率、优化资源配置、拓展公益影响力。
目录
所有评论(0)