CSS3 :nth-of-type() Selector

The :nth-of-type(n) selector matches every element that is the nth child, of a particular type, of its parent.

n can be a number, a keyword, or a formula.

Tip: Look at the :nth-child() selector to select the element that is the nth child, regardless of type, of its parent.

The :nth-of-type() selector is supported in all major browsers, except IE8 and earlier.


:nth-of-type(n) 选择器匹配属于父元素的特定类型的第 N 个子元素的每个元素.

n 可以是数字、关键词或公式。

请参阅 :nth-child() 选择器,该选择器选取父元素的第 N 个子元素, 与类型无关


所有主流浏览器均支持 :nth-of-type() 选择器,除了 IE8 及更早的版本。

p:nth-of-type(2){
background:#ff0000;
}

Odd 和 even 是可用于匹配下标是奇数或偶数的子元素的关键词(第一个子元素的下标是 1)。

在这里,我们为奇数和偶数 p 元素指定两种不同的背景色:

p:nth-of-type(odd){
background:#ff0000;
}p:nth-of-type(even){
background:#0000ff;
}

使用公式 (an + b)。描述:表示周期的长度,n 是计数器(从 0 开始),b 是偏移值。

在这里,我们指定了下标是 3 的倍数的所有 p 元素的背景色:

p:nth-of-type(3n+0){
background:#ff0000;
}

你可能感兴趣的:(CSS3 :nth-of-type() Selector)