简体   繁体   English

使用SASS生成具有动态宽度的列类

[英]Generating column classes with dynamic width using SASS

I was trying to generate a dynamic grid system using SASS with variable widths using a @for loop. 我试图使用@for循环使用具有可变宽度的SASS生成动态网格系统。 So far I have : 到目前为止,我有:

@for $value from 1 through 12 {

  $width: percentage(1/$value);

  .col-md-#{$value} {
    width: $width;
  }
}

The output for the above code would be: 上面代码的输出为:

.col-md-1 {
  width: 100%;
}

.col-md-2 {
  width: 50%;
}

.
.
.

.col-md-11 {
  width: 9.09091%;
}

.col-md-12 {
  width: 8.33333%;
}

Is there anyway this could be reversed by tinkering with the percentage functions in such a way that: 无论如何,可以通过以下方式修改百分比函数来扭转这种情况:

.col-md-12 {
  width: 100%;
}

and

.col-md-1 {
  width: 8.33333%;
}

The opposite of what I currently have. 与我目前所拥有的相反。 Thanks in advance. 提前致谢。

A simple change to the math should do the trick: 对数学进行简单的更改就可以解决问题:

@for $value from 1 through 12 {

  $width: percentage($value/12);

  .col-md-#{$value} {
    width: $width;
  }
}

results in: 结果是:

.col-md-1 {
  width: 8.33333%;
}

.col-md-2 {
  width: 16.66667%;
}

.col-md-3 {
  width: 25%;
}

.col-md-4 {
  width: 33.33333%;
}

.col-md-5 {
  width: 41.66667%;
}

.col-md-6 {
  width: 50%;
}

.col-md-7 {
  width: 58.33333%;
}

.col-md-8 {
  width: 66.66667%;
}

.col-md-9 {
  width: 75%;
}

.col-md-10 {
  width: 83.33333%;
}

.col-md-11 {
  width: 91.66667%;
}

.col-md-12 {
  width: 100%;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM