Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sass - Setting variables with variables [duplicate]

I'm looking to get a solid answer on whether it's possible to reference variables based on the value of another variable. This is possible in PHP (among others).

Example (in Sass):

I want to set the border-color to $green (or $blue, $yellow, etc), which are variables being included from a partial. I am looping through a list of color names like such:

$colors = green, yellow, blue; 

@each $c in $colors {
&.#{$c} {
    border-color: $#{$c};
    }
}

The above code (ideally) generates classes for .green, .yellow, and .blue, each with a border-color set to $green, $yellow or $blue, respectively.

(Obviously) the above code is not working, but is there another way of achieving this in Sass/Compass?

like image 646
Mike Avatar asked Feb 15 '26 21:02

Mike


1 Answers

Looks like a duplicate of this thread: using hash with scss (which I've been watching/secretly pecking at for a few months now)

As Leo points out, you can try to work around this -- but PHP-style variable variables aren't something that the language currently supports.

like image 144
founddrama Avatar answered Feb 17 '26 09:02

founddrama



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!