我一直在尝试仅使用css选择器nth-child和/或nth-last-child中的前四个列表项目
<ul> <li>Hello first item</li> <li>Hello item 2<li> <li>Hello item 3</li> <li>Hello item 4</li> <li>Hello item 5</li> <li>Hello item 6</li> <li>Hello item 7</li> <li>Hello item 8</li> <li>Hello item 9</li> </ul>
我第一次尝试:
ul li:nth-child(1n):not(:nth-last-child(-n+5)){ font-weight:bold; }
但我想简化它
所以我的第二个:
ul li:nth-child(-n+4){ font-weight:bold; }
你能告诉我是否有比第二个更好的解决方案/这是针对第一个(n)孩子的正确方法吗?
解决方法
如果你修正了错字,你的代码是有效的:
虽然更好的解决方案可能是:
ul li:nth-child(-n+4) { font-weight:bold; }