25

I use a git project for a virtual keyboard (https://ngx-material-keyboard.github.io/demo/). And I have some issues to get it running on a small device with 450*250 pixel.

At the end I found the necessary changes in the css if I modify it directly at the web browser with dev tools.

Now I have to find the right position to change the sources.

There will be used the overlay component from angular2-material to visualize the keyboard.

If I comment out the position in the cdk-overlay-container, it works:

.cdk-overlay-container {
/* position: fixed; */
z-index: 1000;

}

But I cant overwrite these from my angular application. Any suggestions?

Screenshot of changes

FAISAL
  • 33,618
  • 10
  • 97
  • 105
user1025633
  • 263
  • 1
  • 3
  • 5

8 Answers8

47

UPDATED ANSWER

From the official documentation:

Styling overlay components

Overlay-based components have a panelClass property (or similar) that can be used to target the overlay pane.

You can override the default dialog container styles by adding a css class in your global styles.css. For example:

.custom-dialog-container .mat-dialog-container {
    /* add your styles */
}

After that, you'll need to providies you css class as a panelClass parameter to your dialog:

this.dialog.open(MyDialogComponent, { panelClass: 'custom-dialog-container' })

Read this official documentation for more information.


ORIGINAL ANSWER

Use ::ng-deep in your component.css to override the default styles.

::ng-deep .cdk-overlay-container {
    /* Do you changes here */
    position: fixed; 
    z-index: 1000;
}
Community
  • 1
  • 1
FAISAL
  • 33,618
  • 10
  • 97
  • 105
  • 2
    It's worth noting that ::ng-deep is deprecated - but I can't find clarification on timeframes or alternatives – Mark Jul 05 '18 at 02:05
  • 8
    ::ng-deep doesn't stay in isolated scope, it will effect other views too – Mar Jul 19 '18 at 13:50
  • 2
    What if i need to set z-index of .cdk-overlay-container for mat-menu and mat-dialog separately. as i have fixed header on my site – Awais Oct 16 '19 at 09:47
  • 1
    For this case `::ng-deep` effectively does the same as to put the css code in the global styles. You're better off putting it in global styles - because it isn't scoped to your component. In fact it's worse than that - lets say you put background: blue - and opened a dialog from another component. It wouldn't be blue right. But then you open this component and it's blue - great! Then go back and open the first one again and it will still be blue. This belongs in global styles.css or scss. – Simon_Weaver Mar 11 '22 at 02:43
4

To be able to override the Material CSS classes from your component styles, you will need to set the View Encapsulation to None on your component:

@Component({
    templateUrl: './my.component.html' ,
    styleUrls: ['./my.component.scss'], //or .css, depending what you use
    encapsulation: ViewEncapsulation.None,
})
Andrei Matracaru
  • 3,511
  • 1
  • 25
  • 29
4

If you want to change the styling of mat-dialogue-container adding a panel class and giving style is enough, but in case if you want to change the styling of cdk-overlay-container then adding a backdropClass will help

const dialogRef = this.dialog.open(PopupComponent, {
  backdropClass: 'popupBackdropClass',
  panelClass: 'custom-dialog-container',
  data: { data: data }
});

in css add

.popupBackdropClass {
   background-color:yellow
 }
Pran R.V
  • 1,015
  • 12
  • 21
1

Overriding cdk-overlay-containeris not good since it is gonna affect all other components. You can create custom OverlayContainer. See example.

Vugar Abdullayev
  • 1,852
  • 3
  • 21
  • 46
0

I found that by using the main/default "Styles.css". Styling changes for the CDK (as well as Material and Animations) are picked-up properly. (caveat) I am using Angular 4 .

Awais
  • 4,752
  • 4
  • 17
  • 40
0

I sort it out myself by using two stylessheet one Global and other component's stylesheet, In global i set z-index to to lower value(1000) so that it goes behind the header and in popup component styles sheet i set that to high value(2000) with !important so that header goes behind my overlay.

That's how i manage to solve it.

Thank me later

Awais
  • 4,752
  • 4
  • 17
  • 40
0

Avoid using encapsulation: ViewEncapsulation.None, because the css classes inside the component's css file, will influence the whole app. The best way is the one suggested by @Faisal

0

I thought up a better(mmm.. more interesting maybe) way to do this by using the :has() css selector. Even though you can't set the class of the .cdk-overlay-container you can set the panelClass of the cdk-overlay-pane. If you set the panelClass in your dialog config you can then add a ::ng-deep statement in the component scss file that uses that panelClass to target the cdk-overlay-container element that contains an element with that matching panelClass.

const dialogRef = this.dialog.open(MyDialogComponent, {
    panelClass: 'my-dialog-panel',
    hasBackdrop: false
});

then in the scss file add a:

::ng-deep .cdk-overlay-container:has(.my-dialog-panel) {
    z-index: 3000;
}

you could also avoid the ng-deep and just add it to the global styles too and the class would still be reasonably isolated compared to the alternatives.