css - 使用 nth-child 选择连续的组?

标签 css css-selectors

是否可以使用nth-child()来选择连续的元素?

我有一个包含 24 个元素的有序列表。我想将它们分成 4 列。目前我是这样做的:

#location-menu li:nth-child(1),
#location-menu li:nth-child(2),
#location-menu li:nth-child(3),
#location-menu li:nth-child(4) {
    margin-left: 0;
}
#location-menu li:nth-child(5),
#location-menu li:nth-child(6),
#location-menu li:nth-child(7),
#location-menu li:nth-child(8) {
    margin-left: 100px;
}

如您所见,到第 24 项时就会失控。最好的办法是使用 CSS3 列将列表拆分为多个列,但该站点在 IE8 中运行很重要。

我希望是这样的:

#location-menu li:nth-child(1-4) {
    margin-left: 0;
}
#location-menu li:nth-child(5-8) {
    margin-left: 0;
}

感谢您的帮助!

最佳答案

试试这个

#location-menu li:nth-child(n+0)  { margin-left: 0;     }
#location-menu li:nth-child(n+5)  { margin-left: 100px; }
#location-menu li:nth-child(n+9)  { margin-left: 200px; }
#location-menu li:nth-child(n+13) { margin-left: 300px; }
/* and so on */

关于css - 使用 nth-child 选择连续的组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8059449/

相关文章:

css - float :left prevents click on <a href> tag

html - 摆脱子菜单中的底部边框

css - 使用两个选择器时 IE8 中断?

css - 选择 body 的第 n 个 h3 元素

css - 如何在不使用位置 : absolute; 的情况下将图像堆叠在一起

javascript - 可以在传单 map 中设置 MBTiles 的样式吗?

html - CSS 链接不工作或什么的

css - :first-child and :nth-child 的奇怪行为

python - Selenium 在 PC 和 RPI 上的性能差异

css - 有没有更有效的方法来编写具有一对多关系的类的 CSS?