LESS CSS 语法对现代化很有用
通常我使用 modernizr 来了解浏览器的功能。同时,我使用 LESS CSS 来使我的 css 更具可读性和可维护性。使用 LESS 嵌套规则的常见样式如下所示:
#header {
color: black;
.logo {
width: 300px;
color: rgba(255,255,255,.6);
&:hover { text-decoration: none }
}
}
然后,如果我使用 Modernizr 样式回退,我会为前一个块添加此文本:
.no-js #header,
.no-rgba #header {
.logo {
color: white;
}
}
所以,看起来我有两个代码分支,每次我需要检查另一个兼容性方面,分支机构的数量将会增加。这段代码的可维护性较差,因为您必须找到应用于每个元素的所有样式,并且我们使用嵌套类获得的好处消失了。
问题:LESS 语法中是否有一种方法可以包含此类回退,而不是为 .no-js 和其他 .no-smth 类启动新的代码分支?
Usually I use modernizr to find out the browser abilities. Same time, I use LESS CSS to make my css more readable and maintainable. Common style using LESS nested rules looks like this:
#header {
color: black;
.logo {
width: 300px;
color: rgba(255,255,255,.6);
&:hover { text-decoration: none }
}
}
Then, if I use modernizr style fall-back, I add this text for previous block:
.no-js #header,
.no-rgba #header {
.logo {
color: white;
}
}
So, it looks like I have two branches of code, and every time I need to check another compatability aspect the number of braches will grow. This code is less maintainable, because you have to find all the styles applied to every element, and the benefit we get using nested classes disappears.
The question: is there a way in LESS syntax to include such fall-backs and not starting a new code-branch for .no-js and other .no-smth classes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您现在可以使用
&
运算符来解决这个问题。以下语法应在 less.js、lessphp 和 dotless:这被编译为:
因此,对于给定的示例,您可以使用以下语法:
...这将是编译成:
You can now use the
&
operator to address this very problem. The following syntax should work in less.js, lessphp, and dotless:This is compiled into:
So, for the given example you could use the following syntax:
... which would be compiled into:
我认为解决这个问题的最佳方法是为这些后备提供一个单独的 .less 文件。我认为这最终会更容易管理,因此您不必在很多地方寻找
.no-rgba
。我确实尝试想出一个你想要的解决方案,但我没有运气。
The best way I can think to approach it is to just have a separate .less file for these fallbacks. I think this would end up much easier to manage so you don't have to peck around for
.no-rgba
in lots of places.I did try coming up with a solution how you wanted it but I had no luck.