SCSS mixin,如何连接单元? (px,%,em)

SCSS mixin, how to concatenate units? (px,%,em)

我想定义类喜欢

.bg-1 {
   width: 1%
}
.bg-2 {
   width: 2%
}

我正在尝试这样做:

@for $i from 1 through 100 {    
  .bg-#{$i} {
    width: #{$i};
  }
}

这至少编译,但打印出来

    .bg-1 {
       width: 1;
    }
    .bg-2 {
       width: 2;
    }

问题是如果我添加:

width: #{$i}%;

我得到了:

error sass/screen.scss (Line 294 of sass/modules/_classes.scss: Invalid CSS after "   $var: ": expected expression (e.g. 1px, bold), was "%;")

试试这个解决方案。有效。

@for $i from 1 through 100 {    
   .bg-#{$i} {
     $percentage: unquote("%");
     width: #{$i}$percentage;
   }
}

或者这样:

 @for $i from 1 through 100 {    
  .bg-#{$i} {
    width: #{$i}unquote("%");
  }
}

或者类似这样的,比较流行

@for $i from 1 through 100 {    
  .bg-#{$i} {
    width: round(percentage($i/100)); // Math function
    height: #{$i}px; // For px or em just concat
  }
}