85

I have to change the background from the snackbar component. I'm using it to alert or inform the user about some either error or completed action the user did.

The material version from the project. "@angular/material": "^5.0.0-rc.1",

The documentation https://material.angular.io/components/snack-bar/api say about an api to change the class.

panelClass: string | string[] Extra CSS classes to be added to the snack bar container.

This is what I add in the css file.

.mycsssnackbartest {
  background-color: blue;
}

This is the code to open the snackbar

this.snackBar.open('Informing the user about sth', 'User Message' , {
    panelClass: ['mycsssnackbartest ']
} );

What am I doing wrong?

  • 2
    `MatSnackBarConfig` has a field named `extraClasses` "Extra CSS classes to be added to the snack bar container." You are using `panelClass` which it doesn't exists – J-R Choiniere Nov 29 '17 at 20:02
  • If found panelClass in the api documentation. https://material.angular.io/components/snack-bar/api. I didn't use extraClasses because the documentation says it is Deprecated. But I'm going to test it. Thanks. – andre luis borges de paula Nov 30 '17 at 11:06

5 Answers5

175

Angular < V15

You have to use the panelClass option (since v6) to apply classes on a snackbar like this:

this.snackBar.open(message, action, {
  duration: 2000,
  panelClass: ['blue-snackbar']
});

CSS (in global styles.scss):

.blue-snackbar {
  background: #2196F3;
}

See the Stackblitz example

Angular >= v15

The Angular team did add global css variable

So you still need to add the panelClass and you can add it to CSS like this

  .mat-mdc-snack-bar-container {
    &.blue-snackbar {
      --mdc-snackbar-container-color: #2196f3;
      --mat-mdc-snack-bar-button-color: #fff;
      --mdc-snackbar-supporting-text-color: #fff;
    }
  }
Philipp Kief
  • 8,172
  • 5
  • 33
  • 43
63

Using themes:

Primary:

this.snackBar.open('Some message','Some action', {
    duration: 2000,
    panelClass: ['mat-toolbar', 'mat-primary']
});

Switch: 'mat-primary' to 'mat-accent' or 'mat-warn'

Falko
  • 17,076
  • 13
  • 60
  • 105
FranSanchis
  • 1,455
  • 14
  • 15
  • 9
    This should really be the accepted answer, as it does it properly by using themes. – Purple Piranha Jan 17 '20 at 12:53
  • 3
    @PurplePiranha I don't agree, `mat-toolbar` is another element of angular material and using this config changed the font size on my `mat-snackbar` which is not really intended. Can be useful in some cases though... – A. Markóczy Feb 05 '20 at 12:30
  • This is just a (in my opinion bad) workaround as it uses the class of a different element to style the snackbar. The OK button for example looks odd as it does not get affected by the styling with this approach. It would be better to make your own class and put it into global styles.scss (as suggested by the accepted answer) – Tobias Kaufmann Nov 27 '20 at 13:41
22
//in component.ts
this.snackbar.open(message, '', {
    duration: 2000,
    verticalPosition: 'top',
    panelClass: ['warning']
 });
//in component.css
::ng-deep .warning{
   background: 'yellow';
}
shreekar hegde
  • 273
  • 2
  • 5
  • 1
    I'm using Angular 7 and this worked for me, not the accepted answer. You have to add ::ng-deep in front of the class name in your css. Thanks so much! – muzurB Apr 26 '19 at 03:18
  • 2
    ::ng-deep is depreciated and will not be supported in the future by Angular https://angular.io/guide/component-styles – tif May 07 '19 at 13:29
  • what should we use instead in this case ? – An-droid Sep 04 '19 at 15:34
  • @An-droid, Angular has not set a version for the removal, since there is no replacement option yet (https://angular.io/guide/deprecations). But I would advise to use global styling (via styles.scss) in combination with the panelClass option for this instead of component specific styling – Mr.wiseguy Sep 05 '19 at 12:52
  • by doing so I'm forced to use `!important` which I shouldn't be using for that application TT.TT – An-droid Sep 06 '19 at 08:13
  • 1
    By giving it a panel class you can give it more specific styling, thus overriding the material styling: `.mat-snack-bar-container.warning { background-color: red }`. – Mr.wiseguy Sep 16 '19 at 09:30
5

Yeah after adding ::ng-deep in global style file(.css) works fine after each attribute... As follows

noMatchConfig: MatSnackBarConfig = {
    duration: 2000,
    horizontalPosition: 'right',
    verticalPosition: 'top',
    panelClass:['redNoMatch']
  }



::ng-deep .redNoMatch
{
  color:white;
  background: #F44336 !important;
}
Adam reuben
  • 45
  • 1
  • 4
1

This can also be achieved globally by importing MAT_SNACK_BAR_DEFAULT_OPTIONS in app.module.ts:

app.module.ts

import {
  MatSnackBarModule,
  MAT_SNACK_BAR_DEFAULT_OPTIONS,
} from '@angular/material/snack-bar';

@NgModule({
  providers: [
    {
      provide: MAT_SNACK_BAR_DEFAULT_OPTIONS,
      useValue: { panelClass: ['mycsssnackbartest'] },
    },
  ],
})

styles.css

.mycsssnackbartest {
  background-color: blue;
}
umbe1987
  • 2,894
  • 6
  • 35
  • 63