adding a custom class to a mat-menu

mat-menu focus first item
mat-menu-item (click)
mat-menu close event
mat-menu with input
mat-menu position
mat-menu is not a known element
matmenutriggerdata
mat-icon menu

I use several menus for my app and I use angular material mat-menu component for this. I can change the style of all menus by writing css code in my global css file for menu original classes. but when I want to add some specific styles to one of them using .custom-class-name .original-material-class-name{} it doesn't apply those styles to that one menu.

here's the whole app in stackblitz: app

header.component.html:

<div>
<a mat-button class="theme-menu-toggle-button" *ngIf="!menuAvailable" 
(click)="changeSidenavMode(!mode)">
  <mat-icon>menu</mat-icon>
</a>
<a href="#" fxHide.lt-md fxShow.gt-sm class="theme-user" mat-button 
[matMenuTriggerFor]="menu">
  <img src="assets/images/user.png" class="theme-profile-image rounded-circle">
  <span class="theme-profile-title">نام نام‌خانوادگی</span>
</a>
<mat-menu #menu="matMenu" class="profile-menu">
  <button mat-menu-item *ngFor="let option of profileOptions">
    <mat-icon>{{option.icon}}</mat-icon>
   <span>{{option.title}}</span>
  </button>
</mat-menu>

styles.css:

.profile-menu .cdk-overlay-pane::before{
 width: 0;
 height: 0;
 border-left: 8px solid transparent;
 border-right: 8px solid transparent;
 border-bottom: 15px solid #5E35B1;
 content: " ";
 position: absolute;
 top: 10px !important;
 animation: fadeInRightBig 0.75s;
}

Add custom class in the mat-menu in backdropClass:

   <button mat-button [matMenuTriggerFor]="menu">Menu</button>
    <mat-menu #menu="matMenu" backdropClass="custom__menu">
      <button mat-menu-item>Item 1</button>
      <button mat-menu-item>Item 2</button>
    </mat-menu>

    .custom__menu + * .cdk-overlay-pane > div.mat-menu-panel > div.mat-menu-content {
      background-color: #777;
      button.mat-menu-item {
        color: white;
      }
    }

Adding panelClass to mat-menu does nothing · Issue #10322 , custom { background-color: darkgrey; border-radius: 0px; } in my global styles file. And I have <mat-menu #appMenu="matMenu" [class]="custom"  Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. adding a custom class to a mat-menu. 0.

Add your stylings to header.component.css, and import it in your page:

@Component({
    selector: 'my-selector',
    templateUrl: './header.component.html',
    styleUrls: ['./header.component.css']
})

UPDATE: You need to add !important tags at end of your css tags.

.profile-menu .cdk-overlay-pane::before{
 width: 0 !important;
 height: 0 !important;
 border-left: 8px solid transparent !important;
 border-right: 8px solid transparent !important;
 border-bottom: 15px solid #5E35B1 !important;
 content: " " !important;
 position: absolute !important;
 top: 10px !important;
 animation: fadeInRightBig 0.75s !important;
}

customize mat-menu-panel panel of Matmenu · Issue #16742 , Without setting the view encapsulation to None , you'll want to add a custom class to the <mat-menu> which will show up on the panel. You can  Bug: Adding panelClass to mat-menu does nothing. The problem is that material menu gets rendered using cdk-overlay-pane, which is outside of the component and imposible to style without using ::ng-deep, which interferes with other compon

you should use id for html tag and use it in css file

What do you mean?

Menu, <mat-menu> is a floating panel containing list of options. class MyComponent { @ViewChild(MatMenuTrigger) trigger: MatMenuTrigger; someMethod()  If a class was applied to the mat-tab, I would expect the generated html to apply the class to both the label and the body. This would allow the dev to manage both with the addition of the appropriate selector. (.mat-tab-label.first or.mat-tab-body.first)

Adding panelClass to mat-menu does nothing, Class to be added to the backdrop element. @Input This method takes classes set on the host mat-menu element and applies them on the menu template that  I think it's a slippery slope if we start adding inputs for all the different elements that leads us to having a bunch of inputs that just proxy classes. You can still add a class to the .mat-menu-panel by having a class on the mat-menu element.

Styling mat-menu items – Angular Days, @Input('class') panelClass: string This method takes classes set on the host mat-​menu element and applies them on the menu template that displays in the overlay  You can pass custom classes to menus. <md-menu #menu="mdMenu" [overlapTrigger]="false" class="my-full-width-menu">. Then you can target that class with global styles. For your needs, unfortunately, you'll need to know some information about where your menu overlay is positioned, and hardcode some repositioning.

Angular Material - dynamic style for mat-menu - angular - html, <mat-menu #playlistMenu="matMenu"> <button mat-menu-item class="​plMenuCSS"> aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii jjjj kkkk llll  The mat-menu instance that we named app-menu can now grab the content of that member data. As you can see, the <ng-template> accesses the property of 'menuData' that we have named 'menuItems' . The <ng-template> adds a pointer or alias to that property (called aliasMenuItems ), like this: <ng-template let-aliasMenuItems="menuItems"> .

Comments
  • didn't make any difference
  • I added important. still no difference. according to angular material, applying custom styles to overlay components like menu is not that easy. material.angular.io/guide/customizing-component-styles and I don't know how to do this.
  • I found this, I hope it will help: stackoverflow.com/questions/51932134/…
  • thanks, but nothing works... global style, encapsulation.None ,... I tried all of them it doesn't work
  • try to override material css (use F12) + encapsulation.None that works for me
  • I just want that menu to change. using id doesn't change the before pseudo element of that menu. you changed the color to red now that is red in all menus.
  • Can you give a picture and show me what exactly you want to change?