:nth-child(n) 匹配子元素的第 N 个,不论元素的类型
n 可以是数字、关键词或公式
Odd 和 even 是可用于匹配下标是奇数或偶数的子元素的关键词
当n是数字或关键词时, 第一个子元素的下标是1
p:nth-child(2)
{
background:#ff0000;
}
为奇数和偶数 p 元素指定两种不同的背景色:
p:nth-child(odd)
{
background:#ff0000;
}
p:nth-child(even)
{
background:#0000ff;
}
公式 (an + b)。描述:表示周期的长度,n 是计数器(从 0 开始),b 是偏移值。
p:nth-child(3n)
{
background:red;
}
p:nth-child(3n+1)
{
background:red;
}
p:nth-child(3n+2){
background:green;
}
:nth-of-type(n) 匹配子元素中特定类型元素的第 N 个
n 可以是数字、关键词或公式
Odd 和 even 是可用于匹配下标是奇数或偶数的子元素的关键词
当n是数字或关键词时, 第一个子元素的下标是1
p:nth-of-type(2)
{
background:#ff0000;
}
为奇数和偶数 p 元素指定两种不同的背景色:
p:nth-of-type(odd)
{
background:#ff0000;
}
p:nth-of-type(even)
{
background:#0000ff;
}
公式 (an + b)。描述:表示周期的长度,n 是计数器(从 0 开始),b 是偏移值。
p:nth-of-type(3n)
{
background:red;
}
p:nth-of-type(3n+1)
{
background:red;
}
p:nth-of-type(3n+2){
background:green;
}