我有这个SCSS代码:
$box-length: 12 !default;
@for $i from 1 through $box-length {
.box-#{$i} {
flex: 0 0 (100% / $box-length * $i);
}
}下面,我需要添加生成的.box-{num}类和其他选择器。要得到这样的CSS结果:
@media screen and (max-width: 768px) {
.row,
.column,
.box,
.box-1,
.box-2,
.box-3,
.box-4,
.box-5 {
/* ... */
}
}如何向.row, .column, .box追加动态.box-{num}类
谢谢!
发布于 2021-09-22 05:45:39
1.使用placeholder selector和@extend
@media screen and (max-width: 768px) {
%mq-768 {
/* ... */
}
}
.row,
.column,
.box {
@extend %mq-768;
}
$box-length: 12 !default;
@for $i from 1 through $box-length {
.box-#{$i} {
@extend %mq-768;
flex: 0 0 (100% / $box-length * $i);
}
}编译为:
@media screen and (max-width: 768px) {
.box-12,
.box-11,
[...],
.box-2,
.box-1,
.row,
.column,
.box {
/* ... */
}
}
.box-1 {
flex: 0 0 8.3333333333%;
}
.box-2 {
flex: 0 0 16.6666666667%;
}
[...]
.box-12 {
flex: 0 0 100%;
}2.使用变量存储选择器和append
$selectors: ".row, .column, .box";
$box-length: 12 !default;
@for $i from 1 through $box-length {
$boxSelector: ".box-#{$i}" ;
$selectors: append($selectors, $boxSelector, $separator: comma);
#{$boxSelector} {
flex: 0 0 (100% / $box-length * $i);
}
}
@media screen and (max-width: 768px) {
#{$selectors} {
color: blue;
}
}编译为:
.box-1 {
flex: 0 0 8.3333333333%;
}
.box-2 {
flex: 0 0 16.6666666667%;
}
[...]
.box-12 {
flex: 0 0 100%;
}
@media screen and (max-width: 768px) {
.row,
.column,
.box,
.box-1,
.box-2,
[...],
.box-11,
.box-12 {
/* ... */
}
}https://stackoverflow.com/questions/69276700
复制相似问题