How can I style Angular 10 Material Select (and others) in my own way

Viewed 228

I am trying to give my angular material dropdown menu its own style. I am using Angular 10. I have done a lot of research an tried a lot of things I could find on Stackoverflow and others, but when I copy approaches from the internet, the style of my mat-select doesn't change. Here my current code:

import { Component, OnInit, ViewEncapsulation } from '@angular/core';


interface Radius {
  value: number;
  viewValue: string;
}

@Component({
  selector: 'app-landing-page',
  animations: [],
  templateUrl: './landing-page.component.html',
  styleUrls: ['./landing-page.component.scss'],

  encapsulation: ViewEncapsulation.None
})
export class LandingPageComponent implements OnInit { // The dropdown menu containing component
  // .....

  radiusList: Radius[] = [
    {value: -1, viewValue: "Nothing"},
    {value: 5, viewValue: "5km"},
    {value: 10, viewValue: "10km"},
    {value: 15, viewValue: "15km"},
    {value: 25, viewValue: "25km"},
    {value: 50, viewValue: "50km"},
    {value: 100, viewValue: "100km"},
  ]
  selectedRadius: Radius;
  
  
  // .....
}

// ---------------------------------
// app.module.ts

import {MatSelectModule} from '@angular/material/select'; 
// ...

@NgModule({
  declarations: [
    AppComponent,
    LandingPageComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpClientModule,

    AppRoutingModule,
    ReactiveFormsModule,
    BrowserAnimationsModule,
    MatSelectModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }
.test.mat-select-panel {
    background: red; // also background-color does not work
}
<mat-form-field appearance="fill">
    <mat-label>Umkreis</mat-label>
    <mat-select panelClass="test" [(ngModel)]="selectedRadius" name="radius">
        <mat-option *ngFor="let radius of radiusList" [value]="radius.value">
            {{radius.viewValue}}
        </mat-option>
    </mat-select>
</mat-form-field>

Can somebody give me a hint how I can style the dropdown menu? Will that be everywhere the same way if I would use other components from Angular Material?

Thank you in advance!

2 Answers

You have to use the ::ng-deep selector in your CSS.

For example:

::ng-deep .mat-select-panel {
    background: red; // also background-color does not work
}

But care, ::ng-deep force a style down through the child component tree into all the child component views and according to the official documentation, this way is deprecated.

You can find more information here

Just put your styles in the styles.scss file at the app level

Related