Less CSS URL 变量始终在变量输出之前显示服务器基本 URL
考虑这个 LESS 代码:
#login-form-submit {
@base-url: "/webshop/rsc/img";
background-image: url("@{base-url}/icons/login.png");
}
输出 CSS 是:
#login-form-submit {
background-image: url("http://localhost:8080/webshop/rsc/css/specific//webshop/rsc/img/icons/login.png");
}
有谁知道为什么会发生这种情况?如果我放弃变量并直接使用字符串,CSS 会按预期输出。 (没有完全限定的 URL。)
运行的服务器是 jBoss EAP 5。
Consider this LESS code:
#login-form-submit {
@base-url: "/webshop/rsc/img";
background-image: url("@{base-url}/icons/login.png");
}
The output CSS is:
#login-form-submit {
background-image: url("http://localhost:8080/webshop/rsc/css/specific//webshop/rsc/img/icons/login.png");
}
Does anybody know why this might be happening? If I abandon the variable and use the string directly, the CSS outputs as expected. (Without the fully qualified URL.)
The server this is running on is jBoss EAP 5.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不幸的是,如果字符串以插值变量开头,则 LESS 会在开头应用相对 URL,从而出现问题。您可以这样:
有关更多信息,查看此问题。
Unfortunately, there is a problem where LESS is applying the relative URL to the beginning if the string starts with an interpolated variable. You could something like:
For more info, see this issue.
LESS 1.3.3 似乎已修复此问题。
This appears to be fixed with LESS 1.3.3.
在 LESS 中的这个错误得到修复之前(如 @rcl 提到的),解决方法是始终假设可以从另一个位置提供服务的媒体的绝对路径(例如开发中的本地服务器,生产中的 CDN)。
在哪里
Until this bug in LESS is fixed (as @rcl mentions), a workaround is to always assume an absolute path for media that may be served from another location (say local server in dev, CDN in production).
where
值得注意的是,您可以通过编译 less(在 JavaScript 中)而不是依赖浏览器“stylesheet/less”解析来解决此问题。通过 JavaScript 调用解析器似乎可以完全避免该问题。
It's worth noting that you can work around this issue by compiling the less (in JavaScript) rather then relying on the browser "stylesheet/less" parsing. Invoking the parser via JavaScript seems to somehow avoid the issue completely.