Overriding Angular Material Size and Styling of md-dialog-container

后端 未结 5 1088
天涯浪人
天涯浪人 2020-12-30 00:14

I am using Angular Material and I am using md-dialog for my popups for my gallery. The default animation and styling looks nice. However, I would like to change the width an

相关标签:
5条回答
  • 2020-12-30 00:25

    There is no need to add global styles.

    You can add this to your own component style:

    ::ng-deep .my-custom-dialog-class {
      mat-dialog-container {
        padding: 0;
      }
    }
    

    Make sure there is no "." (dot class selector) for mat-dialog-container because you must use the html tag selector.

    Then when you open the dialog, add your class in panelClass property:

    this.dialog.open(MyDialogComponent, {
      data: {},
      panelClass: 'my-custom-dialog-class'
    });
    

    By using a custom panelClass, this ensures only dialogs opened with that class will be affected, even using ng::deep.

    0 讨论(0)
  • 2020-12-30 00:29

    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.

    0 讨论(0)
  • 2020-12-30 00:33

    You can change width/height of the modal by setting the values in the open() method, something like the following:

    this.dialog.open(MyDialogComponent, {
      height: '300px'
      width: '400px'
    });
    
    0 讨论(0)
  • 2020-12-30 00:36

    If all that wouldn't work, you can try

    const e: HTMLElement = document.getElementById(this.element.nativeElement.parentElement.id);
    e.style.display = 'flex';
    
    e.style.padding = '0';
    
    0 讨论(0)
  • 2020-12-30 00:49
        const dialogRef = this.dialog.open(WorkModalComponent, {
           width: '1200px',
           data: workObj,
           panelClass: 'my-dialog-container-class' // Replace with your actual dialog container class
        });
    

    add the panelClass to your Dialog and add your CSS style to this class.

    0 讨论(0)
提交回复
热议问题