Saiyam Gambhir Saiyam Gambhir - 9 days ago 5
Sass (Sass) Question

scss mixin for custom css properties

@mixin genericSidesStyles ($sides, $style, $cssProperty) {
@if($sides == '') {
$cssProperty: $style;
}
@else {
@each $side in $sides {
@if ($side == 'top' or $side == 'bottom' or $side == 'left' or $side == 'right' ) {
$cssProperty-#{$side}: $style;
}
}
}
}


This is a scss mixin for giving styles to css properties with sides like margin, padding, border etc.

I am calling my mixin as below

@include genericSidesStyles('top', 20px, 'margin');


here top is for margin-top, 20px is the distance and margin is the cssProperty but I am getting the following error


expected ':' after $cssProperty- in assignment statement


Help me to know where I am wrong in this

Answer

You need to interpolate the $cssProperty variable (so it becomes #{$cssProperty}, just like you have done with the $side variable. So your final code should be:

@mixin genericSidesStyles ($sides, $style, $cssProperty) {
  @if($sides == '') {
    #{$cssProperty}: $style;
  }
  @else {
    @each $cssProperty, $side in $sides {
      @if ($side == 'top' or $side == 'bottom' or $side == 'left' or $side == 'right' ) {
         #{$cssProperty}-#{$side}: $style;
       }
     }
  }
}