38

I have this select in angular material:

enter image description here

Its code :

<md-select placeholder="Descuentos y convenios" [(ngModel)]="passenger.discount">
        <md-option [value]="null" [disabled]="true">
            Descuentos
        </md-option>
        <md-option *ngFor="let option of discounts" [value]="option">
            {{ option }}
        </md-option>
        <md-option [value]="null" [disabled]="true">
            Convenios
        </md-option>
        <md-option *ngFor="let option of agreements" [value]="option">
            {{ option }}
        </md-option>
</md-select>

And I would like to have this styles in it:

enter image description here

I tried to put some classes over md-select and md-option, but not worked. I would like to have maybe just an example of how to put the background color or the border and that would give me an idea.

Thank you in advance

3
  • The components CSS is not working? Commented Apr 28, 2017 at 23:48
  • I exactly do not know how to for example put the border of the options.
    – Kimy BF
    Commented Apr 29, 2017 at 0:02
  • Not all HTML elements that Angular generates are marked with the component name, so the component css has only limited value.
    – TAM
    Commented Mar 1, 2018 at 10:57

12 Answers 12

52

I think classes should work, but you may need to use /deep/ because of the view encapsulation.

Try this:

/deep/ md-select.your-class {
  background-color: blue;
}

You can also play with theming.

4
  • 20
    Note: the /deep/ selector, as well as >>> and ::ng-deep are all being deprecated. More information here. Unfortunately there does not appear to be a replacement.
    – cmegown
    Commented Nov 8, 2017 at 20:24
  • 5
    There better be a replacement. That would be a big mistake to remove without one.
    – Michael
    Commented Oct 10, 2018 at 15:19
  • Although its deprecated, but this is still working until now, the fastest and simplest solution. Commented Jun 30, 2019 at 4:38
  • On Angular use ::ng-deep .className { color: #8d062a !important; }
    – Cesar Vega
    Commented Mar 17, 2021 at 8:13
21

The top solutions of /deep/, >>> and ::ng-deep are being deprecated and should no longer be used.

The recommended method is now view encapsulation


Edit: Word of warning. I do not recommend using this method at all (as of Jan 2019) as setting ViewEncapsulation.None will result in any of that components css becoming global styles (it stops Angular from creating ng_xxx attributes for component scoped css). This will result in global style conflict, especially with lazy loaded module css.

Our solution to ViewEncapsulation was to override very specific css using highly specific css selectors in 1) global css or 2) creating separate style files for certain views / styles / elements, importing into every component required (e.g. styleUrls: [material-table-override.css, component.css]).


I used ViewEncapsulation.None to successfully override material table styles within a single component in Angular 6.

On your component:

import { ViewEncapsulation } from '@angular/core';
// ...
@Component({
    // ...
    encapsulation: ViewEncapsulation.None,
})

Here's a great article on the subject

1
  • This solution is in some cases not optimal because it overrides CSS styles of the parent component if a mat-dialog component is being used. A better solution should manipulate material compnents within the boundaries of the dialog component.
    – Lukaesch
    Commented Jan 4, 2022 at 23:00
13

If you can solve your style issues with the material 2 scss theming that would be the "right" way hears is a link to there site. https://material.angular.io/guide/theming.

However I used !important on the styles I didn't want materials styles to overwrite.

Here is how I used it:

/*hack to get rid of a scrollbar*/
.cdk-focus-trap-content{
    overflow-x: hidden !important;
}

/*hack to get rid of a padding on the popup*/
.mat-dialog-container{
     padding: 0px !important;
 }

I had a situation where a horizontal scroll bar was showing up in the md-sidenav and I got rid of their default padding on the md-dialog.

Not the most elegant solution but I hope this helps.

This is another StackOverflow question that discusses what !important is.

What does !important in CSS mean?

1
  • 6
    Yeah I'm finding I have to slap !important everywhere to override the Material styles. This is very annoying. Commented Aug 21, 2018 at 23:59
11

The correct way to change styles of overlay classes like mat-dialog-container is to use panelClass according to Customizing Angular Material component styles:

Add this to your global stylesheet after your theme setup

.myapp-no-padding-dialog .mat-dialog-container {
  padding: 0;
}

Use the following to open the dialog

this.dialog.open(MyDialogComponent, {panelClass: 'myapp-no-padding-dialog'})
1
  • I like this and is the cleanest answer I have seen so far. Commented Nov 8, 2020 at 14:47
10

you can use:

::ng-deep {
 .mat-dialog-container{
   padding: 0px 
 }
}
1
  • 2
    This is very dangerous solution, becouse this will affect all components in whole app. If you want it like this, you can override in golbal style.scss. Otherwise, prefix ng-deep with some selector. Like :host ::ng-deep or .mat-dialog-override::ng-deep...
    – prespic
    Commented Mar 5, 2021 at 13:08
7

::ng-deep works like a charm for me... for scss and sass file.

2

I do it this way whenever I have to remove scroll from mat-sidenav-container

   .mat-sidenav-container-classname ::ng-deep mat-sidenav-content {
      overflow: hidden;
    }
2
  • 1
    great solution. prevents your changes to mat-sidenav-content in that component from interfering with other instances of mat-sidenav-content
    – clk
    Commented Jun 17, 2021 at 19:07
  • glad it helped! @clk Commented Jun 18, 2021 at 20:48
1

I was facing the same issue with Angular Material, I inspected the select element, checked its classes and tried to overwrite the css rules for material css classes , even tried !important flag for style rules. But it was not working.

Moving all such rules from component to index.html worked for me.

.mat-select-value,
    .mat-option-text,
    .mat-standard-chip,
    .mat-input-element,
    .mat-menu-item {
         color: #666 !important;
         font-size: 13px !important;
     }
}
1

you can use :root as well

:root {
    .mat-dialog-container{
        padding: 0px 
    }
}
0

You can try adding this code.

.mat-dialog-container{
     padding: 0px !important;
}

If this does not work you can use

/deep/.className {
... your code goes here
}
0

Me also faced this kind of situation, but I found a permanent solution to over come this.

If we use ::ng-deep or :root/CLASS_NAME or ViewEncapsulation it may affect other components also. So what you can do is,

  1. Make sure your global style sheet is present at the end of styles array in your angular.json file
"styles": [
      "@angular/material/prebuilt-themes/indigo-pink.css",
      "src/styles.css"     <-----------
    ],
  1. Inspect your code and get the relevant class from the browser
  2. Get the relevant class and override it in your global style file (styles.css)

 .mdc-dialog__surface{
    background-color: antiquewhite !important;
    box-shadow:none !important;
  }
  
  .cdk-overlay-backdrop.cdk-overlay-backdrop-showing {
    opacity: 0.5;
  } 
Updated style output

  1. Similarly you can modify other Angular material components and Bootstrap inbuilt styles also.
-1

Anyone looking for a cleaner approach in 2020, use the panelClass as suggested in the material mat-select documentation. Use the panelClass property

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.