# Ionic 4 - Unable to change toast close button color

**URL:** <https://forum.ionicframework.com/t/ionic-4-unable-to-change-toast-close-button-color/162608>\
**Category:** Ionic Framework\
**Created:** [April 27, 2019, 6:37am UTC](https://forum.ionicframework.com/t/ionic-4-unable-to-change-toast-close-button-color/162608 "2019-04-27T06:37:15Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![iondafi](https://avatars.discourse-cdn.com/v4/letter/i/4da419/32.png) [@iondafi](https://forum.ionicframework.com/u/iondafi)\
**Post date:** [April 27, 2019, 6:37am UTC](https://forum.ionicframework.com/t/ionic-4-unable-to-change-toast-close-button-color/162608/1 "2019-04-27T06:37:16Z")

</div>

I read the documentation, I followed it but I’m unable to change the close button color

I show the toast with the following code where I specify the cssClass as shown in documentation

```auto
    await this.toastController.create({
      message: error,
      color: 'danger',
      showCloseButton: true,
      cssClass: 'button-color-changed',
    }).then(toast => toast.present());

```

Then to my component scss file I add the corresponding class shown below but never changed

```auto
.button-color-changed {
  --button-color: #fff;
}

```

Then I read [this issue](https://github.com/ionic-team/ionic/issues/16907) and changed the css prepending the class with `ion-toast` but again nothing changed

```auto
ion-toast.button-color-changed {
  --button-color: #fff;
}

```

Then I moved the css class to `variables.scss` (before using the `ion-toast` prefixed then the other) but again nothing

Please help me to understand what I missed

```auto

Ionic:

   ionic (Ionic CLI) : 4.12.0 (/usr/local/lib/node_modules/ionic)
   Ionic Framework : @ionic/angular 4.0.0
   @angular-devkit/build-angular : 0.12.3
   @angular-devkit/schematics : 7.2.3
   @angular/cli : 7.2.3
   @ionic/angular-toolkit : 1.2.3

Cordova:

   cordova (Cordova CLI) : 8.1.2 (cordova-lib@8.1.1)
   Cordova Platforms : android 7.1.4
   Cordova Plugins : cordova-plugin-ionic-keyboard 2.1.3, cordova-plugin-ionic-webview 2.3.1, (and 8 other plugins)

System:

   Android SDK Tools : 26.1.1 (/opt/devel/android/android-sdk-macosx/)
   NodeJS : v11.12.0 (/usr/local/Cellar/node/11.12.0/bin/node)
   npm : 6.9.0
   OS : macOS Mojave
   Xcode : Xcode 10.2.1 Build version 10E1001

```

---

<div class="post-metadata">

**Author:** ![hupptechnologies](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/hupptechnologies/32/121997_2.png) [@hupptechnologies](https://forum.ionicframework.com/u/hupptechnologies)\
**Post date:** [April 27, 2019, 1:44pm UTC](https://forum.ionicframework.com/t/ionic-4-unable-to-change-toast-close-button-color/162608/2 "2019-04-27T13:44:21Z")

</div>

Try this code… this is work for me…

Add this code in global.scss file,

```auto
::ng-deep {
    .toast-button{
        color: var(--button-color);
    }
}

```

Add button variable code in your `variables.scss`.

> ` --button-color: #181473;`

---

<div class="post-metadata">

**Author:** ![iondafi](https://avatars.discourse-cdn.com/v4/letter/i/4da419/32.png) [@iondafi](https://forum.ionicframework.com/u/iondafi)\
**Post date:** [April 27, 2019, 2:06pm UTC](https://forum.ionicframework.com/t/ionic-4-unable-to-change-toast-close-button-color/162608/3 "2019-04-27T14:06:56Z")

</div>

Thanks but it doesn’t work, after adding the code you posted I simply changed the `cssClass` property to match the name on your example

```auto

    await this.toastController.create({
      message: error,
      color: 'danger',
      showCloseButton: true,
      cssClass: 'toast-button'
    }).then(toast => toast.present());

```

---

<div class="post-metadata">

**Author:** ![VictorNorman](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/victornorman/32/124743_2.png) [@VictorNorman](https://forum.ionicframework.com/u/VictorNorman)\
**Post date:** [April 28, 2019, 5:26am UTC](https://forum.ionicframework.com/t/ionic-4-unable-to-change-toast-close-button-color/162608/4 "2019-04-28T05:26:56Z")

</div>

Well, I tell you… I’m stumped. I’ve tried everything I can think of to help you, but to no avail.

This may just be one of those things you cannot do… Because Ionic uses the shadow-dom stuff, you cannot just “reach in” to those items in the shadow-dom and style them. I think the only way to solve this problem is for the Ionic folks to expose a variable in their API, like --button-background-color or some such, so that you can change that value.

---

<div class="post-metadata">

**Author:** ![iondafi](https://avatars.discourse-cdn.com/v4/letter/i/4da419/32.png) [@iondafi](https://forum.ionicframework.com/u/iondafi)\
**Post date:** [April 28, 2019, 6:32am UTC](https://forum.ionicframework.com/t/ionic-4-unable-to-change-toast-close-button-color/162608/5 "2019-04-28T06:32:08Z")

</div>

Thanks for your reply but I want to change the text color (for the CLOSE BUTTON) not the background color and the documentation says "use `--button-color`", this is the link to the toast [css custom properties](https://ionicframework.com/docs/api/toast#css-custom-properties) but where, how??

Properties like `--button-color` should solve problems related to shadow dom…

Here we have two problems

- the **close** button seems not customizable
- adding buttons and declaring the `cssClass` to them (see the example below) doesn’t work

Inspecting the DOM the `<button>` (inside the `ion-toast` element) contains the declared class but the css code isn’t found (I’ve placed it in `variables.scss`, `global.scss` and the component page `.scss`)

 ![shoot01](https://global.discourse-cdn.com/ionicframework/original/3X/a/f/afd9afc8c80e862a0177111fee402abf164c504b.jpeg)

```auto

    await this.toastController.create({
      message: error,
      // color: 'danger',
      showCloseButton: true,
      buttons: [
        {
          text: 'Mimic Close',
          cssClass: 'damned-class-not-applied',
          handler: () => {
            console.log('Cancel clicked');
          }
        }
      ]
    }).then(toast => toast.present());

```

---

<div class="post-metadata">

**Author:** ![VictorNorman](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/victornorman/32/124743_2.png) [@VictorNorman](https://forum.ionicframework.com/u/VictorNorman)\
**Post date:** [April 28, 2019, 7:16am UTC](https://forum.ionicframework.com/t/ionic-4-unable-to-change-toast-close-button-color/162608/6 "2019-04-28T07:16:22Z")

</div>

This worked for me.

In `global.scss`, put this:

```auto
.button-color-changed {
  --button-color: purple;
}

```

Works for me. Let me know.

---

<div class="post-metadata">

**Author:** ![iondafi](https://avatars.discourse-cdn.com/v4/letter/i/4da419/32.png) [@iondafi](https://forum.ionicframework.com/u/iondafi)\
**Post date:** [April 28, 2019, 7:29am UTC](https://forum.ionicframework.com/t/ionic-4-unable-to-change-toast-close-button-color/162608/7 "2019-04-28T07:29:41Z")

</div>

Already tried without success, just to be clear (maybe I missed something)

The `global.scss` contains the code posted by you and the `home.page.ts` contains the code shown below

```auto
  constructor(
    public toastController: ToastController) {}

  ngOnInit() {
    this.onUpdateError('simulated toast error');
  }

  async onUpdateError(error: any) {
    await this.toastController.create({
      message: error,
      // color: 'danger',
      showCloseButton: true,
      cssClass: 'button-color-changed',
      buttons: [
        {
          text: 'Mimic Close',
          cssClass: 'button-color-changed',
          handler: () => {
            console.log('Cancel clicked');
          }
        }
      ]
    }).then(toast => toast.present()); }

```

---

<div class="post-metadata">

**Author:** ![VictorNorman](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/victornorman/32/124743_2.png) [@VictorNorman](https://forum.ionicframework.com/u/VictorNorman)\
**Post date:** [April 28, 2019, 7:39am UTC](https://forum.ionicframework.com/t/ionic-4-unable-to-change-toast-close-button-color/162608/8 "2019-04-28T07:39:57Z")

</div>

Here is my entire project. Note that I use it to test some other stuff, so there are a few things in here that have nothing to do with your problem.

home.page.ts:

```auto
import { Component } from '@angular/core';
import { ToastController } from '@ionic/angular';

@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss'],
})
export class HomePage {

  public item2BackgroundSet = false;

  constructor(
    private toastCtrl: ToastController,
  ) {}

  async handleClick() {
    this.item2BackgroundSet = !this.item2BackgroundSet;
    await this.toastCtrl.create({
      message: 'hello world',
      color: 'danger',
      showCloseButton: true,
      cssClass: 'button-color-changed',
    }).then(toast => toast.present());
  }

}

```

home.page.html:

```auto
<ion-header>
  <ion-toolbar>
    <ion-title>
      Ionic Blank
    </ion-title>
  </ion-toolbar>
</ion-header>

<ion-content>
  <ion-list>
    <ion-item>
      <ion-label>I am item 1</ion-label>
    </ion-item>
    <ion-item [ngClass]="{ 'use-pink-background': item2BackgroundSet }">
      <ion-label>I am item 2. Value of backgroundSet: {{item2BackgroundSet}}</ion-label>
    </ion-item>
  </ion-list>
  <ion-button (click)="handleClick()">
    I am a button. Push me to toggle the background color of item 2 and pop up a toast.
  </ion-button>
</ion-content>

```

home.page.scss: (nothing in here useful to you)

```auto
.use-pink-background {
  --ion-background-color: pink;
}

```

global.scss:

```auto
// http://ionicframework.com/docs/theming/

@import '~@ionic/angular/css/core.css';
@import '~@ionic/angular/css/normalize.css';
@import '~@ionic/angular/css/structure.css';
@import '~@ionic/angular/css/typography.css';
@import '~@ionic/angular/css/display.css';
@import '~@ionic/angular/css/padding.css';
@import '~@ionic/angular/css/float-elements.css';
@import '~@ionic/angular/css/text-alignment.css';
@import '~@ionic/angular/css/text-transformation.css';
@import '~@ionic/angular/css/flex-utils.css';

.button-color-changed {
   --button-color: purple;
}

```

---

<div class="post-metadata">

**Author:** ![iondafi](https://avatars.discourse-cdn.com/v4/letter/i/4da419/32.png) [@iondafi](https://forum.ionicframework.com/u/iondafi)\
**Post date:** [April 28, 2019, 7:57am UTC](https://forum.ionicframework.com/t/ionic-4-unable-to-change-toast-close-button-color/162608/9 "2019-04-28T07:57:02Z")

</div>

> [@VictorNorman](#):
>
> import { ToastController } from ‘@ionic/angular’;

Thanks for your time, i’m drive crazy, I’ve created a new blank project and copied your code but the toast button is always blue, the `cssClass` value is totally ignored

 ![02](https://global.discourse-cdn.com/ionicframework/original/3X/f/5/f5fcf73a3264ed9449c233af697b5033b1ad7e62.png)

Tried Chrome, Firefox and Safari

---

<div class="post-metadata">

**Author:** ![VictorNorman](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/victornorman/32/124743_2.png) [@VictorNorman](https://forum.ionicframework.com/u/VictorNorman)\
**Post date:** [April 28, 2019, 10:10am UTC](https://forum.ionicframework.com/t/ionic-4-unable-to-change-toast-close-button-color/162608/10 "2019-04-28T10:10:25Z")

</div>

You must have some css somewhere that sets it to blue, because by default it would be white. When I comment out the --button-color css in global.scss then my button text color is white.

---

<div class="post-metadata">

**Author:** ![iondafi](https://avatars.discourse-cdn.com/v4/letter/i/4da419/32.png) [@iondafi](https://forum.ionicframework.com/u/iondafi)\
**Post date:** [April 28, 2019, 12:08pm UTC](https://forum.ionicframework.com/t/ionic-4-unable-to-change-toast-close-button-color/162608/11 "2019-04-28T12:08:02Z")

</div>

Maybe the default color changes by OS, the demo at the toast documentation page is blue (both MD and IOS)  
I’m on MacOS Mojave

---

<div class="post-metadata">

**Author:** ![sagoo33](https://avatars.discourse-cdn.com/v4/letter/s/71c47a/32.png) [@sagoo33](https://forum.ionicframework.com/u/sagoo33)\
**Post date:** [September 23, 2019, 7:52am UTC](https://forum.ionicframework.com/t/ionic-4-unable-to-change-toast-close-button-color/162608/12 "2019-09-23T07:52:30Z")

</div>

It looks like there was a bug which is fixed now:  
[https://github.com/ionic-team/ionic/pull/18133](https://github.com/ionic-team/ionic/pull/18133)

So, to get the color changed I used the following in my global.scss

```auto
ion-toast.dangerToast {
    opacity: 0.9;
    //text-align: center;
    color: white;
    --background: var(--ion-color-danger);
    --button-color:#333 !important;
}

```

with the toast generation code as:

```auto
  async showToastAlert(message: string) {
    
    const toast = await this.toastController.create(
      {
        message: message,
        showCloseButton: true,
        translucent: false,
        position: 'bottom',
        cssClass: 'dangerToast',
        closeButtonText: 'OK',
        duration: 5000
      });
    toast.present();
  }

```
