Ionic 4 button ngStyle background-color


Previously, we could set the background-color of a button with ngStyle like so:
<ion-button ion-button small [ngStyle]="{'background-color':'pink'}">TEST</ion-button>

But now that only changes the background behind the button not the BG within the button:

I have tried using --background-color and --ion-background-color

I am using ngStyle to set color conditionally:
<ion-button ion-button small [ngStyle]=" !isSelected&&{'background-color':'pink'}">TEST</ion-button>



How would you set the background-color of a button using ngStyle?


As long as I have <ion-button> within <ion-buttons> ngStyle works:
<ion-buttons slot="start"> <ion-button ion-button small [ngStyle]="{'background-color':'pink'}">TEST<ion-button> </ion-buttons


read the tutorial, I think you are missing some important keys about shadow dom, introduced in ionic v4, and how you could style element across the dom

also this other video from @joshmorony is a really good intro to styling in v4


I can style the background color using --ion-color-base: pink!important; in scss.

How would you use this in ngStyle?
The following is not working for me. Maybe you cannot?

<ion-button [ngStyle]="{'--ion-color-base':'pink'}">Test</ion-button>

Okay, interestingly, I can use style but not ngStyle.
This works:
<ion-button style='--ion-color-pink'>Test</ion-button>

But, this removes my option to use conditional ngStyle.
I realize there are other ways to do this but I am trying to find out if I cannot use ngStyle to achieve this.


why don’t you use a separate scss file for your page or component?

then just define a class style like

 <ion-button [class.whatever]="myCondition">

and in your secs

ion-button {
  &.whatever {
     --a-variable-to-customize: red;

Sounds good but is using ngStyle no longer an option?


I have absolutely no idea, I don’t like to have style in middle of my template, I don’t use style or ngStyle at all :wink:


Here is my scenario:

I have a number of buttons and each should be colored depending on if they were selected.
I used [ngStyle] to color them based on a var withing the buttons parent item in a list.
I actually had these in slides to boot.
So, the var that determines if they are a different color is: cat.isSelected

            <ion-slide class="catSlide" *ngFor="let cat of categories_ar; let i = index">
                <ion-buttons slot="start">
                    <ion-button class="catBtn" ion-button small (click)="select_category(cat)" [ngStyle]="!cat.isSelected&&{'background-color':'green'}||{'background-color':'pink'}">{{cat.title}}</ion-button>

with ngStyle I cannot set the color.
With style, I can set the color but not use condition

Avoiding these two options and doing it in scss, how would I pass each individual cat.isSelected to the condition?

@if (isSelected) {
–ion-color-base: yellow!important;


Okay, duh…

<ion-button [class.catBtn]="cat.isSelected"> </ion-button>

1 Like

Yep just defined multiple class in your scss, one for each single colors or something


Almost there…

If I want to define a default class ( not selected ) how does this work without passing notSelected…

This works:
<ion-button [class.catBtn]='!cat.isSelected' [class.catBtnSelected]='cat.isSelected'></ion-button>

This does not:
<ion-button catBtn [class.catBtnSelected]='cat.isSelected'></ion-button>


a default class, like standard css?

<ion-button class="something">

in scss

 ion-button.something {

or thru attributes

<ion-button catBtn

=> in scss

 ion-button[catBtn] {

But in the html this does not work:
<ion-button catBtn [class.catBtnSelected]='cat.isSelected'></ion-button>

<ion-button [class.catBtn] [class.catBtnSelected]='cat.isSelected'></ion-button>

But this does:
<ion-button [class.catBtn]='!cat.isSelected' [class.catBtnSelected]='cat.isSelected'</ion-button>

 <ion-button class="catBtn" [class.catBtnSelected]='cat.isSelected'></ion-button>

Also does not work. Appreciate the effort though.
The catBtn class works but the catBtnSelected does not now.

Due to the title being related to whether or not ngStyle will work conditionally. I will leave this open in case anyone else chimes in.

Otherwise, it looks like not using ngStyle is the way to go and I have a lot of v3 pages to update.


Weird, the following works for example well in my app

<ion-item [class.item-toggle-checked]="itemShouldBeChecked" class="item-toggle">

good luck then


I tried it n reverse order as you have but still no catBtnSelected working (your class.item-toggle-checked).
Can you share your scss for that example?