-1

I have several colours set up as classes

.black {background: $black;};
.red {background: $red;}
.yellow {background: $yellow;}
.grey {background: $grey;}
.white {background: $white;}
.blue {background: $blue;}
.full-black {background: #000;}
.light-black {background: #222;}

but i want to create a mixin which takes the class name and automatically creates the class name and fills in the background colour so i don't have to type this out every time..

I have tried something like this

@mixin colours ($black,$full-black,$light-black,$red,$blue,$yellow,$white,$grey) {
    .(colours): background:{colours};
}

But can't figure out the right code.

cimmanon
  • 67,211
  • 17
  • 165
  • 171
FoamyMedia
  • 486
  • 3
  • 14
  • 32

1 Answers1

2

you should use "associative array" to define the name of your colors and the code:

$colors: ("emerald": "#2ecc71", "carrot": "#e67e22");

@each $name, $hexa in $colors {
    .#{$name} {
      color: #{$hexa};
    }
}

I made a simple codepen to show you: http://codepen.io/pik_at/pen/MYGJqY

Pik_at
  • 1,459
  • 9
  • 14