您必须使用 CSS 预处理器来执行此操作。
SASS
placeholder
%rounded-corner {}
%corner {}
%button-effective {}
.button {
@extend %rounded-corner;
@extend %corner;
@button-effective;
/* Some other styles. */
}
.box {
@extend %rounded-corner;
}
编译为:
.button, .box {
/* rounded-corner styles */
}
.button {
/* corner styles here */
}
.button {
/* button-effective styles here */
}
.button {
/* Some other styles. */
}
/*
`.box` is NOT defined here because it only uses placeholders. So it
is placed where the placeholder is defined.
*/
注意: 使用占位符时,CSS 选择器会添加到定义占位符的任何位置。不是定义选择器的地方。
extend
.rounded-corner {}
.corner {}
.button-effective {}
.button {
@extend .rounded-corner;
@extend .corner;
@extend .button-effective
// Continue with other classes.
}
编译为:
.rounded-corner, .button {}
.corner, .button {}
.button-effective, .button {}
mixin
@mixin rounded-corner {}
@mixin corner {}
@mixin button-effective {}
.button {
@include .rounded-corner;
@include .corner;
@include .button-effective
// Continue with other classes.
}
编译为:
.button {
/* rounded-corner styles here */
/* corner styles here */
/* button-effective styles here */
}
少
LESS 具有与 SASS 相似的 sytanx,并且也具有 extend 和 mixin,但如果您想将一个类的样式添加到另一个类,LESS 会更宽容一些。虽然我相信在 LESS 中仍然被认为是一个 mixin,但您可以将一种类样式添加到另一种类样式中,如下所示,而无需使用关键字。
.rounded-corner {}
.corner {}
.button-effective {}
.button {
.rounded-corner;
.corner;
.button-effective;
// Continue with other classes.
}
编译为:
.button {
/* rounded-corner styles here */
/* corner styles here */
/* button-effective styles here */
}