# How to use variable.scss $color in scss file

**URL:** <https://forum.ionicframework.com/t/how-to-use-variable-scss-color-in-scss-file/68813>\
**Category:** ionic-v3\
**Created:** [November 1, 2016, 11:57am UTC](https://forum.ionicframework.com/t/how-to-use-variable-scss-color-in-scss-file/68813 "2016-11-01T11:57:52Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![chanyap92](https://avatars.discourse-cdn.com/v4/letter/c/a3d4f5/32.png) [@chanyap92](https://forum.ionicframework.com/u/chanyap92)\
**Post date:** [November 1, 2016, 11:57am UTC](https://forum.ionicframework.com/t/how-to-use-variable-scss-color-in-scss-file/68813/1 "2016-11-01T11:57:52Z")

</div>

by default the ionic had provide the following color  
$colors: (  
primary: #EF5A28,  
secondary: #f5911e,  
danger: #f53d3d,  
light: #f4f4f4,  
dark: #222,  
favorite: #69BB7B,  
darkgraycustom: #333333,  
mediumgraycustom:#666666,  
lightgraycustom : #b2b2b2  
);

this is easy for use when we are directly bind to DOM using color=“primary”, but if i want to use in my .scss file then how to i access the color in this list?  
for example if i want my border to be the primary color? something like border:1px solid $color.primary?

---

<div class="post-metadata">

**Author:** ![OnnoG](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/onnog/32/104283_2.png) [@OnnoG](https://forum.ionicframework.com/u/OnnoG)\
**Post date:** [November 1, 2016, 2:50pm UTC](https://forum.ionicframework.com/t/how-to-use-variable-scss-color-in-scss-file/68813/2 "2016-11-01T14:50:50Z")

</div>

Use `map-get()`. For example, to set the text color in a css element to “primary”:

```
 color: map-get($colors, primary);
```

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [November 1, 2016, 7:11pm UTC](https://forum.ionicframework.com/t/how-to-use-variable-scss-color-in-scss-file/68813/3 "2016-11-01T19:11:07Z")

</div>

I believe a better way than `map-get` is:

```auto
color($colors, primary, base)

```

---

<div class="post-metadata">

**Author:** ![Robbannn](https://avatars.discourse-cdn.com/v4/letter/r/b5ac83/32.png) [@Robbannn](https://forum.ionicframework.com/u/Robbannn)\
**Post date:** [June 15, 2017, 11:43am UTC](https://forum.ionicframework.com/t/how-to-use-variable-scss-color-in-scss-file/68813/4 "2017-06-15T11:43:56Z")

</div>

Just providing a full example for clarity:

```auto
.some-class {
    color: color($colors, primary, base);
}

```

---

<div class="post-metadata">

**Author:** ![Ajith\_23](https://avatars.discourse-cdn.com/v4/letter/a/bcef8e/32.png) [@Ajith\_23](https://forum.ionicframework.com/u/Ajith_23)\
**Post date:** [July 11, 2017, 5:46am UTC](https://forum.ionicframework.com/t/how-to-use-variable-scss-color-in-scss-file/68813/5 "2017-07-11T05:46:24Z")

</div>

Hi,

I have changed the default values as

$colors: (  
primary : #fff,  
mp-main : #62687f,  
mp-secondary : #30bfbf,  
mp-background : #F5F7FA,  
mp-maintext : #4f5362,  
mp-subtext : #99a7b2  
);

will this cause any issue?

---

<div class="post-metadata">

**Author:** ![Minoruser](https://avatars.discourse-cdn.com/v4/letter/m/f1d935/32.png) [@Minoruser](https://forum.ionicframework.com/u/Minoruser)\
**Post date:** [October 20, 2017, 9:01am UTC](https://forum.ionicframework.com/t/how-to-use-variable-scss-color-in-scss-file/68813/6 "2017-10-20T09:01:28Z")

</div>

for clarity: that’s the right way to explain stuff

---

<div class="post-metadata">

**Author:** ![alex351](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/alex351/32/106752_2.png) [@alex351](https://forum.ionicframework.com/u/alex351)\
**Post date:** [November 14, 2017, 10:16am UTC](https://forum.ionicframework.com/t/how-to-use-variable-scss-color-in-scss-file/68813/7 "2017-11-14T10:16:56Z")

</div>

> [@OnnoG](#):
>
> color: map-get($colors, primary);

I get “undefined variable”. Your suggestion is not complete. How to import the variables from the variables file? Do I have to import the variables file into every component?

---

<div class="post-metadata">

**Author:** ![OnnoG](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/onnog/32/104283_2.png) [@OnnoG](https://forum.ionicframework.com/u/OnnoG)\
**Post date:** [November 14, 2017, 6:21pm UTC](https://forum.ionicframework.com/t/how-to-use-variable-scss-color-in-scss-file/68813/8 "2017-11-14T18:21:10Z")

</div>

Well, first of all try the way @rapropos suggested, i.e. instead of `map-get()` use `color($colors, primary, base)`.

Next, check that a `$color` array is defined in your `src/theme/variables.scss` file. All these variables are imported automatically.

PS: In principle you can define CSS/SASS variables wherever you want or need them. But the idea behind the theme colors is that they are defined for all components of your app in this central `variables.scss` file.

---

<div class="post-metadata">

**Author:** ![alex351](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/alex351/32/106752_2.png) [@alex351](https://forum.ionicframework.com/u/alex351)\
**Post date:** [November 15, 2017, 7:00am UTC](https://forum.ionicframework.com/t/how-to-use-variable-scss-color-in-scss-file/68813/9 "2017-11-15T07:00:07Z")

</div>

Thank you for your help.

Whatever I do, I get undefined. But I do have to say I am using the angular cli not the ionic one, if that makes a difference.

My folder structure:  
src  
|-app  
| / |-components  
| // |-somecomponent  
| /// |-somecomponent.scss  
|  
|- theme  
| / |-variables.scss  
| // |- $colors: (  
| /// primary: #333;  
| // );

Whenever I want to use my color variable inside a component scss file, I get undefined. Did not find anywhere how to import those variables or if Ionic or angular-cli or node-sass makes it for you.

---

<div class="post-metadata">

**Author:** ![huholmes](https://avatars.discourse-cdn.com/v4/letter/h/e36b37/32.png) [@huholmes](https://forum.ionicframework.com/u/huholmes)\
**Post date:** [January 12, 2018, 4:37pm UTC](https://forum.ionicframework.com/t/how-to-use-variable-scss-color-in-scss-file/68813/10 "2018-01-12T16:37:24Z")

</div>

thanks for the answer. Could you explain a little bit more about why color($colors, primary, base) is better than map-get($colors,primary)? thanks

---

<div class="post-metadata">

**Author:** ![14145057](https://avatars.discourse-cdn.com/v4/letter/1/c67d28/32.png) [@14145057](https://forum.ionicframework.com/u/14145057)\
**Post date:** [March 14, 2018, 5:37pm UTC](https://forum.ionicframework.com/t/how-to-use-variable-scss-color-in-scss-file/68813/11 "2018-03-14T17:37:44Z")

</div>

The reason you may be getting the undefined is due to `color: map-get($colors, primary);` being placed in the scss file before the $colors property, thus the reason you are getting undefined. Place it after the `$colors: ();` block and it will no longer give you an error.

---

<div class="post-metadata">

**Author:** ![alex351](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/alex351/32/106752_2.png) [@alex351](https://forum.ionicframework.com/u/alex351)\
**Post date:** [March 14, 2018, 8:06pm UTC](https://forum.ionicframework.com/t/how-to-use-variable-scss-color-in-scss-file/68813/12 "2018-03-14T20:06:00Z")

</div>

Thank you for your effort.

Anyhow, I find it logical that variables inside the theme and variables folders are loaded before the scss in the components. So I have no idea, why they would load differently.

---

<div class="post-metadata">

**Author:** ![ronyef](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/ronyef/32/77921_2.png) [@ronyef](https://forum.ionicframework.com/u/ronyef)\
**Post date:** [December 28, 2018, 2:50am UTC](https://forum.ionicframework.com/t/how-to-use-variable-scss-color-in-scss-file/68813/13 "2018-12-28T02:50:41Z")

</div>

On Ionic 4, use this:

.aClass {  
color: var(–ion-color-primary);  
}

---

<div class="post-metadata">

**Author:** ![Ratikanta](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/ratikanta/32/109400_2.png) [@Ratikanta](https://forum.ionicframework.com/u/Ratikanta)\
**Post date:** [December 18, 2019, 5:14pm UTC](https://forum.ionicframework.com/t/how-to-use-variable-scss-color-in-scss-file/68813/14 "2019-12-18T17:14:08Z")

</div>

In my case it was `var(--ion-color-primary);` double dash in the beginning. Maybe you have missed it.

---

<div class="post-metadata">

**Author:** ![Sylvinrodz](https://avatars.discourse-cdn.com/v4/letter/s/e68b1a/32.png) [@Sylvinrodz](https://forum.ionicframework.com/u/Sylvinrodz)\
**Post date:** [March 16, 2020, 9:32am UTC](https://forum.ionicframework.com/t/how-to-use-variable-scss-color-in-scss-file/68813/15 "2020-03-16T09:32:53Z")

</div>

in ionic 4  
color:var(–ion-color-primary);

---

<div class="post-metadata">

**Author:** ![mhartington](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mhartington/32/137757_2.png) [@mhartington](https://forum.ionicframework.com/u/mhartington)\
**Post date:** [July 19, 2021, 9:01pm UTC](https://forum.ionicframework.com/t/how-to-use-variable-scss-color-in-scss-file/68813/16 "2021-07-19T21:01:24Z")

</div>

Closed on July 19, 2021, 9:01pm UTC


