Angular Material Overriding Default Style of SnackBar Component

前端 未结 6 1398
甜味超标
甜味超标 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 07:53

    Verified for @angular/material v7.0.x:

    CSS !important modifier does the trick.

    Put this is src/styles.scss (the app's global css):

    .mat-snack-bar-container {
      max-width: 100% !important;
    }
    

    Also we tweak its font:

    /* Overrides SnackBar CSS in Material Design's .mat-simple-snackbar class */
    /* Original sizes: font: 24px, height: 47.952px */ 
    .mat-simple-snackbar {
      display: flex; 
      font-size: 28px !important; // 28px  is double, 42px for triple
      min-height: 70px !important; // 70px for double, 90px for triple
      align-items: center !important;
      justify-content: center !important;
    }
    
    0 讨论(0)
  • 2021-02-04 07:57

    To do this properly, you need to set the View Encapsulation to None on your component:

    @Component({
        templateUrl: './my.component.html' ,
        styleUrls: ['./my.component.css'], 
        encapsulation: ViewEncapsulation.None,
    })
    

    Then in your component css you can just do this:

    .mat-snack-bar-container {
       max-width: 800px;
    }
    

    From the official docs:

    View Encapsulation = None means that Angular does no view encapsulation. Angular adds the CSS to the global styles. The scoping rules, isolations, and protections discussed earlier don't apply. This is essentially the same as pasting the component's styles into the HTML.

    0 讨论(0)
  • 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.
    0 讨论(0)
  • 2021-02-04 08:03

    The way to go.

    
    encapsulation: ViewEncapsulation.None
    
    

    Here is a stackblick to demonstrate

    0 讨论(0)
  • 2021-02-04 08:07

    As of June 30, 2019, using Angular Material 8.0.1 with Angular 8.0.3, the following SCSS and typescript seems to work for overriding the color of the action button in an Angular Material snackbar *without using !important *:

    styles.scss (not the extremely long duration, which allowed me to inspect the styling before it disappeared):

    $snackBarTextColor: white;
    $snackBarBackgroundNormal: #087a51;
    $snackBarActionColor: lightgray;
    .snackBarInfo {
        background-color: $snackBarBackgroundNormal;
        color: $snackBarTextColor;
    
    
    }
    .mat-simple-snackbar > span {
        font-weight: bold;
    }
    .mat-simple-snackbar-action {
        .mat-button {
            .mat-button-wrapper {
                color: $snackBarActionColor;
            }
        }
    }
    

    app.module.ts:

    import { MAT_SNACK_BAR_DEFAULT_OPTIONS } from '@angular/material/snack-bar';
    providers: [
            {
                provide: MAT_SNACK_BAR_DEFAULT_OPTIONS,
                useValue: {
                    duration: 41000,
                    horizontalPosition: 'center',
                    verticalPosition: 'bottom',
                    panelClass: 'snackBarInfo'
                }
            }
        ]
    
    0 讨论(0)
  • 2021-02-04 08:13

    Put css in your styles.scss or styles.css

    .snackbar {
        max-width: 90% !important;
        margin-left: auto !important; // center align from left
        margin-right: auto !important; // center align from right
        margin-bottom: 1rem !important; 
        padding: 10px !important; // spacing between the text and boundary
        background-color: green;
        color: color;
    
        .mat-button-wrapper {
            color: black !important; // action text color
        }
    }
    

    Note: make sure you have set !important with every style, without it, style wouldn't work.

    in component.ts

    this.snackbar.open(this.resMsg.message, 'OK', {
        panelClass: 'snackbar'
    })
    

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