Angular Material Overriding Default Style of SnackBar Component

前端 未结 6 1397
甜味超标
甜味超标 2021-02-04 07:34

I am attempting to override the default max-width of the snackbar component in Angular Material.

The CSS applied by Angular Material is shown below:

.mat         


        
6条回答
  •  死守一世寂寞
    2021-02-04 08:02

    Angular 10 and without special tricks:

    1. use panelClass when opening the snackbar, for example:
    this.snackBar.open("VeryLongTextWithoutThePossibilityOfBreakingAutomatically", "X", {
        duration: 0,
        panelClass: 'notif-error'
    });
    

    duration: 0 is only for debugging.

    1. write this into your style.css
    .notif-error {
        background-color: red;
    }
    .mat-snack-bar-container.notif-error {
        max-width: 100%;
    }
    

    Now because of css-specificity, this will be the most specific rule.

    • Beware that there should not be space between .mat-snack-bar-container and .notif-error.
    • This rule will apply to elements which has both the .mat-snack-bar-container and .notif-error classes.
    • This also works when your .notif-error class is empty.

提交回复
热议问题