:nth-child()不适用于类,它查找元素本身。您需要使用包装器来包装.container div,并使用以下内容:
.wrapper div:nth-child(n+3) {
/* put your styles here... */
}
澄清:nth-child()
使用.container:nth-child(n 3)可能会也可能不工作。因为:nth-child()伪类表示与选择器匹配的第n个子元素(在这种情况下为.container)。
如果.container元素不是其父项的第n个子元素,则不会被选中。
The :nth-child(an+b) pseudo-class notation represents an element
that has an+b-1 siblings before it in the document tree, for any
positive integer or zero value of n, and has a parent element.
考虑这个例子:
在这种情况下,.container:nth-child(2)将不会选择第二个div.container元素(具有第5个内容)。因为该元素不是其父项的第二个子节点,而是父节点的子树中。
另外,.container:nth-child(n3)将会选择所有的div.container元素,因为n从父的子树中的第一个元素开始为0,第一个div.container是其父代的第四个子节点。
n starts from 0
n = 0: (0 + 3) = 3 => 3rd element
n = 1: (1 + 3) = 4 => 4th element
n = 2: (2 + 3) = 5 => 5th element
...
因此,div.container:nth-child(n 3)表示匹配div.container选择器的所有第3,第4,第5,…子元素。