【问题标题】:LESS mix-in for nth-child?第n个孩子的混入更少?
【发布时间】:2013-08-09 16:59:55
【问题描述】:

我正在尝试制作一个 LESS mixin,它会给我这个输出:

.resource:nth-child(8n+1) { clear: left; }

到目前为止我已经得到了这个:

.wrap-every(@n) {
    &:nth-child(@n + "n+1") {  // parse error on this line
        clear: left;
    }
}

.resource {
    .wrap-every(8);
}

但它在指示的行上给出了一个解析错误

ParseError:无法识别的输入

有没有办法做到这一点?

【问题讨论】:

    标签: css less mixins


    【解决方案1】:

    小于 >= 1.4

    你可以这样做:

    .wrap-every(@n) {
      &:nth-child(@{n}n + 1) {
            clear: left;
        }
    }
    

    这应该有所需的输出。无需任何 hack。

    在 Less 的旧版本中

    你可以试试简单的字符串插值

    .wrap-every(@n) {
        @t: ~":nth-child(@{n}n + 1)";
        &@{t} {
            clear: left;
        }
    }
    

    和两种情况下的输出 CSS 应该是这样的:

    .resource:nth-child(8n + 1) {
      clear: left;
    }
    

    【讨论】:

    • +1 用于包含 @{n} 的新语法,因为在之前的 le​​ss 版本中我们可以只使用 @n 语法。
    • 使用最近的 dotless(用于 .net)和旧版本对我有用。谢谢。
    • 这解决了我的问题!以防万一有人搜索,我试图将nth-last-child 包含在mixin 中的变量中,并收到错误Missing closing ')'。正在驱使我香蕉。 @{variable} nth-last-child 中的语法已修复!
    • nth-last-child 的语法是什么?上述技术对我不起作用
    猜你喜欢
    • 2017-03-11
    • 1970-01-01
    • 1970-01-01
    • 2016-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多