# Overriding component SASS variables in dynamic themes

**URL:** <https://forum.ionicframework.com/t/overriding-component-sass-variables-in-dynamic-themes/115186>\
**Category:** ionic-v3\
**Created:** [December 12, 2017, 11:18pm UTC](https://forum.ionicframework.com/t/overriding-component-sass-variables-in-dynamic-themes/115186 "2017-12-12T23:18:43Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![loki9182](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/loki9182/32/103293_2.png) [@loki9182](https://forum.ionicframework.com/u/loki9182)\
**Post date:** [December 12, 2017, 11:18pm UTC](https://forum.ionicframework.com/t/overriding-component-sass-variables-in-dynamic-themes/115186/1 "2017-12-12T23:18:43Z")

</div>

I’m working on allowing my users to flip between two themes at runtime. light-theme and dark-theme. I’m having trouble using sass variables within my two classes.

In my variables.scss, I import my two scss files as follow:

```auto
@import "theme.light";
@import "theme.dark";

```

In theme.light.scss I have the following code:

```auto
.light-theme {
  $customColor: yellow;
  $list-md-background-color: $customColor;

  .item {
    background-color: $customColor;
  }
}

```

At the root of my application, I apply the selectedTheme dynamically as follow:

`<ion-nav [root]="rootPage" #content swipeBackEnabled="false" [class]="selectedTheme"></ion-nav>`

This partially works when I defined classes in my theme files as follow:

```auto
.item {
    background-color: $customColor;
  }

```

but it does not work when attempting to define sass variables to customize ionic components as follow:

$list-md-background-color: #ff0000;

Perhaps I’m misunderstanding how to use SASS but is there a way to override Ionic component SASS variable in separate theme files?

---

<div class="post-metadata">

**Author:** ![robinyo](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/robinyo/32/115964_2.png) [@robinyo](https://forum.ionicframework.com/u/robinyo)\
**Post date:** [December 13, 2017, 12:01am UTC](https://forum.ionicframework.com/t/overriding-component-sass-variables-in-dynamic-themes/115186/2 "2017-12-13T00:01:22Z")

</div>

Ionic theming is based on you overriding the (shared) Sass variables found in Ionic’s source scss files.

For example (`variables.scss`):

```auto
$text-color: white !default;
$link-color: color($colors, light) !default;
$toolbar-background: transparent !default;

```

Then:

```auto
@import "ionic.theme.default";

@import "./green-and-blue-theme";
@import "./facebook-messenger-theme";

```

See: [https://github.com/Robinyo/big-top/blob/master/src/theme/variables.scss](https://github.com/Robinyo/big-top/blob/master/src/theme/variables.scss)

And in `app.html`:

`<ion-nav main [root]="rootPage" #content swipeBackEnabled="false" [class]="theme"></ion-nav>`

See: [https://github.com/Robinyo/big-top/blob/master/src/app/app.html](https://github.com/Robinyo/big-top/blob/master/src/app/app.html)

And in `app.component.ts`:

```auto
  public toggleTheme() {

    if (this.theme === 'facebook-messenger-theme') {
      this.theme = 'green-and-blue-theme';
    } else {
      this.theme = 'facebook-messenger-theme';
    }
  }

```

See: [https://github.com/Robinyo/big-top/blob/master/src/app/app.component.ts](https://github.com/Robinyo/big-top/blob/master/src/app/app.component.ts)

Ref: [https://robferguson.org/blog/2017/11/12/theming-your-ionic-3-app/](https://robferguson.org/blog/2017/11/12/theming-your-ionic-3-app/)

---

<div class="post-metadata">

**Author:** ![loki9182](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/loki9182/32/103293_2.png) [@loki9182](https://forum.ionicframework.com/u/loki9182)\
**Post date:** [December 13, 2017, 1:27pm UTC](https://forum.ionicframework.com/t/overriding-component-sass-variables-in-dynamic-themes/115186/3 "2017-12-13T13:27:21Z")

</div>

Thanks for the example Rob. It seems to confirm what I was thinking.  
When importing theme files, it doesn’t look like you can override Ionic component SASS variables. In your variables.scss file, you make global changes as follow:

```auto
$text-color: white !default;
$link-color: color($colors, light) !default;
$toolbar-background: transparent !default;

```

but once you get into the individual theme files, you make modifications to the classes themselves instead of the sass variables:

```auto
.green-and-blue-theme {
  ion-content {
    @include linear-gradient(left,$green-and-blue);
    -webkit-background-size: cover;
    -moz-background-size: cover;
    background-size: cover;
  }
}

```

I was hoping to “skin” my app with various theme files using the Ionic SASS variables. Do you know if there is a way to do that?

Otherwise, I’d have to inspect the resulting html and override the values of the resulting classes such as ion-content and toolbar-background, etc… kind of a pain. (But then again, scrolling through the list of Ionic scss variables is not exactly a time saver either). Would there be a downside to creating multiple theme files who each override 10-20 classes like ion-content and toolbar-background and not using the sass variables?

---

<div class="post-metadata">

**Author:** ![barreola](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/barreola/32/116725_2.png) [@barreola](https://forum.ionicframework.com/u/barreola)\
**Post date:** [January 24, 2018, 8:53pm UTC](https://forum.ionicframework.com/t/overriding-component-sass-variables-in-dynamic-themes/115186/4 "2018-01-24T20:53:52Z")

</div>

Did you resolve it? I have the same question!  
I have plenty of custom themes, but I cannot change de color of the tabs icon, I’ve tried with `$tabs-tab-color-active: #ff0000;` but that’s globally, and red does’nt fit with black, jaja.

---

<div class="post-metadata">

**Author:** ![loicsalou](https://avatars.discourse-cdn.com/v4/letter/l/45deac/32.png) [@loicsalou](https://forum.ionicframework.com/u/loicsalou)\
**Post date:** [April 14, 2018, 5:31pm UTC](https://forum.ionicframework.com/t/overriding-component-sass-variables-in-dynamic-themes/115186/5 "2018-04-14T17:31:33Z")

</div>

it’s simply not possible. sass is transpiled to css at build time so the variables become part of the final CSS, and hence they’re static. This means that they are statically coded in the CSS.  
I’m also looking for some smart solution to achieve the same goal as you but one thing I’m pretty sure is that sass does not offer any way how to achieve this…  
The only solution I can see so far is to target tags and components in the theme css files. Not ideal IMHO.

---

<div class="post-metadata">

**Author:** ![loki9182](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/loki9182/32/103293_2.png) [@loki9182](https://forum.ionicframework.com/u/loki9182)\
**Post date:** [April 14, 2018, 7:39pm UTC](https://forum.ionicframework.com/t/overriding-component-sass-variables-in-dynamic-themes/115186/6 "2018-04-14T19:39:36Z")

</div>

I got it working decently well. Here is how:

In each page’s scss, I set only the layout for my components. No colors. In the html, I always set two classes. One for the layout and one for the color.

Then in my Themes folder, I added two scss files. theme.dark.scss and theme.light.scss.

In my variables.scss I import both files:

```auto
@import "theme.light";
@import "theme.dark";

```

My two theme files go as follow:

```auto
.dark-theme {

  $bodyBackground: #110521;
  $navBarBackground: #040f1f;
  $canceled: #ffea00;
  $textColor: #e3e3e3;
  $rowColor: linear-gradient(to bottom, #30304c 0%, #1a2232 49%, #0e1113 100%);
  $rowBorder: thin solid rgba(35, 34, 71, 0.7);
  $facebook: #4267B2;
  $fadedText: rgba(255, 255, 255, 0.5);
  $menuBorder: rgba(128, 128, 128, 0.51);

  $sectionHeaderBackground: linear-gradient(90deg, rgb(26, 59, 74) 0%, rgb(28, 32, 51) 20%, rgba(20, 19, 18, 0) 60%);
  $tableHeaderBackground: linear-gradient(to bottom, rgba(42,42,51,1) 0%, rgba(20,57,82,1) 51%, rgba(20,19,18,1) 100%);
  $tableBackground: #000048;
  $tableBorder: thin solid grey;
  $tableRowBackground: linear-gradient(to bottom, rgba(72,72,87,1) 0%, rgba(42,78,102,1) 51%, rgba(57,62,92,1) 100%);

  .sectionHeaderColors {
    background: $sectionHeaderBackground;
  }
}

```

My theme.light.scss of course starts with

`.light-theme {...`

and contains the same color classes. Only the $variables at the top change from one theme to the other.

You’ll notice that my theme files only specify colors. In my various pages, I could go specify that one of my section header is affected by 2 classes. .sectionHeaderLayout and .sectionHeaderColors

The trick is to switch the class that’s applied at the root of your app so that you are using theme.dark or theme.light. The result is that you’ll switch from using all the color classes from one theme to the other.

My app.html contains:

`<ion-nav [root]="rootPage" #content swipeBackEnabled="false" [class]="selectedTheme"></ion-nav>`

Then in your app.component.ts, you just have to switch the variable “selectedTheme” to the name of the class you want to apply at the root. So:

```auto
this.selectedTheme = "dark-theme";
or 
this.selectedTheme = "light-theme";

```

this way, you app will switch from on color theme to the other throughout your entire app. 🙂  
Also important, if you’re using a menu component, you’ll notice in your app.html that the ion-menu is outside the ion-nav tag to which you applied the dynamic theme. The solution is to simply apply the dynamic theme to your menu the same way you applied it to the ion-app:

`<ion-menu [class]="selectedTheme" [content]="content">`

Let me know if this works for you guys.

---

<div class="post-metadata">

**Author:** ![robinyo](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/robinyo/32/115964_2.png) [@robinyo](https://forum.ionicframework.com/u/robinyo)\
**Post date:** [April 14, 2018, 11:45pm UTC](https://forum.ionicframework.com/t/overriding-component-sass-variables-in-dynamic-themes/115186/7 "2018-04-14T23:45:57Z")

</div>

> [@barreola](#):
>
> I cannot change de color of the tabs icon

See: [https://robferguson.org/blog/2018/04/09/theming-your-ionic-3-app-bespoke-svg-icons/](https://robferguson.org/blog/2018/04/09/theming-your-ionic-3-app-bespoke-svg-icons/)

For example, coloured SVG icons (in Tabs component):

 ![venue-page--1-](https://global.discourse-cdn.com/ionicframework/original/3X/1/8/18b57dba383b6149feb251c8384e9bff54a98584.jpg)

---

<div class="post-metadata">

**Author:** ![loki9182](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/loki9182/32/103293_2.png) [@loki9182](https://forum.ionicframework.com/u/loki9182)\
**Post date:** [April 14, 2018, 11:48pm UTC](https://forum.ionicframework.com/t/overriding-component-sass-variables-in-dynamic-themes/115186/8 "2018-04-14T23:48:27Z")

</div>

In all honesty, I kind of gave up trying to theme Ionic components with this multi-theme method. I simply build my own components with good old divs.

---

<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:59pm UTC](https://forum.ionicframework.com/t/overriding-component-sass-variables-in-dynamic-themes/115186/9 "2021-07-19T21:59:26Z")

</div>


