Add custom styling to ion-select

17,230

Solution 1

You can fix this easily now

Add to ion-select element:

[interfaceOptions]="{cssClass: 'my-class'}"

Add to css:

.my-class .alert-wrapper {
    max-width: 94% !important;
}

Solution 2

Yes, you can use cssClass in option like this:

// In your component
daysOptions = {
    cssClass: 'my-class',
    ...,
}

Then in css you can do what you want eg:

.my-class .alert-wrapper {
    max-width: 94% !important;
}

Thank's to ionic docs: https://ionicframework.com/docs/api/components/alert/AlertController/#advanced

Solution 3

I needed a color selector couple of months ago but I couldn’t find any. The only way to do this was using custom CSS. I tried adding CSS classes to ion-select and ion-option but the classes doesn’t get reflected in the generated output. So the only way to apply the custom CSS to ion-select and ion-options is by using the parent element and some JS.

You can check the logic in: HTML: https://github.com/ketanyekale/ionic-color-and-image-selector/blob/master/src/pages/home/home.html

TS: https://github.com/ketanyekale/ionic-color-and-image-selector/blob/master/src/pages/home/home.ts

SCSS: https://github.com/ketanyekale/ionic-color-and-image-selector/blob/master/src/pages/home/home.scss

Share:
17,230
Yokesh Varadhan
Author by

Yokesh Varadhan

Updated on June 14, 2022

Comments

  • Yokesh Varadhan
    Yokesh Varadhan almost 2 years

    I have a ion-select with few options i gave a header using [selectOptions], is there a way to define a css so that i could able to set background-color to header, button alignment ,and add a icon to the header

     <ion-select [selectOptions]="daysOptions" #selectDays="ngModel" required name="selectedDay" [(ngModel)]="selectDay" >
             <ion-option *ngFor="let day of Days;" [value]="day.val">{{day.name}}</ion-option>
     </ion-select>
    

    could someone help me