5

I want to define classes like

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

And I'm trying with this:

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

This at least compiles, but prints out

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

the problem is that If I add:

width: #{$i}%;

I got:

error sass/screen.scss (Line 294 of sass/modules/_classes.scss: Invalid CSS after "   $var: ": expected expression (e.g. 1px, bold), was "%;")
Toni Michel Caubet
  • 19,333
  • 56
  • 202
  • 378

2 Answers2

5

Or something like this, which is more popular.

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

Try this solution. It works.

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

Or this:

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