Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access nested SASS variables?

Several colors in this .scss file are defined as such:

$colors: (
  primary :   #cd0e11,
  secondary:  #23aa0b,
  danger:     #f53d3d,
  light:      #f4f4f4,
  dark:       #222
);

How do I access them? Say I want to set something to primary.

I tried:

h1 {
color: $colors:primary
}

This is Ionic 2, so it could be a framework specific thing.

like image 869
Ka Mok Avatar asked Dec 12 '16 18:12

Ka Mok


People also ask

Is it possible to nest variables within variables in SASS?

Interpolation of variable names is currently not possible in SASS.

Can I use SASS variables in JS?

Sass variables and JavaScript. Sass is a pre-processing language, meaning it's turned into CSS before it ever is a part of a website. For that reason, accessing them from JavaScript in the same way as CSS custom properties — which are accessible in the DOM as computed styles — is not possible.

Can I use CSS variables in SASS?

Sass variables are all compiled away by Sass. CSS variables are included in the CSS output. CSS variables can have different values for different elements, but Sass variables only have one value at a time.

Where do you declare variables in SASS?

Declaration of a variable in SASS: In SASS, you can define a variable by using $ symbol at the starting of the name of the variable and followed by its value.


2 Answers

I found out this is called SASS mapping. The following will work. It works like a key value fetcher.

color: map-get($colors, primary)

Check this out for more info on it.

like image 167
Ka Mok Avatar answered Oct 18 '22 02:10

Ka Mok


You could try this:

h1 {
   color: color($colors, primary);
}
like image 43
Mete Cantimur Avatar answered Oct 18 '22 00:10

Mete Cantimur