Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular mat-select text color doesn't change

I am using mat-select in my Angular application. I would like to change the text color, but the color doesn't change.

<mat-select style="color: red" [(ngModel)]="someValue">
  <mat-option>class="empty-select"</mat-option>
  <mat-option class="not-empty-select" *ngFor="let unit of units [value]="unit">{{unit}}</mat-option>
</mat-select>

I can change the background color without problems, but the text color doesn't change.

like image 875
Nancy Avatar asked Oct 15 '18 09:10

Nancy


People also ask

How do I change the color of my mat select?

Steps to reproduce: Include the mat-select-theme mixin to register the default theme (here dark-theme) @include mat-select-theme($dark-theme); Include the mat-select-color mixin to override the default theme (here with the light-theme) . light-theme { @include mat-select-color($light-theme); }

What is Mat Optgroup?

The <mat-optgroup> element can be used to group common options under a subheading. The name of the group can be set using the label property of <mat-optgroup> . Like individual <mat-option> elements, an entire <mat-optgroup> can be disabled or enabled by setting the disabled property on the group.

How do you readonly mat select?

which out put looks like: It fades out the text and the lining below get changes, is it possible to make it readonly? You can replace the mat-select form field with a input box and bind input box data with mat-select data.


4 Answers

You need to apply style on mat-option rather than mat-select as:

<mat-select [(ngModel)]="someValue">
  <mat-option class="empty-select"></mat-option>
  <mat-option style="color: red" class="not-empty-select" *ngFor="let unit of units [value]="unit">{{unit}}</mat-option>
</mat-select>

Also you can set color in class not-empty-select.

Update 1:

If you want to change color of selected option add following CSS:

.not-empty-select.mat-selected {
  color: green !important;
}

Update 2:

If you want to change color in select box modify CSS as:

.not-empty-select.mat-selected {
  color: green !important;
}

:host /deep/ .mat-select-value-text {
  color: green !important;
}
like image 129
Anshuman Jaiswal Avatar answered Oct 22 '22 02:10

Anshuman Jaiswal


You can overwrite the css for following elements as -

Select box panel

/deep/ .mat-select-panel {
    background-color: red;
}

Select box options element

/deep/ .mat-form-field-infix{
     background-color: red;
}

Select box

/deep/ .mat-select{
   background-color: red;
}

do not forget to add /deep/ as mentioned.

Select Box text color

Use this one if you want to change the text color of select.

/deep/ .mat-form-field-type-mat-select .mat-form-field-label, .mat-select-value {
  color: red;
}

Demo copy is here - https://stackblitz.com/edit/angular-material-pagination-123456-5teixy

like image 41
Sunil Singh Avatar answered Oct 22 '22 00:10

Sunil Singh


If you check closely mat-select-value is the class which holds the css for placeholer font in mat-select tag;

so Using

::ng-deep .mat-select-value{    
      color: white!important;  }

will apply the color you give. !important is to override the default value.

like image 13
Ganapathy Avatar answered Oct 22 '22 02:10

Ganapathy


Add this to your styles.css file

.mat-select-red .mat-select-value{
   color: red !important;
}

In your component.html file

<mat-select class="mat-select-red" [(ngModel)]="someValue">
  <mat-option>class="empty-select"</mat-option>
  <mat-option class="not-empty-select" *ngFor="let unit of units [value]="unit">{{unit}}</mat-option>
</mat-select>
like image 1
Dushyanth Kandiah Avatar answered Oct 22 '22 02:10

Dushyanth Kandiah