I'm trying to change the style of the md-dialog.
in my main.scss i'm importing the prebuild pink-bluegrey theme... then in my component I import the following -->
@import "@angular/material/dialog/dialog.scss";
$mat-dialog-padding: 0;
$mat-dialog-border-radius: 0.5rem;
$background: #ffffff;
@mixin mat-dialog-container {
padding: $mat-dialog-padding;
border-radius: $mat-dialog-border-radius;
background: $background;
}
@include mat-dialog-container;
The padding and border radius is correctly applied to the dialog window. But the background is not working... also tried the !important statement.
I'm using this in a single component... Is there also a change to apply those styles globally?
in chrome dev tools I see those applied style changes. The background gets overwritten by the pink-bluegrey theme..
hope anyone can help.
thanks
MatDialog creates modal dialogs that implements the ARIA role=”dialog” pattern by default. You can change the dialog's role to alertdialog via MatDialogConfig . You should provide an accessible label to this root dialog element by setting the ariaLabel or ariaLabelledBy properties of MatDialogConfig .
First, we can open a dialog from one of our components: Ok let's see what is going on here: In order to create Material Dialog instances, we are injecting the MatDialogservice via the constructor. Then we are creating an instance of MatDialogConfig, which will configure the dialog with a set of default behaviors.
if you want to change background color different for dialogs, set background color of = transparency. then set background color for dialog component.
The MatDialogRef provides a handle on the opened dialog. It can be used to close the dialog and to receive notifications when the dialog has been closed. Any notification Observables will complete when the dialog closes. dialogRef. afterClosed().
It is better practice to add a wrapper class around your dialog, and then add styling to the children. Have a look at this article for more information.
When you open your Angular dialog, you can add a panelClass
attribute, like this:
this.dialog.open(MyDialogComponent, {panelClass: 'my-panel'})
.
then, in your css (e.g. in the root styles.css file), you can add the following:
.my-panel .mat-dialog-container {
overflow: hidden;
border-radius: 5px;
padding: 5px;
}
EDIT Warning
It is also possible to add the css to another file than the root styles.css, but then you have to use ::ng-deep
in the css (e.g. ::ng-deep .my-panel{ // ... }
). This is not advised, as ::ng-deep
is deprecated in Angular
EDIT2 Good alternative
If you are using scss, then you can place your .my-panel
-style in your mydialog.component.scss file, by using a @mixin
, and @import
the file in styles.scss. You can then use @include
to load the defined mixin.
in your mydialog.component.scss file
@mixin myPanel(){
.my-panel .mat-dialog-container {
// css here
}
}
in your styles.scss
@import 'path/to/mydialog.component.scss' // you don't need the .scss suffix
@include myPanel();
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