# AlertController background color

**URL:** <https://forum.ionicframework.com/t/alertcontroller-background-color/98257>\
**Category:** ionic-v3\
**Created:** [July 14, 2017, 8:59pm UTC](https://forum.ionicframework.com/t/alertcontroller-background-color/98257 "2017-07-14T20:59:49Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![wterrill](https://avatars.discourse-cdn.com/v4/letter/w/c6cbf5/32.png) [@wterrill](https://forum.ionicframework.com/u/wterrill)\
**Post date:** [July 14, 2017, 8:59pm UTC](https://forum.ionicframework.com/t/alertcontroller-background-color/98257/1 "2017-07-14T20:59:49Z")

</div>

I’m having a problem trying to change the background color on a specific alert controller. Specifically, I’m trying to modify the SASS variable “alert-md-background-color” but only for one specific alert.

I’ve tried using [cssClass: in the alert definition like this](https://forum.ionicframework.com/t/usage-of-cssclass-to-style-alert-button-in-ionic-2/55970) as well as [using alert.setCssClass()](https://github.com/ionic-team/ionic/issues/6835) but this doesn’t seem to work either. (It seems like the Sass variables are overriding any modification?)

So, the only way I’ve been able to successful change the background color is by setting $alert-md-background-color:#F35E5D in the variables.scss file… but that changes the background color on all alerts.

So, I’m stuck… what’s the proper way to change the background color on a specific alert? (or anything that is governed by a Sass variable, for that matter)

Thanks!

---

<div class="post-metadata">

**Author:** ![KishuPro](https://avatars.discourse-cdn.com/v4/letter/k/b4bc9f/32.png) [@KishuPro](https://forum.ionicframework.com/u/KishuPro)\
**Post date:** [July 15, 2017, 3:16pm UTC](https://forum.ionicframework.com/t/alertcontroller-background-color/98257/2 "2017-07-15T15:16:39Z")

</div>

Please copy paste the actual code _you_ are using here; basically it should be as simple as setting the `cssClass`.

---

<div class="post-metadata">

**Author:** ![wterrill](https://avatars.discourse-cdn.com/v4/letter/w/c6cbf5/32.png) [@wterrill](https://forum.ionicframework.com/u/wterrill)\
**Post date:** [July 15, 2017, 7:21pm UTC](https://forum.ionicframework.com/t/alertcontroller-background-color/98257/3 "2017-07-15T19:21:38Z")

</div>

Thanks for the reply KishuPro.

Here’s the code in the landing.ts file:

```auto
  about() {
    let alert = this.alertCtrl.create({
      title: "This is a title:",
      subTitle: 'This is a subtitle',
      cssClass: 'custom-alertDanger',
      message: `<p>This is a message </p>`, 
      buttons: [
        {
          text: 'OK',
          role: 'cancel',
          handler: () => {}}]});
    alert.present();
  }

```

and here’s the CSS style definition in the landing.scss page:

```auto
.md,.ios,.wp {
  page-landing {
    .custom-alertDanger {
      background-color: #F35E5D !important;
      background: #F35E5D !important;
      color: red !important;
      $alert-md-background-color: #F35E5D;
      $alert-ios-background: #F35E5D;
    }}}

```

\*note: I’ve tried every conceivable variation of the above (i.e. each style alone, each one with or without !important) to no avail. Also, I had other CSS styles on this page that were working, so the stylesheet is properly named and handled, etc.

This is what I get back:  
 ![image](https://global.discourse-cdn.com/ionicframework/original/3X/f/d/fd215bcf3d6ff28f591464c7a4bc8812f97f9bbd.png)

However, when I take the same CSS code, remove it from landing.scss and put it in instead in app.scss, I get this:  
 ![image](https://global.discourse-cdn.com/ionicframework/original/3X/4/1/418090e5d93b347a1343fb0bde3c5305cc2e6b9f.png)

Notice that the title and subtitle have changed color. (yay!) I used inline html in the message line so the

tag styles were used (yay!) The weird thing is that the ENTIRE background of everything except the alertcontroller is now red.

Finally, If I add this line to variable.scss (and remove all custom-alertDanger CSS definitions)

```auto
$alert-md-background-color:#F35E5D;

```

I get what I’m trying to do:

![image](https://global.discourse-cdn.com/ionicframework/original/3X/d/7/d7f6a1176a2b2e8795ecae8a8b1d57e39e5f19b4.png)

but, it’s now set for ALL of the alertController messages, instead of this specific one.

If you could point me in the right direction to try and research this, I’d greatly appreciate it.

---

<div class="post-metadata">

**Author:** ![SigmundFroyd](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/sigmundfroyd/32/111604_2.png) [@SigmundFroyd](https://forum.ionicframework.com/u/SigmundFroyd)\
**Post date:** [July 15, 2017, 7:51pm UTC](https://forum.ionicframework.com/t/alertcontroller-background-color/98257/4 "2017-07-15T19:51:50Z")

</div>

I don’t think you need to specify `.ios, .md, .wp` in your scss, but as for your specific issue I don’t think the alert will actually wind up being a child of your page, so does it look if you move `.custom-alertDanger` outside of the `page-loading` block?

---

<div class="post-metadata">

**Author:** ![wterrill](https://avatars.discourse-cdn.com/v4/letter/w/c6cbf5/32.png) [@wterrill](https://forum.ionicframework.com/u/wterrill)\
**Post date:** [July 15, 2017, 8:15pm UTC](https://forum.ionicframework.com/t/alertcontroller-background-color/98257/5 "2017-07-15T20:15:34Z")

</div>

Hello SigmundFroyd, and thanks for chiming in.

You’re absolutely correct, the .ios, .md .wp additions are not required. I got that from this [video by Josh Morony](https://www.joshmorony.com/hacking-css-in-ionic-2/) (at the 14:00 minute mark) Basically, he suggested to add it to make you’re css file as specific as possible since the most specific CSS “wins” if there is a conflict. Therefore (as I understand it) adding that is like adding a !important for all of the subsequent styles. (Yeah… I was getting desperate)

Anyhow, to answer your question, taking the .custom-alertDanger makes the alert act the same as when I put it in the app.scss file. that is… the entire background of the page (except the alert controller background) goes red like this:  
 ![image](https://global.discourse-cdn.com/ionicframework/original/3X/e/2/e2144994e825f0ded8ebd1edb3de593a44d7db97.png)

Also, I tried using the browser to find the style that I need to update with the idea that I could do it directly. I was able to see that using the following outside of the page-loading block:

```auto
    .alert-md .alert-message {
        background: red;
    }

```

almost did what I wanted as below:  
 ![image](https://global.discourse-cdn.com/ionicframework/original/3X/9/4/942d1bdf791f984362a0f21d6a2929129e9b3fd8.png)

but for ALL of the alert controllers in the application. Putting it inside of the page-loading block did nothing.

So… a step closer? I keep reading that these styles happen “outside” of the page, but I’m not sure if that means that it is possible to edit them on a per-alert basis.

EDIT: I found that using:

```auto
.alert-md .alert-wrapper {
    background-color: red;
}

```

did the same thing as using

```auto
$alert-md-background-color:#F35E5D;

```

In the variables.scss file. Again, this occurred only outside of the page-landing block, and changed all of the alerts.

EDIT 2:  
[I found another solution here](https://forum.ionicframework.com/t/creating-custom-alerts-in-ionic2/49723/10) that I still can’t make work, but seems promising. In that thread, user jeffreyCarre suggested the following:

```auto
You can add a class to the alert:

{
.., cssClass:'alert-danger'
}
in the Css:

 &.alert-danger {
    .alert-head {
      background: map-get($colors, danger);
      color: #fff;
      margin-bottom: 10px;
    }
  }

```

Unfortunately, I couldn’t make that work… but the &. selector seems like it might be another way to attack the problem. ([more info here](https://css-tricks.com/the-sass-ampersand/))

---

<div class="post-metadata">

**Author:** ![wterrill](https://avatars.discourse-cdn.com/v4/letter/w/c6cbf5/32.png) [@wterrill](https://forum.ionicframework.com/u/wterrill)\
**Post date:** [July 15, 2017, 9:46pm UTC](https://forum.ionicframework.com/t/alertcontroller-background-color/98257/6 "2017-07-15T21:46:50Z")

</div>

OK… I got something that works.

I found [this post on stackexchange](https://stackoverflow.com/questions/37118385/styling-ionic-2-toast) about styling toast, and the second solution from user arkade did the trick.

So, here is my alert (it hasn’t changed) in the landing.ts file:

```auto
  show_about_alert() {
    let alert = this.alertCtrl.create({
      title: "This is a title:",
      subTitle: 'This is a subtitle',
      cssClass: 'custom-alert-danger',
      message: `<p>This is a message </p>`, 
      buttons: [
        {
          text: 'OK',
          role: 'cancel',
          handler: () => {
          }
        }
      ]
    });
    alert.present();
  }

```

And here is the landing.scss page. (note, this could also be put in app.scss file.)

```auto
.md,
.ios,
.wp {
  .custom-alert-danger {
    > div {
      background: red; // if you don't use .md, .ios, .mp initially, you'd have to use !important here.
      color: #fff;
      margin-bottom: 10px;
    }
  }
page-landing {
   // The alert controller CssClass MUST go outside of the page definitions.
}

```

This gives me this:  
 ![image](https://global.discourse-cdn.com/ionicframework/original/3X/3/8/38b186f5ea687ef93a49ac08cea3069abac56ff1.png)

So… let me pose another question.  
Here’s the html I see in the browser when I look at the above:

 ![image](https://global.discourse-cdn.com/ionicframework/original/3X/a/1/a1139f81e1dd783e7bde1699d23146b5a5e42ed4.png)

and you can see where my custom-alert-danger is coming into play. However, how would I target JUST the background color for things inside of the custom class? For example, if I only wanted to change the background color of the:  
`<div class="alert-message">` ?  
to get something that looks like this:  
 ![image](https://global.discourse-cdn.com/ionicframework/original/3X/9/4/942d1bdf791f984362a0f21d6a2929129e9b3fd8.png)

EDIT:  
turns out it’s straightforward:

```auto
.md,
.ios,
.wp {
  .custom-alert-danger {
    > div {
      background: red;
      color: #fff;
      margin-bottom: 10px;
    }
    .alert-message{
        background: blue;
    }
  }
  page-landing {}
}

```

to get this:  
 ![image](https://global.discourse-cdn.com/ionicframework/original/3X/3/0/307042933e5295de555e1374f11325f47ecdb067.png)

I would definitely like to know if there is a more elegant way to do this.

---

<div class="post-metadata">

**Author:** ![KishuPro](https://avatars.discourse-cdn.com/v4/letter/k/b4bc9f/32.png) [@KishuPro](https://forum.ionicframework.com/u/KishuPro)\
**Post date:** [July 16, 2017, 4:48am UTC](https://forum.ionicframework.com/t/alertcontroller-background-color/98257/7 "2017-07-16T04:48:52Z")

</div>

I see that you’ve tried to attack the problem in many ways, but I think the following should be a more elegant way to do it.

In `landing.ts`

```auto
let alert = this.alertCtrl.create({
   ....
   cssClass: 'myalert'
});

```

and in `landing.scss`

```auto
.myalert .alert-wrapper {
    background-color: #F35E5D;
}

```

Try this and let me know if this works for you or not.

---

<div class="post-metadata">

**Author:** ![wterrill](https://avatars.discourse-cdn.com/v4/letter/w/c6cbf5/32.png) [@wterrill](https://forum.ionicframework.com/u/wterrill)\
**Post date:** [July 17, 2017, 9:27pm UTC](https://forum.ionicframework.com/t/alertcontroller-background-color/98257/8 "2017-07-17T21:27:36Z")

</div>

well, sonofagun… that worked great! (and WAY more elegant than my monstrosity.) Thanks KishuPro! (one note, though… it had to be set outside of the “landing-page” block in the scss file, for anyone that follows this in the future)

---

<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:** [July 17, 2017, 9:35pm UTC](https://forum.ionicframework.com/t/alertcontroller-background-color/98257/9 "2017-07-17T21:35:44Z")

</div>

I think a better approach is to move the alert CSS to `app.scss`. Leaving it in a separate file but outside of the ordinary scoping block is mystifying to readers and encourages conflicts.

---

<div class="post-metadata">

**Author:** ![KishuPro](https://avatars.discourse-cdn.com/v4/letter/k/b4bc9f/32.png) [@KishuPro](https://forum.ionicframework.com/u/KishuPro)\
**Post date:** [July 18, 2017, 3:40am UTC](https://forum.ionicframework.com/t/alertcontroller-background-color/98257/10 "2017-07-18T03:40:22Z")

</div>

You are most welcome!

Yeah it needs to be outside the page css block, it might be a good idea to move the `css` to the `app.scss` as @rapropos suggested.

Anyways, it’s good that it works for you! 👍

---

<div class="post-metadata">

**Author:** ![wterrill](https://avatars.discourse-cdn.com/v4/letter/w/c6cbf5/32.png) [@wterrill](https://forum.ionicframework.com/u/wterrill)\
**Post date:** [July 18, 2017, 6:50pm UTC](https://forum.ionicframework.com/t/alertcontroller-background-color/98257/11 "2017-07-18T18:50:28Z")

</div>

I agree. thanks for the suggestion.

---

<div class="post-metadata">

**Author:** ![AmrAlmagic](https://avatars.discourse-cdn.com/v4/letter/a/df788c/32.png) [@AmrAlmagic](https://forum.ionicframework.com/u/AmrAlmagic)\
**Post date:** [July 16, 2018, 11:31am UTC](https://forum.ionicframework.com/t/alertcontroller-background-color/98257/12 "2018-07-16T11:31:54Z")

</div>

this works for me :  
.

```auto
custom-alert-danger
  .alert-ios .alert-message,
  .alert-md .alert-message {
    max-height: 100%;
    color: white !important;
}

```

---

<div class="post-metadata">

**Author:** ![ng22792](https://avatars.discourse-cdn.com/v4/letter/n/5e9695/32.png) [@ng22792](https://forum.ionicframework.com/u/ng22792)\
**Post date:** [September 10, 2018, 8:11am UTC](https://forum.ionicframework.com/t/alertcontroller-background-color/98257/13 "2018-09-10T08:11:57Z")

</div>

I have created a utilities.ts provider and AlertUtility is a class there, in which I call up AlertController.  
Now I have applied the ‘cssClass’ to this Utilitities’ alertcontroller object, but then since providers don’t have any scss file, where should I mention this ‘custom-alert-danger’ css definition that is now working?

I tried to put it in app.scss as well as variables.scss but no luck.  
Thanks in advance!

my code :

@Injectable()  
export class AlertUtility {

constructor(private \_alertCtrl: AlertController) {}

private showWarningMessage(title, subtitle, btn) {  
let alert1 = this.\_alertCtrl.create({  
title: title,  
subTitle: subtitle,  
cssClass: ‘custom-alert-danger’,  
buttons: [btn]  
});  
alert1.present();  
}

public presentWarning(title, subtitle, btn) {  
this.showWarningMessage(title, subtitle, btn);  
}

}
