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.
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); }
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.
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.
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;
}
You can overwrite the css for following elements as -
/deep/ .mat-select-panel {
background-color: red;
}
/deep/ .mat-form-field-infix{
background-color: red;
}
/deep/ .mat-select{
background-color: red;
}
do not forget to add /deep/ as mentioned.
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
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.
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>
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