“Sel nth of type”的版本间差异
本页内容
(创建页面,内容为“{{DISPLAYTITLE::nth-of-type(n)}}14 = CSS3 :nth-of-type() Selector = 完整CSS选择器参考手册 指定每个p元素匹配同类型中的第2个同级兄弟元素的背景色: <sample title="" desc="" lang="html" hererun="1"> p:nth-of-type(2) { background:#ff0000; } </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title>…”) |
无编辑摘要 |
||
第53行: | 第53行: | ||
{| class="table table-striped table-hover" | {| class="table table-striped table-hover" | ||
! 选择器 | ! 选择器 | ||
! | ! Chrome | ||
! | ! Internet Explorer / Edge | ||
! | ! Firefox | ||
! | ! Safari | ||
! | ! Opera | ||
|- | |- | ||
| :nth-of-type() | | :nth-of-type() |
2022年8月19日 (五) 04:34的版本
CSS3 :nth-of-type() Selector
指定每个p元素匹配同类型中的第2个同级兄弟元素的背景色:
示例
p:nth-of-type(2)
{
background:#ff0000;
}
定义和用法
- nth-of-type(n)选择器匹配同类型中的第n个同级兄弟元素。
n可以是一个数字,一个关键字,或者一个公式。
提示: 请参阅 :nth-child() 选择器。该选择器匹配父元素中的第n个子元素。Look at the :nth-child() 。
浏览器支持
表格中的数字表示支持该属性的第一个浏览器版本号。
选择器 | Chrome | Internet Explorer / Edge | Firefox | Safari | Opera |
---|---|---|---|---|---|
:nth-of-type() | 4.0 | 9.0 | 3.5 | 3.2 | 9.6 |
更多实例
实例 1
奇数和偶数是可以作为关键字使用用于相匹配的子元素,其索引是奇数或偶数(该索引的第一个子节点是1)。
在这里,我们为奇数和偶数p元素指定两个不同的背景颜色:
示例
p:nth-of-type(odd)
{
background:#ff0000;
}
p:nth-of-type(even)
{
background:#0000ff;
}
Example 2
使用公式(an+ b).描述:a代表一个循环的大小,N是一个计数器(从0开始),以及b是偏移量。
在这里,我们对所有索引是3的倍数的p元素指定了背景颜色:
示例
p:nth-of-type(3n+0)
{
background:#ff0000;
}