如何在CSS注释中使用LESS变量?

Rie*_*bel 6 html css less

是否可以在CSS评论中为mixins使用LESS变量?我需要那些精神.

例如(不工作/仅替换图像路径):

.sprite (@width) {
    /** sprite: sprite-@{width}; sprite-image: url('../img/@{width}/sprite-@{width}.png'); sprite-layout: vertical */

    .picture {
        background-image: url('../img/@{width}/picture.png'); /** sprite-ref: sprite-@{width}; */
    }
}

.sprite(800);
Run Code Online (Sandbox Code Playgroud)

额外问题:在使用lessc编译后,我可以阻止background-image和sprite-comment之间的换行符吗?

Luk*_*age 2

不,你不能在注释中使用变量。

添加一个被浏览器忽略的属性“comment”怎么样?

您可以尝试使用转义字符串,例如

prop: ~"url('blah'); /* comment */";
Run Code Online (Sandbox Code Playgroud)

但它会产生 2 个分号(有效的 CSS)并且非常hacky。

  • 背景图像:~“url('../img/@{width}/picture.png'); /** sprite-ref: sprite-@{width}; */”; 之类的作品。但现在我遇到了相对路径的问题。使用客户端 less.js 解析器时不会加载图像。 (2认同)