Skip to content

Instantly share code, notes, and snippets.

@ntassone
Last active February 18, 2021 18:23
Show Gist options
  • Star 26 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save ntassone/9238396 to your computer and use it in GitHub Desktop.
Save ntassone/9238396 to your computer and use it in GitHub Desktop.
SASS - Loop through array to create class sets.
//Set Variables
$button-white: #ffffff;
$button-green: #44ca00;
$button-green-dark: #369a12;
$button-blue: #a6d1f9;
$button-blue-dark: #14283e;
$button-gray: #eeeeee;
$button-red: #9e0b0f;
//Create Array
$button-colors: (green $button-green white,
darkgreen $button-green-dark white,
blue $button-blue white,
darkblue $button-blue-dark white,
gray $button-gray #999,
red $button-red white
);
//Each loop to create classes
.button {
@each $type in $button-colors {
&.#{nth($type, 1)} {
background-color: #{nth($type, 2)};
color: #{nth($type, 3)};
&:hover {
background-color: darken(nth($type, 2), 5%);
}
}
}
}
@maxbailey
Copy link

I received this error code, from line 22.

Syntax error: Invalid CSS after "&.": expected class name, was "#006400"

@ntassone
Copy link
Author

@maxbailey are you running the latest version of sass? works for me, check out this pen: http://codepen.io/ntassone/pen/dslvt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment