Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use variable in another variable in SCSS

Tags:

sass

How I can use a variable in another variable? How I can do this:

$buttons: default, success, primary, info, warning, danger;

$default_color: #aaa;
$success_color: #bbb;
$primary_color: #ccc;
$info_color: #ddd;
$warning_color: #eee;
$danger_color: #fff;


@each $b in $buttons {
  .btn-#{$b} {
    color: $#{b}_color;
  }
}
like image 542
Chalist Avatar asked Jan 31 '26 21:01

Chalist


2 Answers

The way you are looking doesn't implement in SCSS yet but you can do something like this:

$default_color: #aaa;
$success_color: #bbb;
$primary_color: #ccc;
$info_color:    #ddd;
$warning_color: #eee;
$danger_color:  #fff;

$buttonsList:
  "default" $default_color,
  "success" $success_color,
  "primary" $primary_color,
  "info"    $info_color,
  "warning" $warning_color,
  "danger"  $danger_color;

@each $b in $buttonsList {
  .btn-#{nth($b, 1)} {
    color: nth($b, 2);
  }
}

Sassmeister link.

like image 181
Masoud Avatar answered Feb 04 '26 01:02

Masoud


Sass does not allow variables to be created or accessed dynamically. Use map instead http://sass-lang.com/documentation/file.SASS_REFERENCE.html#maps Example: https://stackoverflow.com/a/17978519

like image 36
Mohammad Niknam Avatar answered Feb 04 '26 00:02

Mohammad Niknam



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!