Is it possible to change the color of buttons of Alert (Ok, Cancel) in Ionic2? Can we use cssClass
property to give color to buttons?
.buttonCss{
button{
color:#e74c3c !important;
}
}
The above code gives the same color to the both Ok and Cancel buttons like the below image
But I need to get the following result(Both button shoulb in in different color),
Any help is appreciated...!
showAlert()
function showAlert(title, message, yesHandler, noHandler, caller) {
let alert = this.alertCtrl.create({
title: title || "Please Confirm",
message: message,
cssClass:'buttonCss',
buttons: [
{
text: 'Exit',
handler: () => yesHandler(caller)
},
{
text: 'Cancel',
role: 'cancel',
handler: () => noHandler(caller)
}
]
});
alert.present();
}
1) First option, just using a class for the alert, and a css style rule for each button
showAlert(title, message, yesHandler, noHandler, caller) {
let alert = this.alertCtrl.create({
title: title || "Please Confirm",
message: message,
cssClass:'buttonCss',
buttons: [
{
text: 'Exit',
handler: () => yesHandler(caller)
},
{
text: 'Cancel',
role: 'cancel',
handler: () => noHandler(caller)
}
]
});
alert.present();
}
And then:
.buttonCss {
button.alert-button:nth-child(1){
color: red;
}
button.alert-button:nth-child(2){
color: green;
}
}
This way the first button (nth-child(1)
) will be red
and the second button (nth-child(2)
) will be green
.
2) Second option, using a class for the alert, and adding the cssClass
property to each button, in order to use that custom class on each button
showAlert(title, message, yesHandler, noHandler, caller) {
let alert = this.alertCtrl.create({
title: title || "Please Confirm",
message: message,
cssClass:'buttonCss',
buttons: [
{
text: 'Exit',
cssClass: 'exit-button',
handler: () => yesHandler(caller)
},
{
text: 'Cancel',
role: 'cancel',
cssClass: 'cancel-button',
handler: () => noHandler(caller)
}
]
});
alert.present();
}
And then:
.buttonCss {
button.alert-button.exit-button{
color: red;
}
button.alert-button.cancel-button{
color: green;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With