css - 是否可以在不使用 mixins 的情况下避免属性重复?
问题描述
如何避免属性重复?
我创建了一个以颜色为参数的 mixin(是唯一改变的属性)。有没有其他方法可以在不使用 mixins 的情况下重构它?
td.animal-table__cell {
&.animalType {
&.mammal span::before {
content: ' ';
width: 6px;
height: 6px;
border-radius: 3px;
background-color: red;
}
&.reptile span::before {
content: ' ';
width: 6px;
height: 6px;
border-radius: 3px;
background-color: green;
}
&.bird span::before {
content: ' ';
width: 6px;
height: 6px;
border-radius: 3px;
background-color: purple;
}
&.others span::before {
content: ' ';
width: 6px;
height: 6px;
border-radius: 3px;
background-color: orange;
}
}
}
解决方案
用逗号枚举来对常用样式进行分组:
td.animal-table__cell {
&.animalType {
&.mammal, &.reptile, &.bird, &.others {
span::before {
content: ' ';
width: 6px;
height: 6px;
border-radius: 3px;
}
}
&.mammal span::before {
background-color: red;
}
&.reptile span::before {
background-color: green;
}
&.bird span::before {
background-color: purple;
}
&.others span::before {
background-color: orange;
}
}
}
如果您知道无论如何都会在嵌套跨度上拥有这些样式,您甚至可以摆脱列表并span::before
直接放入。&.animalType
推荐阅读
- r - 从每个文本文件中提取特定行并存储在一个 txt 文件中
- javascript - 从 ember 组件类中获取 ember 组件的属性
- c - OpenSSL EVP_aes_128_cbc 解密得到意外结果
- python - 在 reset_index(drop=True) 之后添加列时,Pandas 数据帧显着提高了性能
- python - 如何在基于多列标准的熊猫中跳过 lambda 移位滚动函数中的单元格
- php - 从 ArgumentValueResolver 返回响应
- google-analytics - 无法接收来自 Google Measurement Protocol (Google Analytics v4) 的事件
- azure-data-explorer - Kusto 创建用于测试的内存表
- android - 如何将 CSV 文件加载到 Flutter 应用程序中?
- javascript - 为什么我提交新搜索时之前的结果没有清除?