关于nth-child()的问题

span:nth-child(-n+3)
匹配前三个子元素中的span元素
为什么是匹配前3个元素??如果n是0开始的话,不应该是n+3吗?

阅读 2.7k
2 个回答
  1. span:nth-child(n+3) 感觉是匹配的从第三个往后的所有 span 元素

  2. n 应该是从 0,1,2,…… 加上负号才能保证 nth-chlid(里面的数字小于等于三)

Example:

-n+6 / represents the first 6 elements of the list /

The values of A and B can be negative, but only the positive results of An+B, >for n ≥ 0, are used.

你可以这样理解,n是从0开始计数(0,1,2...)
所以(-n+6) => (6,5,4,3,2,1)所以是前六个
上面的引用已经说了,只有正数会被使用。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题