仅重用 scss 中另一个类的特定属性

问题描述

可以使用@extend 方法在 scss 中重用另一个类的所有属性。

.class1 {
   height: 10px;
   width: 10px;
}

.class2 {
   @extend .class1;
   border-color: #000000;
}

结果是

.class1,.class2 {
   height: 10px;
   width: 10px;
}

.class2 {
   border-color: #000000;
}

是否可以仅重用另一个类的自定义属性?类似的东西

.class2 {
   @extend .class1.height;
   border-color: #000000;
}

结果是这样的

.class1 {
   height: 10px;
   width: 10px;
}

.class2 {
   height: 10px;
   border-color: #000000;
}

非常感谢。

解决方法

你可以,但为此你需要扩展一个 placeholder 而不是一个类。您将所需的属性抽象为占位符,并应用于所需的类:

%custom-height {
   height: 10px;
}

.class1 {
   @extend %custom-height;
   width: 10px;
}

.class2 {
   @extend %custom-height;
   border-color: #000000;
}

这将导致:

.class1,.class2 {
   height: 10px;
}

.class1 {
   width: 10px;
}

.class2 {
   border-color: #000000;
}

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...