2

I followed the answers found here: Ionic 2 Alert customization

But I can't find all the other classes or elements that have to be styled so alerts look like I want.

I edited my code so it looks like this:

app.scss:

.alertCustomCss {
  background-color: color($colors, dark, base);
  color: white;
  button {
      color: white !important;
  }
}

ts:

const alert = this.alertCtrl.create({
    title: title,
    subTitle: msg,
    buttons: ['OK'],
    cssClass: 'alertCustomCss'
});
alert.present();

But this makes all the text white, replaces the transparency of the modal page that holds the popup and sets it to set 'background-color' (so the page that called the popup is not visible anymore). The text of the button is set to white.

Please notice that the background around the popup is not transparent anymore.

enter image description here

The question is how to set the background color of the text placeholder and not the whole page? What css properties to use?

Broader question: What are the elements (css classes or directives) of the alert popup that can be styled? Title text color, content text font, etc..

Tadija Bagarić
  • 2,495
  • 2
  • 31
  • 48
  • All element in the page can be styled by css. So you just need to find the class and add your style. If it does not work so your style is wrong. You need to find a way to correct it. It is just html and css, no need Ionic knowledge. Remember, some style need `!important` to override – Duannx Nov 09 '17 at 02:29
  • @Duannx Thank you for the advice. Do you know what css properties do I have to set to change the background of an ionic alert window? What did you use to change yours? Or even better where can I find that info? The docs don't say much about this – Tadija Bagarić Nov 09 '17 at 06:37
  • 1
    Just inspect your element in browser dev tool. You will find all class what you need to style. – Duannx Nov 09 '17 at 06:49

2 Answers2

5

I edited all my AlertController.create methods to look like this:

const alert = this.alertCtrl.create({
    title: title,
    subTitle: msg,
    buttons: ['OK'],
    cssClass: 'alertCustomCss' // <- added this
});

And I added this to app.scss:

.alertCustomCss {
    color: white; // text color for basic alerts
    button {
        color: white !important;
        background-color: color($colors, secondary, base) !important;
    }
    .alert-message {
        color: white; // text color for confirm alerts
    }
    .alert-wrapper {
        background-color: color($colors, dark, base);
    }
  }
Tadija Bagarić
  • 2,495
  • 2
  • 31
  • 48
1

if you enclosed the desired word in "span" like:

title: <span class='myAlertTitle'>Special Dogs</span>

and then you add that class in the app.scss normally, it will apply even you don't define cssClass attribute to the Alert Object.

It's not the OOTB way but you have more control over the style, I think...

Ari Waisberg
  • 1,186
  • 1
  • 12
  • 23