css - LESS 在第 n 个子选择器上添加空格

标签 css css-selectors less whitespace

我目前有这个酒糟代码:

.myclass{
    display:inline-block;
    overflow: auto;
    & li{
        display: none;
        &:nth-child(1),:nth-child(2),:nth-child(3){
            display:inline-block;
        }
    }
}

正如您所看到的,我想要的是只显示前三个 li 元素,其余的将显示为 none。但是编译使这个输出:

.myclass{
    display:inline-block;
    overflow: auto;
}
.myclass li{
    display: none;
}
.myclass li:nth-child(1),
.myclass li :nth-child(2),
.myclass li :nth-child(3) {
    display: inline-block;
}

正如你所看到的,:nth-child(2):nth-child(3) 中有一个空格,所以只有第一个 child 是工作正常。

如何删除这个空格?

最佳答案

你的问题是你只使用 LESS's immediate parent selector (&)在第一个 :nth-child 声明上,而不是在其他两个声明上。很明显,如果我们在每个声明之间添加换行符:

&:nth-child(1),
:nth-child(2),
:nth-child(3){
    display:inline-block;
}

要解决此问题,您只需将 & 放置在其他两个 :nth-child 声明之前即可:

&:nth-child(1),
&:nth-child(2),
&:nth-child(3){
    display:inline-block;
}

此外,您不需要在 li 选择器之前添加 &:

.myclass {
    li {
        ...
    }
}

关于css - LESS 在第 n 个子选择器上添加空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31839263/

相关文章:

html - 我怎样才能使 DIV 中的第一个元素具有与其他元素不同的 CSS?

javascript - 隔离较少的样式以响应组件

javascript - 我应该如何设置卡片的样式,以便每行可以有 3 张卡片?

html - 如何使用 css 为表格中的第 n 个 td 设置样式

jquery - 在 JSP 的 for 循环中添加 Li 元素,但 li a :first , last 或 nth child 总是给出最后一个元素

javascript - jquery从字符串重建选择器

css - 如何更改 Twitter 的 Bootstrap CSS 的颜色?

php - Slim Framework 404 css 问题

html - DIV 图像在 Firefox 中不显示

javascript - 固定顶部导航 + 使用 Jquery 的粘性页脚