1

I'm trying to create a bunch of columns for a grid layout, and wondering if I can automate the width calculations with a SASS/SCSS mixin.

I'm currently writing

@mixin setWidth($a, $b){
 width : ($a / $b)*100%;
}
.col-1-2{
 @include setWidth(1, 2);
}

That's fine, but I'm just wondering if there's a way to make it even more DRY, by setting the class with a variable name and creating the class with a mixin.

Something like

@mixin setClass($a, $b){
  .col-$a-$b{
    width : ($a / $b)*100%;
  }
}

@include setClass(1, 2);
suryanaga
  • 3,728
  • 11
  • 36
  • 47

1 Answers1

5

You just need to interpolate the variables #{$a}when they are being used for naming conventions

@mixin setClass($a, $b){
  .col-#{$a}-#{$b} {
    width : ($a / $b)*100%;
  }
}

@include setClass(1, 2);

An example : http://sassmeister.com/gist/8634660fa4b32f50b4c1

Vangel Tzo
  • 8,885
  • 3
  • 26
  • 32