css - SASS 匹配类名中的数字

标签 css math sass modulus

我正在寻找一种方法来匹配类名中的数字。

.col-2 {...}
.col-3 {...}
.col-4 {...}
...
.col-12 {...}

如果类名是数字的倍数,我想匹配。假设我要匹配 .col-3.col-6.col-9

现在我知道你可以做 (Number) mod(other number),但这不是类名的一部分。

编辑: 我找到了答案:

@include breakpoint(500px) {
    @for $i from 1 through $number-of-columns {
        @if $i % 4 == 0{
            [class$="#{$i}"] {
               background-color: red;
            }
        }
    }
}

最佳答案

根据这两个答案,我想到了这个 mixin:

SASS

@mixin col-mod($n, $max) {
    %mod-#{$n} {
        @content;
    }
    $i: $n;
    @while $i <= $max {
        .col-#{$i} {
            @extend %mod-#{$n}
        }
        $i: $i+$n;
    }
}

@include col-mod(3, 12) {
    /* Your styles here */
}

输出CSS

.col-3, .col-6, .col-9, .col-12 {
  /* Your styles here */
}

关于css - SASS 匹配类名中的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22249527/

相关文章:

html - 想要使用 Bootstrap 将一个完整页面拆分为四个相同大小的 div

html - 如何在 CSS 中创建复杂的阴影?

html - 100% div的外部和内部

algorithm - 如何将一组数字分成两个子集,这些子集的元素数量相等,并且总和尽可能接近?

Java 四舍五入到下一个 5000

algorithm - 对多边形的点进行排序以进行绘制

javascript - 如何从 webpack 入口点排除文件

html - SASS -scss 背景图像链接

html - 如何使用 <hr> 使两侧尺寸相同?

css - SASS 无法在 Visual Studio Code 1.33.0 中工作