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. css

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. less

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


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

n 能够是数字、关键词或公式。 code

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


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

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

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

在这里,咱们为奇数和偶数 p 元素指定两种不一样的背景色: element

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

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

在这里,咱们指定了下标是 3 的倍数的全部 p 元素的背景色:

p:nth-of-type(3n+0){
background:#ff0000;
}
相关文章
相关标签/搜索