Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set mat-menu style

Im having alot of trouble with this, and the apparent solutions aren't working or im doing something wrong (probably the latter).

I want to style my mat-menu and the mat-menu-item's, ive tried doing this:

::ng-deep .mat-menu{
  background-color:red;
}

But it doesnt work, my menu looks like this (nothing abornomal)

<mat-menu #infoMenu="matMenu">
  <button mat-menu-item>
    <mat-icon>dialpad</mat-icon>
    <span>Resume</span>
  </button>
  <button mat-menu-item>
    <mat-icon>voicemail</mat-icon>
    <span>Portfolio</span>
  </button>
  <button mat-menu-item>
    <mat-icon>notifications_off</mat-icon>
    <span>References</span>
  </button>
  <button mat-menu-item>
    <mat-icon>notifications_off</mat-icon>
    <span>Contact</span>
  </button>
</mat-menu>

I have also tried /deep/ but I know that shouldn't work and in fact should be depreciated in Angular 4 but I did it to test, I am not sure how to style the elements at this point.

like image 713
Cacoon Avatar asked Oct 19 '17 00:10

Cacoon


People also ask

What is mat-menu?

mat-menu selector is used to display floating menu panel containing list of menu items.

What is matMenuTriggerFor?

matMenuTriggerFor is passed the menu identifier to attach the menus.

How do you use matMenuTriggerData?

This is done by typing [matMenuTriggerFor]="app-menu" . The next thing we do is passing the component's member data to the mat-menu through this directive: [matMenuTriggerData]="menuData" . The mat-menu instance that we named app-menu can now grab the content of that member data.

How do I close Matmenu?

As mentioned in the above Github issue, there are following issues in the first SO solution. Hover the mouse cursor on the button and the menu will pop up. But if you click on the button, it will hide and show the menu.


2 Answers

Easier Way If you want to change your component only without affecting other components, you should add a class to the menu.

<mat-menu #infoMenu="matMenu" class="customize"></mat-menu>

Then style your menu with ::ng-deep.

::ng-deep .customize {
  background: red;
}

voila!! your customization will not affect other components.

Another Way: you can add backdropClass to the menu.

 <mat-menu #infoMenu="matMenu" backdropClass="customize"></mat-menu>

Then add CSS style class with +* in your styles.css

.customize+* .mat-menu-panel {
  background: red;
}

This is also accomplished without affecting others, but adding css in styles.css may be a bit inconvenient.

like image 73
Md Rafee Avatar answered Sep 21 '22 05:09

Md Rafee


Define original background-color and mouseover color both in the CSS:

.mat-menu-item {
  color: blue !important;
}

button.mat-menu-item{
    background-color: white;
}

button.mat-menu-item:hover {
    background-color: blue;
    color: #fff !important;
}
like image 35
Thanigai Arasu Avatar answered Sep 20 '22 05:09

Thanigai Arasu