【发布时间】:2011-09-26 15:41:03
【问题描述】:
我正在使用 less.js 并经常使用 mixins。例如。我确实有一个像这样的基本类“gradientBlack”。
.gradientBlack {
background: #333333;
background: -moz-linear-gradient(top, #5a5a5a 0%, #333333 60%, #000000 100%);
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #5a5a5a), color-stop(60%, #333333), color-stop(100%, #000000));
background: -webkit-linear-gradient(top, #5a5a5a 0%, #333333 60%, #000000 100%);
background: -o-linear-gradient(top, #5a5a5a 0%, #333333 60%, #000000 100%);
background: -ms-linear-gradient(top, #5a5a5a 0%, #333333 60%, #000000 100%);
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#5a5a5a', endColorstr='#000000', GradientType=0 );
background: linear-gradient(top, #5a5a5a 0%, #333333 60%, #000000 100%);
}
然后我在几个定义中重用这个类,比如
h3 {
.gradientBlack;
...
}
.darkBox {
.gradientBlack;
...
}
这种方法的一个缺点是,它使用冗余定义使 CSS 膨胀。例如。计算出的 CSS 可能与此类似。
h3 {
background: #333333;
background: -moz-linear-gradient(top, #5a5a5a 0%, #333333 60%, #000000 100%);
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #5a5a5a), color-stop(60%, #333333), color-stop(100%, #000000));
//... and maybe some more (redundant) definitions
}
.darkBox {
background: #333333;
background: -moz-linear-gradient(top, #5a5a5a 0%, #333333 60%, #000000 100%);
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #5a5a5a), color-stop(60%, #333333), color-stop(100%, #000000));
//... and maybe some more (redundant) definitions
}
对于像我这样使用大量渐变、圆角等的人来说,这加起来很快。
问题(已编辑)
我发现这个主题的已知名称是选择器继承(请参阅Sass),现在似乎还没有实现。用法和优点讨论here。此语法的计算 css 可能如下所示。
h3,
.darkBox,
.gradientBlack {
background: #333333;
background: -moz-linear-gradient(top, #5a5a5a 0%, #333333 60%, #000000 100%);
...
}
尽管如此,我将不胜感激任何建议,什么时候打扰,什么时候不打扰 - 以及任何其他主题提示如何继续,只要选择器继承不是一个选项。
【问题讨论】:
-
恕我直言,您提出的预期结果对我来说真的没有意义,因为它只是常规的 CSS。请说明您想要实现的目标...
-
嗨,bzx。是的,它是普通的CSS。但是比手写要容易得多。如果……就像上面的工作一样,我的类定义将始终保留在一个地方,而不是分散在各个地方(例如渐变、圆角、文本阴影等)。因此,掌握和更改布局会容易得多,而后者可以在不添加和/或删除我的(可能是复杂的)选择器的情况下在不同的地方完成。迎接阳光。
标签: css coding-style syntax less