Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using hash with scss

Tags:

sass

hash

hi i have many variables with colors. so want do something like this:

$culture: #00CC66;
$party:#CC9900;

 @each $i in culture, party {
.bl_#{$i}{
   border-left-color: #{$#{$i}};
}
}

for print:

bl_culture{border-left-color:#00cc66}
bl_party{border-left-color:#cc9900}

How can ido it?

thanks

like image 506
Luca Romagnoli Avatar asked Jun 15 '11 07:06

Luca Romagnoli


People also ask

What does #{} mean in sass?

Interpolation can be used almost anywhere in a Sass stylesheet to embed the result of a SassScript expression into a chunk of CSS. Just wrap an expression in #{} in any of the following places: Selectors in style rules. Property names in declarations. Custom property values.

How do I export variables in SCSS?

Exporting variables from SCSS stylesheet $myCustomVariable: #424242; After we defined anywhere in our SCSS stylesheet this variable, we are working with two custom SCSS functions. You should use the define-custom-property($name, $value) which accepts 2 parameters to define a new custom variable for export.

What is the difference between mixin and variable?

Mixin do some kind of styling which can be used again again in your whole styling and same you can use variable as much as you can. Functions and Mixins are different. One returns value while the other is used like a macro to substitute.


1 Answers

SASS 3.3 UPDATE
With some new functionality from sass 3.3 you can choose your variable names and do away with the annoying nth()

@each $name, $color in(
    'red'   $red,
    'green' $green,
) {
    .color-#{$name} {
        background-color: $color;
    }
}

ORIGINAL
It's a bit of a pain but you can get by with a list and then for looping through that.
For example:

$colorList:
    "red"    $red,
    "green"  $green
;

@each $i in $colorList{
    .color-#{nth($i,1)}{
         background-color:nth($i,2);
    }
}

Having pre defined these color vars elsewhere you get:

.color-red{
   background-color:#FF0000
}

.color-green{
   background-color:#00FF00
}
like image 80
Allan Hortle Avatar answered Sep 19 '22 06:09

Allan Hortle