css - 更好的第 n 个子选择器

标签 css less css-selectors

我的 LESS 是这样的,

.img-preview {
   &:first-child:nth-last-child(6),
   &:first-child:nth-last-child(6) ~ a:nth-child(2),
   &:first-child:nth-last-child(6) ~ a:nth-child(3),
   &:first-child:nth-last-child(6) ~ a:nth-child(4),
   &:first-child:nth-last-child(6) ~ a:nth-child(5) {}
}

如您所见,如果只有 6 个子元素,我将尝试选择除最后一个元素之外的所有元素。除了像上面那样重复编写代码,有没有更好的方法来编写它。

我已经尝试了以下,但是在编译时它给出了错误。

.img-preview {
       &:first-child:nth-last-child(6) ~ a:not(:last-child) {}
}

最佳答案

~ a:not(:last-child) 部分匹配第一个 child 之后的元素,因此不是第一个 child 本身。您仍然需要为此选择 &:first-child:nth-last-child(6):

.img-preview {
    &:first-child:nth-last-child(6),
    &:first-child:nth-last-child(6) ~ &:not(:last-child) {}
}

关于css - 更好的第 n 个子选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25397079/

相关文章:

php - 将 wordpress 帖子 ID 添加到 div

css - 无法使用 bootstrap rails 元素在图像上设置 css z-index

css - LESS 编译 border-radius : 50%/20%

node.js - 在 Windows 上的 Symfony 2 项目中编译 Bootstrap 3

html - 如何使用 CSS 正确选择第一个或最后一个 child ?

css - "a"和 "a:link"之间的区别

css - Python 使用 Selenium 查找元素

jquery - 带有标签的自定义 HTML 复选框

jquery - 如何在 jcarousel 中获得可见的中间 li?

css - Twitter Bootstrap 3 导航栏的风格