Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic 2 Alert customization

I want to customize my alerts in Ionic 2. I know that I can do it globally in the variables.scss, but I want to modify a specific one, in a specific page.

I tried cssClass in the alert code, I tried other different things, that work, but globally, not for a specific one.

Is there any way to do it?

like image 260
Mystearica Avatar asked Apr 19 '26 10:04

Mystearica


2 Answers

Edit all your AlertController.create methods to look like this:

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

And add this to app.scss:

.alertCustomCss {
    // text color for basic alerts
    color: white; 

    // alert modal page bg color. Warning this will remove the alert transparency
    background-color: color($colors, dark, base); 

    button {
        color: white !important; // button text color
        background-color: color($colors, secondary, base) !important;
            //button bg color
    }

    .alert-message {
        color: white; // text color for confirm alerts
    }

    .alert-wrapper {
        background-color: color($colors, dark, base); // bg color of alert content
    }
  }
like image 126
Tadija Bagarić Avatar answered Apr 21 '26 01:04

Tadija Bagarić


Adding the styles in app.css and calling it in page.ts

showAlert() {
    let alert = this.alertCtrl.create({
      title: 'New Friend!',
      subTitle: 'Your friend, Obi wan Kenobi, just accepted your friend request!',
      buttons: ['OK'],
      cssClass: 'alertCustomCss'
    });
    alert.present();
}

In App.css

.alertCustomCss{
  background-color: white;
  color: blue;
  button{
      color: blue;
  }
}
like image 31
Ragavan Rajan Avatar answered Apr 21 '26 03:04

Ragavan Rajan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!