Need some explanation on HTML, nth-child

前端 未结 2 1546
感动是毒
感动是毒 2021-01-21 11:30

NOTE: SEE BELOW FOR CLEARER EXPLANATION

I\'m trying to figure out why this is happening.

jsFiddle 1 - Before

HTML

2条回答
  •  温柔的废话
    2021-01-21 11:45

    Isn't .big-chix:nth-child() only suppose to select all the .big-chix classes (which is 6 .big-chix), then set 1, 3, 5 to a background-color of #eee, and 2, 4, 6 to #aaa?

    No.

    :nth-child() selects "The nth element in the parent", not "The nth element that also matches the other parts of the selector".

    Each selector is applied independently and only elements that match all the components will match the complete selector.

    Note, however, that there is :nth-of-type() which should do what you want.

提交回复
热议问题