5

I followed the official documentation and my code is like below:

let alert = this.alertCtrl.create({
  title: 'Alert',
  subTitle: 'Subtitle',
  message: 'This is an alert message.',
  buttons: ['OK'],
  cssClass: 'profalert'
});

alert.present();

and my CSS:

.profalert{
    color:#e7333c;
    background-color: red;

}

But this is not changing the alertbox color. I also tried alert .setCssClass('profalert');

I already checked this

Sampath
  • 63,341
  • 64
  • 307
  • 441
Arijit
  • 1,633
  • 2
  • 21
  • 35

5 Answers5

6

It is working fine.You just need to declare it inside the app.scss file.

app.scss

.profalert{
    color:#e7333c;
    background-color: red;

}

Result:

When I applied your style to my app's alert box:

enter image description here

Sampath
  • 63,341
  • 64
  • 307
  • 441
6

If you're working with Ionic 4 then add:

.profalert{
color:#e7333c;
background-color: red;
}

to global.scss

If interested in horizontal alignment of alert control buttons, check my answer here

If you're migrating from older versions to ionic 4.x, check out this repo for breaking changes.

Community
  • 1
  • 1
7guyo
  • 3,047
  • 1
  • 30
  • 31
0

To elaborate on the previous answers, you can actually do it in your page.css but it needs to be outside of the page-name{ } like so:

page-name{

}

.profalert{
    color:#e7333c;
    background-color: red;

}
Mike
  • 71
  • 1
  • 5
0
.profalert {
    color:#e7333c;
    background-color: red;
}

1.add scss-class in global.scss in ionic4,
2.add scss-class in app.scss in ionic3,
you should add scss-class in root scss file

ChrisMM
  • 8,448
  • 13
  • 29
  • 48
Anand Acharya
  • 89
  • 1
  • 3
0

In Angular, the CSS of a specific page is scoped only to elements of that page. Even though the Alert can be presented from within a page, the ion-alert element is appended outside of the current page. This means that any custom styles need to go in a global stylesheet file. In an Ionic Angular starter this can be the src/global.scss file or you can register a new global style file by adding to the styles build option in angular.json.Style Placement

Aviyot G
  • 1
  • 1