nth-type-of 与 nth-child

:nth-type-of()

匹配父元素的第n个子元素E。

:nth-child()

匹配父元素的第n个子元素E,假设该子元素不是E,则选择符无效。
**

设置奇偶不同样式

  1. .list li:nth-of-type(n) {
  2. background: #fff;
  3. }
  4. .list li:nth-of-type(2n) {
  5. background: #57f4c6;
  6. }

image.png

设置前n个元素的样式

.list li:nth-of-type(-n + 3) {
  background: #57f4c6;
}

image.png

设置倒数n个元素的样式

.list li:nth-last-of-type(-n + 3) {
  background: #57f4c6;
}

image.png

设置从n - 1个起的元素的样式

.list li:nth-of-type(n + 3) {
  background: #57f4c6;
}

image.png

设置某一范围内元素的样式

.list li:nth-of-type(-n + 8):nth-of-type(n + 3) {
  background: #57f4c6;
}

image.png