Saiyam Gambhir
Saiyam Gambhir

Reputation: 546

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

Upvotes: 0

Views: 739

Answers (1)

ESR
ESR

Reputation: 1689

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;
       }
     }
  }
}

Upvotes: 1

Related Questions