这是 Sass 的实际行为和用例@extend
。
解释
为了清楚起见,更新您的代码如下
%red-color{
color: red
}
@mixin btn-structure ($text-case: null, $text-shadow: null, $decoration: none ){
display: inline-block;
text: {
decoration: $decoration;
transform: $text-case;
shadow: $text-shadow
}
}
.link-btn{
@extend %red-color;
@include btn-structure($text-case: 'uppercase', $decoration: underline);
}
.test-class{
@extend %red-color;
@include btn-structure($text-case: 'uppercase', $decoration: underline);
}
这将编译为,
.link-btn, .test-class {
color: red;
}
.link-btn {
display: inline-block;
text-decoration: underline;
text-transform: "uppercase";
}
.test-class {
display: inline-block;
text-decoration: underline;
text-transform: "uppercase";
}
如您所见,@extend
用于“将一组 CSS 属性从一个选择器共享到另一个选择器”,可以组合在一起(.link-btn, .test-class
)。然而,@include
用于在任何需要的地方插入样式,而不是棒状。
解决方案
根据您的要求,您可以诉诸@include
并声明一个mixin @mixin red-color
,如下所示,
%red-color{
color: red
}
@mixin red-color{
color: red
}
@mixin btn-structure ($text-case: null, $text-shadow: null, $decoration: none ){
display: inline-block;
text: {
decoration: $decoration;
transform: $text-case;
shadow: $text-shadow
}
}
.link-btn{
@include red-color;
@include btn-structure($text-case: 'uppercase', $decoration: underline);
}
输出
编译后的css将是,
.link-btn {
color: red;
display: inline-block;
text-decoration: underline;
text-transform: "uppercase";
}
希望这可以帮助。